Skip to content

Commit

Permalink
feat: product search endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
suk-6 committed Sep 15, 2024
1 parent 90d7ca1 commit 7dfed57
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/modules/product/product.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class ProductController {
@ApiOkResponse({ description: 'Product', type: GetProductDTO })
@ApiNotFoundResponse({ description: 'Product not found' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async getProduct(@Query('id') id: string) {
return this.productService.getProduct(id);
async getProduct(@CurrentUser() { id }: JwtPayload, @Query('id') productId: string) {
return this.productService.getProduct(id, productId);
}

@Put()
Expand Down Expand Up @@ -61,6 +61,17 @@ export class ProductController {
return this.productService.getProducts(id);
}

@Get('search')
@ApiBearerAuth()
@UseGuards(AuthGuard('access'))
@ApiOperation({ summary: '상품 검색하기' })
@ApiOkResponse({ description: 'Product list', type: [GetProductDTO] })
@ApiNotFoundResponse({ description: 'Products not found' })
@ApiUnauthorizedResponse({ description: 'Unauthorized' })
async searchProduct(@CurrentUser() { id }: JwtPayload, @Query('keyword') keyword: string) {
return this.productService.searchProducts(id, keyword);
}

@Put('favorite')
@ApiBearerAuth()
@UseGuards(AuthGuard('access'))
Expand Down
32 changes: 31 additions & 1 deletion src/modules/product/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class ProductService {
});
}

async getProduct(productId: string) {
async getProduct(userId: string, productId: string) {
return this.prisma.product
.findFirstOrThrow({
where: {
Expand All @@ -137,6 +137,9 @@ export class ProductService {
select: {
userId: true,
},
where: {
userId: userId,
},
},
fundingLog: {
select: {
Expand Down Expand Up @@ -196,6 +199,33 @@ export class ProductService {
});
}

async searchProducts(userId: string, keyword: string) {
return this.prisma.product
.findMany({
where: {
name: {
contains: keyword,
},
},
include: {
userFavorite: {
select: {
userId: true,
},
where: {
userId: userId,
},
},
},
})
.then((product) => {
return product.map((product) => ({
...product,
userFavorite: !!product.userFavorite.length,
}));
});
}

async favoriteProduct(userId: string, productId: string) {
return this.prisma.userFavorite
.create({
Expand Down

0 comments on commit 7dfed57

Please sign in to comment.