food comments

This commit is contained in:
2025-12-07 11:02:55 +03:30
parent f762b03260
commit 6b7ead553a
4 changed files with 43 additions and 24 deletions
@@ -9,7 +9,6 @@ import {
ApiResponse, ApiResponse,
ApiCreatedResponse, ApiCreatedResponse,
ApiOkResponse, ApiOkResponse,
ApiNotFoundResponse,
ApiQuery, ApiQuery,
ApiBody, ApiBody,
ApiParam, ApiParam,
@@ -18,6 +17,7 @@ import {
import { AuthGuard } from 'src/modules/auth/guards/auth.guard'; import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { UserId } from 'src/common/decorators/user-id.decorator'; import { UserId } from 'src/common/decorators/user-id.decorator';
import { RestId } from 'src/common/decorators/rest-id.decorator';
@ApiTags('food-comments') @ApiTags('food-comments')
@Controller() @Controller()
@@ -34,37 +34,41 @@ export class FoodCommentController {
return this.foodCommentService.create(userId, createFoodCommentDto); return this.foodCommentService.create(userId, createFoodCommentDto);
} }
@Get('public/food-comments') @Get('public/food-comments/:foodId')
@ApiOperation({ summary: 'Get all food comments (public - only approved)' }) @ApiOperation({ summary: 'Get all food comments (public - only approved)' })
@ApiOkResponse({ description: 'List of approved food comments' }) @ApiOkResponse({ description: 'List of approved food comments' })
@ApiQuery({ name: 'page', required: false, type: Number }) @ApiQuery({ name: 'page', required: false, type: Number })
@ApiQuery({ name: 'limit', required: false, type: Number }) @ApiQuery({ name: 'limit', required: false, type: Number })
@ApiQuery({ name: 'foodId', required: false, type: String }) @ApiParam({ name: 'foodId', required: true, type: String })
@ApiQuery({ name: 'orderBy', required: false, type: String }) @ApiQuery({ name: 'orderBy', required: false, type: String })
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] }) @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
findAll(@Query() dto: FindFoodCommentsDto) { findAll(@Query() dto: FindFoodCommentsDto, @Param('foodId') foodId: string) {
// Only show approved comments for public endpoint // Only show approved comments for public endpoint
return this.foodCommentService.findAll({ ...dto, isApproved: true }); return this.foodCommentService.findAll({ ...dto, isApproved: true, foodId });
} }
@Get('public/food-comments/:id') // @Get('public/food-comments/:id')
@ApiOperation({ summary: 'Get a food comment by id' }) // @ApiOperation({ summary: 'Get a food comment by id' })
@ApiParam({ name: 'id', required: true }) // @ApiParam({ name: 'id', required: true })
@ApiOkResponse({ description: 'The food comment' }) // @ApiOkResponse({ description: 'The food comment' })
@ApiNotFoundResponse({ description: 'Comment not found' }) // @ApiNotFoundResponse({ description: 'Comment not found' })
findById(@Param('id') id: string) { // findById(@Param('id') id: string) {
return this.foodCommentService.findById(id); // return this.foodCommentService.findById(id);
} // }
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@ApiBearerAuth() @ApiBearerAuth()
@Patch('public/food-comments/:id') @Patch('public/food-comments/:foodCommentId')
@ApiOperation({ summary: 'Update a food comment (own comments only)' }) @ApiOperation({ summary: 'Update a food comment (own comments only)' })
@ApiParam({ name: 'id', required: true }) @ApiParam({ name: 'foodCommentId', required: true })
@ApiBody({ type: UpdateFoodCommentDto }) @ApiBody({ type: UpdateFoodCommentDto })
@ApiOkResponse({ description: 'The updated comment' }) @ApiOkResponse({ description: 'The updated comment' })
update(@Param('id') id: string, @Body() updateFoodCommentDto: UpdateFoodCommentDto, @UserId() userId: string) { update(
return this.foodCommentService.update(id, userId, updateFoodCommentDto, false); @Param('foodCommentId') foodCommentId: string,
@Body() updateFoodCommentDto: UpdateFoodCommentDto,
@UserId() userId: string,
) {
return this.foodCommentService.update(foodCommentId, userId, updateFoodCommentDto, false);
} }
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
@@ -89,8 +93,8 @@ export class FoodCommentController {
@ApiQuery({ name: 'isApproved', required: false, type: Boolean }) @ApiQuery({ name: 'isApproved', required: false, type: Boolean })
@ApiQuery({ name: 'orderBy', required: false, type: String }) @ApiQuery({ name: 'orderBy', required: false, type: String })
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] }) @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
findAllAdmin(@Query() dto: FindFoodCommentsDto) { findAllAdmin(@Query() dto: FindFoodCommentsDto, @RestId() restId: string) {
return this.foodCommentService.findAll(dto); return this.foodCommentService.findAll({ ...dto, restId });
} }
@UseGuards(AdminAuthGuard) @UseGuards(AdminAuthGuard)
@@ -99,7 +103,6 @@ export class FoodCommentController {
@ApiOperation({ summary: 'Update a food comment (admin - can change approval status)' }) @ApiOperation({ summary: 'Update a food comment (admin - can change approval status)' })
@ApiParam({ name: 'id', required: true }) @ApiParam({ name: 'id', required: true })
@ApiBody({ type: UpdateFoodCommentDto }) @ApiBody({ type: UpdateFoodCommentDto })
@ApiOkResponse({ description: 'The updated comment' })
updateAdmin(@Param('id') id: string, @Body() updateFoodCommentDto: UpdateFoodCommentDto, @UserId() userId: string) { updateAdmin(@Param('id') id: string, @Body() updateFoodCommentDto: UpdateFoodCommentDto, @UserId() userId: string) {
return this.foodCommentService.update(id, userId, updateFoodCommentDto, true); return this.foodCommentService.update(id, userId, updateFoodCommentDto, true);
} }
@@ -114,4 +117,3 @@ export class FoodCommentController {
return this.foodCommentService.remove(id, userId, true); return this.foodCommentService.remove(id, userId, true);
} }
} }
@@ -8,6 +8,11 @@ export class CreateFoodCommentDto {
@ApiProperty({ description: 'Food ID' }) @ApiProperty({ description: 'Food ID' })
foodId: string; foodId: string;
@IsNotEmpty()
@IsString()
@ApiProperty({ description: 'Order ID' })
orderId: string;
@IsNotEmpty() @IsNotEmpty()
@IsNumber() @IsNumber()
@Min(1) @Min(1)
@@ -20,6 +20,11 @@ export class FindFoodCommentsDto {
@ApiPropertyOptional({ description: 'Food ID to filter comments' }) @ApiPropertyOptional({ description: 'Food ID to filter comments' })
foodId?: string; foodId?: string;
@IsOptional()
@IsString()
@ApiPropertyOptional({ description: 'Restaurant ID to filter comments' })
restId?: string;
@IsOptional() @IsOptional()
@IsString() @IsString()
@ApiPropertyOptional({ description: 'User ID to filter comments' }) @ApiPropertyOptional({ description: 'User ID to filter comments' })
@@ -9,6 +9,7 @@ import { EntityManager } from '@mikro-orm/postgresql';
import { RequiredEntityData } from '@mikro-orm/core'; import { RequiredEntityData } from '@mikro-orm/core';
import { FoodComment } from '../entities/food-comment.entity'; import { FoodComment } from '../entities/food-comment.entity';
import { FoodMessage, UserMessage, FoodCommentMessage } from 'src/common/enums/message.enum'; import { FoodMessage, UserMessage, FoodCommentMessage } from 'src/common/enums/message.enum';
import { Order } from '../../orders/entities/order.entity';
@Injectable() @Injectable()
export class FoodCommentService { export class FoodCommentService {
@@ -20,7 +21,7 @@ export class FoodCommentService {
) {} ) {}
async create(userId: string, createFoodCommentDto: CreateFoodCommentDto): Promise<FoodComment> { async create(userId: string, createFoodCommentDto: CreateFoodCommentDto): Promise<FoodComment> {
const { foodId, rating, comment } = createFoodCommentDto; const { foodId, orderId, rating, comment } = createFoodCommentDto;
const food = await this.foodRepository.findOne({ id: foodId }); const food = await this.foodRepository.findOne({ id: foodId });
if (!food) { if (!food) {
@@ -32,8 +33,14 @@ export class FoodCommentService {
throw new NotFoundException(UserMessage.USER_NOT_FOUND); throw new NotFoundException(UserMessage.USER_NOT_FOUND);
} }
// Check if user already commented on this food const order = await this.em.findOne(Order, { id: orderId });
if (!order) {
throw new NotFoundException('Order not found');
}
// Check if user already commented on this food for this order
const existingComment = await this.foodCommentRepository.findOne({ const existingComment = await this.foodCommentRepository.findOne({
order: { id: orderId },
food: { id: foodId }, food: { id: foodId },
user: { id: userId }, user: { id: userId },
}); });
@@ -43,6 +50,7 @@ export class FoodCommentService {
} }
const data: RequiredEntityData<FoodComment> = { const data: RequiredEntityData<FoodComment> = {
order,
food, food,
user, user,
rating, rating,
@@ -146,4 +154,3 @@ export class FoodCommentService {
} }
} }
} }