This commit is contained in:
2025-12-07 00:03:32 +03:30
parent eb48187ad0
commit f2ae0fe020
10 changed files with 486 additions and 0 deletions
@@ -0,0 +1,117 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { FoodCommentService } from '../providers/food-comment.service';
import { CreateFoodCommentDto } from '../dto/create-food-comment.dto';
import { UpdateFoodCommentDto } from '../dto/update-food-comment.dto';
import { FindFoodCommentsDto } from '../dto/find-food-comments.dto';
import {
ApiTags,
ApiOperation,
ApiResponse,
ApiCreatedResponse,
ApiOkResponse,
ApiNotFoundResponse,
ApiQuery,
ApiBody,
ApiParam,
ApiBearerAuth,
} from '@nestjs/swagger';
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';
@ApiTags('food-comments')
@Controller()
export class FoodCommentController {
constructor(private readonly foodCommentService: FoodCommentService) {}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Post('public/food-comments')
@ApiOperation({ summary: 'Create a new food comment and rating' })
@ApiCreatedResponse({ description: 'The comment has been successfully created.' })
@ApiBody({ type: CreateFoodCommentDto })
create(@Body() createFoodCommentDto: CreateFoodCommentDto, @UserId() userId: string) {
return this.foodCommentService.create(userId, createFoodCommentDto);
}
@Get('public/food-comments')
@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 })
@ApiQuery({ name: 'orderBy', required: false, type: String })
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
findAll(@Query() dto: FindFoodCommentsDto) {
// Only show approved comments for public endpoint
return this.foodCommentService.findAll({ ...dto, isApproved: true });
}
@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')
@ApiOperation({ summary: 'Update a food comment (own comments only)' })
@ApiParam({ name: 'id', 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);
}
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Delete('public/food-comments/:id')
@ApiOperation({ summary: 'Delete a food comment (own comments only)' })
@ApiParam({ name: 'id', required: true })
@ApiResponse({ status: 200, description: 'Comment deleted' })
remove(@Param('id') id: string, @UserId() userId: string) {
return this.foodCommentService.remove(id, userId, false);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Get('admin/food-comments')
@ApiOperation({ summary: 'Get all food comments (admin - including unapproved)' })
@ApiOkResponse({ description: 'List of all food comments' })
@ApiQuery({ name: 'page', required: false, type: Number })
@ApiQuery({ name: 'limit', required: false, type: Number })
@ApiQuery({ name: 'foodId', required: false, type: String })
@ApiQuery({ name: 'userId', required: false, type: String })
@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);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Patch('admin/food-comments/:id')
@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);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Delete('admin/food-comments/:id')
@ApiOperation({ summary: 'Delete a food comment (admin)' })
@ApiParam({ name: 'id', required: true })
@ApiResponse({ status: 200, description: 'Comment deleted' })
removeAdmin(@Param('id') id: string, @UserId() userId: string) {
return this.foodCommentService.remove(id, userId, true);
}
}