import { put , get , del , param , requestBody , HttpErrors , post , } from '@loopback/rest' ; import { authenticate } from '@loopback/authentication' ; import { authorize } from '@loopback/authorization' ; import { UserProfile , SecurityBindings } from '@loopback/security' ; import { inject } from '@loopback/core' ; import { repository } from '@loopback/repository' ; import { ShoppingCartRepository } from '../repositories' ; import { ShoppingCart , ShoppingCartItem } from '../models' ; import { basicAuthorization } from '../services' ; import debugFactory from 'debug' ; import { OPERATION_SECURITY_SPEC } from '../utils' ; const debug = debugFactory ( 'loopback:example:shopping' ) ; /**
 * Controller for shopping cart
 */ export class ShoppingCartController { constructor ( @ inject ( SecurityBindings . USER ) public currentUserProfile , @ repository ( ShoppingCartRepository ) public shoppingCartRepository ) { } param userId param cart @ post ( '/shoppingCarts/{userId}' , { security : OPERATION_SECURITY_SPEC , responses : { '204' : { description : 'User shopping cart is created or updated' , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'customer' ] , voters : [ basicAuthorization ] } ) async create ( @ param . path . string ( 'userId' ) userId , @ requestBody ( { description : 'shopping cart' } ) cart ) { debug ( 'Create shopping cart %s: %j' , userId , cart ) ; await this . shoppingCartRepository . set ( userId , cart ) ; } param userId param cart @ put ( '/shoppingCarts/{userId}' , { security : OPERATION_SECURITY_SPEC , responses : { '204' : { description : 'User shopping cart is created or updated' , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'customer' ] , voters : [ basicAuthorization ] } ) async set ( @ param . path . string ( 'userId' ) userId , @ requestBody ( { description : 'shopping cart' } ) cart ) { debug ( 'Create shopping cart %s: %j' , userId , cart ) ; await this . shoppingCartRepository . set ( userId , cart ) ; } param userId @ get ( '/shoppingCarts/{userId}' , { security : OPERATION_SECURITY_SPEC , responses : { '200' : { description : 'User shopping cart is read' , content : { 'application/json' : { schema : { 'x-ts-type' : ShoppingCart } } } , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'customer' ] , voters : [ basicAuthorization ] } ) async get ( @ param . path . string ( 'userId' ) userId ) { debug ( 'Get shopping cart %s' , userId ) ; const cart = await this . shoppingCartRepository . get ( userId ) ; debug ( 'Shopping cart %s: %j' , userId , cart ) ; if ( cart == null ) { throw new HttpErrors . NotFound ( `Shopping cart not found for user: ${ userId }` ) ; } else { return cart ; } } param userId @ del ( '/shoppingCarts/{userId}' , { security : OPERATION_SECURITY_SPEC , responses : { '204' : { description : 'User shopping cart is deleted' , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'customer' ] , voters : [ basicAuthorization ] } ) async remove ( @ param . path . string ( 'userId' ) userId ) { debug ( 'Remove shopping cart %s' , userId ) ; await this . shoppingCartRepository . delete ( userId ) ; } param userId param cart @ post ( '/shoppingCarts/{userId}/items' , { security : OPERATION_SECURITY_SPEC , responses : { '200' : { description : 'User shopping cart item is created' , content : { 'application/json' : { schema : { 'x-ts-type' : ShoppingCart } } , } , } , } , } ) @ authenticate ( 'jwt' ) @ authorize ( { allowedRoles : [ 'customer' ] , voters : [ basicAuthorization ] } ) async addItem ( @ param . path . string ( 'userId' ) userId , @ requestBody ( { description : 'shopping cart item' } ) item ) { debug ( 'Add item %j to shopping cart %s' , item , userId ) ; return this . shoppingCartRepository . addItem ( userId , item ) ; } }