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,
ApiCreatedResponse,
ApiOkResponse,
ApiNotFoundResponse,
ApiQuery,
ApiBody,
ApiParam,
@@ -18,6 +17,7 @@ import {
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { UserId } from 'src/common/decorators/user-id.decorator';
import { RestId } from 'src/common/decorators/rest-id.decorator';
@ApiTags('food-comments')
@Controller()
@@ -34,37 +34,41 @@ export class FoodCommentController {
return this.foodCommentService.create(userId, createFoodCommentDto);
}
@Get('public/food-comments')
@Get('public/food-comments/:foodId')
@ApiOperation({ summary: 'Get all food comments (public - only approved)' })
@ApiOkResponse({ description: 'List of approved food comments' })
@ApiQuery({ name: 'page', 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: '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
return this.foodCommentService.findAll({ ...dto, isApproved: true });
return this.foodCommentService.findAll({ ...dto, isApproved: true, foodId });
}
@Get('public/food-comments/:id')
@ApiOperation({ summary: 'Get a food comment by id' })
@ApiParam({ name: 'id', required: true })
@ApiOkResponse({ description: 'The food comment' })
@ApiNotFoundResponse({ description: 'Comment not found' })
findById(@Param('id') id: string) {
return this.foodCommentService.findById(id);
}
// @Get('public/food-comments/:id')
// @ApiOperation({ summary: 'Get a food comment by id' })
// @ApiParam({ name: 'id', required: true })
// @ApiOkResponse({ description: 'The food comment' })
// @ApiNotFoundResponse({ description: 'Comment not found' })
// findById(@Param('id') id: string) {
// return this.foodCommentService.findById(id);
// }
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Patch('public/food-comments/:id')
@Patch('public/food-comments/:foodCommentId')
@ApiOperation({ summary: 'Update a food comment (own comments only)' })
@ApiParam({ name: 'id', required: true })
@ApiParam({ name: 'foodCommentId', required: true })
@ApiBody({ type: UpdateFoodCommentDto })
@ApiOkResponse({ description: 'The updated comment' })
update(@Param('id') id: string, @Body() updateFoodCommentDto: UpdateFoodCommentDto, @UserId() userId: string) {
return this.foodCommentService.update(id, userId, updateFoodCommentDto, false);
update(
@Param('foodCommentId') foodCommentId: string,
@Body() updateFoodCommentDto: UpdateFoodCommentDto,
@UserId() userId: string,
) {
return this.foodCommentService.update(foodCommentId, userId, updateFoodCommentDto, false);
}
@UseGuards(AuthGuard)
@@ -89,8 +93,8 @@ export class FoodCommentController {
@ApiQuery({ name: 'isApproved', required: false, type: Boolean })
@ApiQuery({ name: 'orderBy', required: false, type: String })
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
findAllAdmin(@Query() dto: FindFoodCommentsDto) {
return this.foodCommentService.findAll(dto);
findAllAdmin(@Query() dto: FindFoodCommentsDto, @RestId() restId: string) {
return this.foodCommentService.findAll({ ...dto, restId });
}
@UseGuards(AdminAuthGuard)
@@ -99,7 +103,6 @@ export class FoodCommentController {
@ApiOperation({ summary: 'Update a food comment (admin - can change approval status)' })
@ApiParam({ name: 'id', required: true })
@ApiBody({ type: UpdateFoodCommentDto })
@ApiOkResponse({ description: 'The updated comment' })
updateAdmin(@Param('id') id: string, @Body() updateFoodCommentDto: UpdateFoodCommentDto, @UserId() userId: string) {
return this.foodCommentService.update(id, userId, updateFoodCommentDto, true);
}
@@ -114,4 +117,3 @@ export class FoodCommentController {
return this.foodCommentService.remove(id, userId, true);
}
}
@@ -8,6 +8,11 @@ export class CreateFoodCommentDto {
@ApiProperty({ description: 'Food ID' })
foodId: string;
@IsNotEmpty()
@IsString()
@ApiProperty({ description: 'Order ID' })
orderId: string;
@IsNotEmpty()
@IsNumber()
@Min(1)
@@ -20,6 +20,11 @@ export class FindFoodCommentsDto {
@ApiPropertyOptional({ description: 'Food ID to filter comments' })
foodId?: string;
@IsOptional()
@IsString()
@ApiPropertyOptional({ description: 'Restaurant ID to filter comments' })
restId?: string;
@IsOptional()
@IsString()
@ApiPropertyOptional({ description: 'User ID to filter comments' })
@@ -9,6 +9,7 @@ import { EntityManager } from '@mikro-orm/postgresql';
import { RequiredEntityData } from '@mikro-orm/core';
import { FoodComment } from '../entities/food-comment.entity';
import { FoodMessage, UserMessage, FoodCommentMessage } from 'src/common/enums/message.enum';
import { Order } from '../../orders/entities/order.entity';
@Injectable()
export class FoodCommentService {
@@ -20,7 +21,7 @@ export class FoodCommentService {
) {}
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 });
if (!food) {
@@ -32,8 +33,14 @@ export class FoodCommentService {
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({
order: { id: orderId },
food: { id: foodId },
user: { id: userId },
});
@@ -43,6 +50,7 @@ export class FoodCommentService {
}
const data: RequiredEntityData<FoodComment> = {
order,
food,
user,
rating,
@@ -146,4 +154,3 @@ export class FoodCommentService {
}
}
}