feat(constructeurs): introduce constructors management
This commit is contained in:
33
src/constructeurs/constructeurs.controller.ts
Normal file
33
src/constructeurs/constructeurs.controller.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Controller, Get, Post, Patch, Delete, Param, Body, Query } from '@nestjs/common'
|
||||
import { ConstructeursService } from './constructeurs.service'
|
||||
import { CreateConstructeurDto, UpdateConstructeurDto } from '../shared/dto/constructeur.dto'
|
||||
|
||||
@Controller('constructeurs')
|
||||
export class ConstructeursController {
|
||||
constructor(private readonly constructeursService: ConstructeursService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() payload: CreateConstructeurDto) {
|
||||
return this.constructeursService.create(payload)
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Query('search') search?: string) {
|
||||
return this.constructeursService.findAll(search)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.constructeursService.findOne(id)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() payload: UpdateConstructeurDto) {
|
||||
return this.constructeursService.update(id, payload)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.constructeursService.remove(id)
|
||||
}
|
||||
}
|
||||
10
src/constructeurs/constructeurs.module.ts
Normal file
10
src/constructeurs/constructeurs.module.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { ConstructeursService } from './constructeurs.service'
|
||||
import { ConstructeursController } from './constructeurs.controller'
|
||||
|
||||
@Module({
|
||||
controllers: [ConstructeursController],
|
||||
providers: [ConstructeursService],
|
||||
exports: [ConstructeursService],
|
||||
})
|
||||
export class ConstructeursModule {}
|
||||
64
src/constructeurs/constructeurs.service.ts
Normal file
64
src/constructeurs/constructeurs.service.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { PrismaService } from '../prisma/prisma.service'
|
||||
import { CreateConstructeurDto, UpdateConstructeurDto } from '../shared/dto/constructeur.dto'
|
||||
import { Prisma } from '@prisma/client'
|
||||
|
||||
@Injectable()
|
||||
export class ConstructeursService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private buildSearchWhere(search?: string): Prisma.ConstructeurWhereInput {
|
||||
if (!search) return {}
|
||||
const term = search.trim()
|
||||
if (!term) return {}
|
||||
return {
|
||||
OR: [
|
||||
{ name: { contains: term, mode: 'insensitive' } },
|
||||
{ email: { contains: term, mode: 'insensitive' } },
|
||||
{ phone: { contains: term, mode: 'insensitive' } },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
async create(data: CreateConstructeurDto) {
|
||||
return this.prisma.constructeur.create({
|
||||
data: {
|
||||
...data,
|
||||
name: data.name.trim(),
|
||||
email: data.email?.trim(),
|
||||
phone: data.phone?.trim(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async findAll(search?: string) {
|
||||
return this.prisma.constructeur.findMany({
|
||||
where: this.buildSearchWhere(search),
|
||||
orderBy: { name: 'asc' },
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
return this.prisma.constructeur.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateConstructeurDto) {
|
||||
return this.prisma.constructeur.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...data,
|
||||
name: data.name?.trim(),
|
||||
email: data.email?.trim(),
|
||||
phone: data.phone?.trim(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
return this.prisma.constructeur.delete({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user