full project: admin panel, backend modules, docs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { Controller, Get, Post, Delete, Param, Body, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { ProfessionsService } from './professions.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
|
||||
@ApiTags('Professions')
|
||||
@Controller('professions')
|
||||
@@ -11,4 +12,18 @@ export class ProfessionsController {
|
||||
findAll() {
|
||||
return this.professions.findAll();
|
||||
}
|
||||
|
||||
@Post()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
create(@Body() body: { name: string }) {
|
||||
return this.professions.create(body.name);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
remove(@Param('id') id: string) {
|
||||
return this.professions.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, ConflictException, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
@@ -8,4 +8,17 @@ export class ProfessionsService {
|
||||
findAll() {
|
||||
return this.prisma.professions.findMany({ orderBy: { name: 'asc' } });
|
||||
}
|
||||
|
||||
async create(name: string) {
|
||||
const existing = await this.prisma.professions.findUnique({ where: { name } });
|
||||
if (existing) throw new ConflictException('Ya existe esta profesión');
|
||||
return this.prisma.professions.create({ data: { name } });
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const prof = await this.prisma.professions.findUnique({ where: { id } });
|
||||
if (!prof) throw new NotFoundException('Profesión no encontrada');
|
||||
await this.prisma.professions.delete({ where: { id } });
|
||||
return { message: 'Profesión eliminada' };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user