canina/backend/src/pets/pets.controller.ts

180 lines
5.3 KiB
TypeScript

import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Req, HttpStatus } from '@nestjs/common';
import { PetsService } from './pets.service';
import { CreatePetDto } from './dto/create-pet.dto';
import { UpdatePetDto } from './dto/update-pet.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse, ApiOkResponse, ApiCreatedResponse, ApiBadRequestResponse, ApiNotFoundResponse } from '@nestjs/swagger';
@ApiTags('Pets - مدیریت حیوانات خانگی')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('pets')
@ApiResponse({
status: HttpStatus.UNAUTHORIZED,
description: 'عدم احراز هویت - توکن معتبر نیست یا ارسال نشده است',
schema: {
example: {
success: false,
message: 'Unauthorized',
code: 'UNAUTHORIZED',
details: {}
}
}
})
export class PetsController {
constructor(private readonly petsService: PetsService) {}
@Post()
@ApiOperation({ summary: 'ثبت حیوان خانگی جدید' })
@ApiCreatedResponse({
description: 'حیوان خانگی جدید با موفقیت ثبت شد',
schema: {
example: {
id: 'c7b8d9e0-1234-5678-abcd-ef1234567890',
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
name: 'بادی',
type: 'سگ',
breed: 'ژرمن شپرد',
age: 3,
weight: '25.50',
activityLevel: 'متوسط',
imageUrl: null,
createdAt: '2026-05-26T18:10:00.000Z'
}
}
})
@ApiBadRequestResponse({
description: 'خطا در صحت‌سنجی فیلدهای ورودی',
schema: {
example: {
success: false,
message: 'نوع حیوان خانگی اجباری است',
code: 'BAD_REQUEST',
details: {}
}
}
})
create(@Req() req: any, @Body() createPetDto: CreatePetDto) {
return this.petsService.create(req.user.id, createPetDto);
}
@Get()
@ApiOperation({ summary: 'لیست حیوانات خانگی کاربر فعلی' })
@ApiOkResponse({
description: 'آرایه‌ای از حیوانات خانگی ثبت شده کاربر',
schema: {
example: [
{
id: 'c7b8d9e0-1234-5678-abcd-ef1234567890',
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
name: 'بادی',
type: 'سگ',
breed: 'ژرمن شپرد',
age: 3,
weight: '25.50',
activityLevel: 'متوسط',
imageUrl: null,
createdAt: '2026-05-26T18:10:00.000Z'
}
]
}
})
findAll(@Req() req: any) {
return this.petsService.findAllByUser(req.user.id);
}
@Get(':id')
@ApiOperation({ summary: 'دریافت جزئیات کامل یک حیوان خانگی' })
@ApiOkResponse({
description: 'اطلاعات کامل حیوان خانگی مشخص شده با ID',
schema: {
example: {
id: 'c7b8d9e0-1234-5678-abcd-ef1234567890',
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
name: 'بادی',
type: 'سگ',
breed: 'ژرمن شپرد',
age: 3,
weight: '25.50',
activityLevel: 'متوسط',
imageUrl: null,
medicalConditions: [],
reminders: [],
healthLogs: [],
createdAt: '2026-05-26T18:10:00.000Z'
}
}
})
@ApiNotFoundResponse({
description: 'حیوان خانگی پیدا نشد یا متعلق به کاربر جاری نیست',
schema: {
example: {
success: false,
message: 'Pet not found or unauthorized',
code: 'NOT_FOUND',
details: {}
}
}
})
findOne(@Req() req: any, @Param('id') id: string) {
return this.petsService.findOne(id, req.user.id);
}
@Patch(':id')
@ApiOperation({ summary: 'بروزرسانی اطلاعات حیوان خانگی' })
@ApiOkResponse({
description: 'اطلاعات حیوان خانگی با موفقیت به‌روزرسانی شد',
schema: {
example: {
id: 'c7b8d9e0-1234-5678-abcd-ef1234567890',
name: 'بادی قهرمان',
type: 'سگ',
breed: 'ژرمن شپرد',
age: 4,
weight: '26.00',
activityLevel: 'زیاد'
}
}
})
@ApiNotFoundResponse({
description: 'حیوان خانگی یافت نشد',
schema: {
example: {
success: false,
message: 'Pet not found',
code: 'NOT_FOUND',
details: {}
}
}
})
update(@Req() req: any, @Param('id') id: string, @Body() updatePetDto: UpdatePetDto) {
return this.petsService.update(id, req.user.id, updatePetDto);
}
@Delete(':id')
@ApiOperation({ summary: 'حذف حیوان خانگی' })
@ApiOkResponse({
description: 'حیوان خانگی با موفقیت حذف شد',
schema: {
example: {
success: true,
message: 'حیوان خانگی با موفقیت حذف شد'
}
}
})
@ApiNotFoundResponse({
description: 'حیوان خانگی یافت نشد',
schema: {
example: {
success: false,
message: 'Pet not found',
code: 'NOT_FOUND',
details: {}
}
}
})
remove(@Req() req: any, @Param('id') id: string) {
return this.petsService.remove(id, req.user.id);
}
}