Files
dmenu-api/src/modules/food-comments/controllers/food-comment.controller.ts
T
2025-12-07 11:02:55 +03:30

120 lines
5.0 KiB
TypeScript

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,
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';
import { RestId } from 'src/common/decorators/rest-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/: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 })
@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, @Param('foodId') foodId: string) {
// Only show approved comments for public endpoint
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);
// }
@UseGuards(AuthGuard)
@ApiBearerAuth()
@Patch('public/food-comments/:foodCommentId')
@ApiOperation({ summary: 'Update a food comment (own comments only)' })
@ApiParam({ name: 'foodCommentId', required: true })
@ApiBody({ type: UpdateFoodCommentDto })
@ApiOkResponse({ description: 'The updated comment' })
update(
@Param('foodCommentId') foodCommentId: string,
@Body() updateFoodCommentDto: UpdateFoodCommentDto,
@UserId() userId: string,
) {
return this.foodCommentService.update(foodCommentId, 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, @RestId() restId: string) {
return this.foodCommentService.findAll({ ...dto, restId });
}
@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 })
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);
}
}