loopback 4 как избежать создания более 5 товаров в корзине - PullRequest
1 голос
/ 02 августа 2020

Я новичок в loopback 4, и почти вся документация, которую я нашел, относится к более низким версиям. У меня есть корзина для покупок, но мне нужно избегать ее, чтобы в ней было больше 5 товаров ... как я могу сделать это ограничение?

Это мои модели:

import {Entity, hasMany, model, property} from '@loopback/repository';
import {Item} from './item.model';

@model()
export class Shoppingcar extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  id?: string;

  @property({
    type: 'string',
    required: true,
  })
  desc: string;

  @property({
    type: 'string',
    required: true,
    }
  })
  shopper: string;

  @hasMany(() => Item)
  items: Item[];

  constructor(data?: Partial<Gateway>) {
    super(data);
  }
}

export interface ShoppingcarRelations {
  // describe navigational properties here
}

export type ShoppingcarWithRelations = Shoppingcar & ShoppingcarRelations;

и

import {Entity, model, property, belongsTo} from '@loopback/repository';
import {Shoppingcar} from './shoppingcar.model';

@model()
export class Item extends Entity {
  @property({
    type: 'string',
    id: true,
    generated: true,
  })
  id?: string;

  @property({
    type: 'string',
    required: true,
  })
  name: string;

  @property({
    type: 'string',
    required: true,
  })
  date: string;

  @belongsTo(() => Shoppingcar)
  shoppingcarId: string;

  constructor(data?: Partial<Item>) {
    super(data);
  }
}

export interface ItemRelations {
  // describe navigational properties here
}

export type ItemWithRelations = Item & ItemRelations;

1 Ответ

1 голос
/ 03 августа 2020

Создайте один перехватчик, проверьте количество элементов в перехватчике, если количество больше 5, выдается ошибка, иначе продолжить с next ()

...