1

I am new to loopback 4, and almost all the documentation I found is for lower versions. I have a shopping cart, but I need to avoid it to have more than 5 items... how can I made this constraint?

These are my models:

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;

and

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;
assembler
  • 3,098
  • 12
  • 43
  • 84

1 Answers1

1

Create one interceptor, check for the item count in the interceptor, if the count is greater the 5 throw an error else continue with next()