import { EntityRepository , Repository } from 'typeorm' ; import CartEntity from '@/entity/cart' ; import UserEntity from '@/entity/user' ; import ProductEntity from '@/entity/product' ; @ EntityRepository ( CartEntity ) class CartRepository extends Repository < CartEntity > { async findByIdxOfProductAndUser ( userIdx , productIdx ) { const cartItem = await this . findOne ( { where : { user : { idx : userIdx } , product : { idx : productIdx } , } , } ) ; return cartItem ; } async findByIdxOfCartIdxAndUser ( cartIdx , userIdx ) { const cartItem = await this . findOne ( { where : { idx : cartIdx , user : { idx : userIdx } , } , } ) ; return cartItem ; } async getCartAmountOfUser ( userIdx ) { const amount = await this . count ( { where : { user : { idx : userIdx } , } , } ) ; return amount ; } async getItems ( userIdx ) { const cartItems = await this . find ( { select : [ 'idx' , 'product' , 'createdAt' , 'updatedAt' ] , where : { user : userIdx , } , relations : [ 'product' ] , } ) ; return cartItems ; } async addItem ( product , user ) { const cart = new CartEntity ( ) ; cart . product = product ; cart . user = user ; const newCart = await this . save ( cart ) ; return newCart ; } async deleteItem ( cartIdx ) { await this . delete ( { idx : cartIdx } ) ; } } export default CartRepository ;