loopback 4 如何避免在购物车中创建超过 5 件商品
loopback 4 how to avoid creating more than 5 items in a shopping cart
我是 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;
创建一个拦截器,
检查拦截器中的项目计数,如果计数大于 5 则抛出错误,否则继续 next()
我是 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;
创建一个拦截器, 检查拦截器中的项目计数,如果计数大于 5 则抛出错误,否则继续 next()