feat: add generated code
This commit is contained in:
38
backend/prisma/schema.prisma
Normal file
38
backend/prisma/schema.prisma
Normal file
@@ -0,0 +1,38 @@
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
enum EquipmentStatus {
|
||||
Active
|
||||
Inactive
|
||||
Repair
|
||||
}
|
||||
|
||||
model Equipment {
|
||||
id String @id @default(uuid())
|
||||
name String
|
||||
serialNumber String @unique
|
||||
dateOfInspection DateTime? @db.Date
|
||||
commissionedAt DateTime?
|
||||
status EquipmentStatus @default(Active)
|
||||
statusChanges ChangeEquipmentStatus[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model ChangeEquipmentStatus {
|
||||
id String @id @default(uuid())
|
||||
equipment Equipment @relation(fields: [equipmentId], references: [id])
|
||||
equipmentId String
|
||||
newStatus EquipmentStatus
|
||||
number String?
|
||||
date DateTime
|
||||
responsible String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
13
backend/src/app.module.ts
Normal file
13
backend/src/app.module.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { EquipmentModule } from './equipment/equipment.module';
|
||||
import { ChangeEquipmentStatusModule } from './change-equipment-status/change-equipment-status.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
AuthModule,
|
||||
EquipmentModule,
|
||||
ChangeEquipmentStatusModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { ChangeEquipmentStatusService } from './change-equipment-status.service';
|
||||
import { CreateChangeEquipmentStatusDto } from './dto/create-change-equipment-status.dto';
|
||||
import { UpdateChangeEquipmentStatusDto } from './dto/update-change-equipment-status.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
|
||||
const parseJson = <T>(value?: string): T | undefined => {
|
||||
if (!value) return undefined;
|
||||
try { return JSON.parse(value) as T; } catch { return undefined; }
|
||||
};
|
||||
|
||||
@ApiTags('change-equipment-status')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('change-equipment-status')
|
||||
export class ChangeEquipmentStatusController {
|
||||
constructor(private readonly changeEquipmentStatusService: ChangeEquipmentStatusService) {}
|
||||
|
||||
@Get()
|
||||
findAll(
|
||||
@Query('skip') skip?: string,
|
||||
@Query('take') take?: string,
|
||||
@Query('orderBy') orderBy?: string,
|
||||
@Query('where') where?: string,
|
||||
) {
|
||||
return this.changeEquipmentStatusService.findAll({
|
||||
skip: skip ? Number(skip) : 0,
|
||||
take: take ? Number(take) : 25,
|
||||
orderBy: parseJson(orderBy),
|
||||
where: parseJson(where),
|
||||
});
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.changeEquipmentStatusService.findOne({ id });
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: CreateChangeEquipmentStatusDto) {
|
||||
return this.changeEquipmentStatusService.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UpdateChangeEquipmentStatusDto) {
|
||||
return this.changeEquipmentStatusService.update({ where: { id }, data: dto });
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.changeEquipmentStatusService.remove({ id });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ChangeEquipmentStatusService } from './change-equipment-status.service';
|
||||
import { ChangeEquipmentStatusController } from './change-equipment-status.controller';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Module({
|
||||
controllers: [ChangeEquipmentStatusController],
|
||||
providers: [ChangeEquipmentStatusService, PrismaService],
|
||||
})
|
||||
export class ChangeEquipmentStatusModule {}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { ChangeEquipmentStatus, Prisma } from '@prisma/client';
|
||||
import { CreateChangeEquipmentStatusDto } from './dto/create-change-equipment-status.dto';
|
||||
import { UpdateChangeEquipmentStatusDto } from './dto/update-change-equipment-status.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ChangeEquipmentStatusService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findAll(params: {
|
||||
skip?: number;
|
||||
take?: number;
|
||||
where?: Prisma.ChangeEquipmentStatusWhereInput;
|
||||
orderBy?: Prisma.ChangeEquipmentStatusOrderByWithRelationInput;
|
||||
}): Promise<{ data: ChangeEquipmentStatus[]; total: number }> {
|
||||
const { skip, take, where, orderBy } = params;
|
||||
const [data, total] = await this.prisma.$transaction([
|
||||
this.prisma.changeEquipmentStatus.findMany({ skip, take, where, orderBy }),
|
||||
this.prisma.changeEquipmentStatus.count({ where }),
|
||||
]);
|
||||
return { data, total };
|
||||
}
|
||||
|
||||
async findOne(where: Prisma.ChangeEquipmentStatusWhereUniqueInput): Promise<ChangeEquipmentStatus | null> {
|
||||
return this.prisma.changeEquipmentStatus.findUnique({ where });
|
||||
}
|
||||
|
||||
async create(dto: CreateChangeEquipmentStatusDto): Promise<ChangeEquipmentStatus> {
|
||||
return this.prisma.changeEquipmentStatus.create({
|
||||
data: {
|
||||
equipmentId: dto.equipmentId,
|
||||
newStatus: dto.newStatus,
|
||||
number: dto.number,
|
||||
date: new Date(dto.date),
|
||||
responsible: dto.responsible,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async update(params: {
|
||||
where: Prisma.ChangeEquipmentStatusWhereUniqueInput;
|
||||
data: UpdateChangeEquipmentStatusDto;
|
||||
}): Promise<ChangeEquipmentStatus> {
|
||||
const { where, data: dto } = params;
|
||||
return this.prisma.changeEquipmentStatus.update({
|
||||
where,
|
||||
data: {
|
||||
...(dto.equipmentId !== undefined && { equipmentId: dto.equipmentId }),
|
||||
...(dto.newStatus !== undefined && { newStatus: dto.newStatus }),
|
||||
...(dto.number !== undefined && { number: dto.number }),
|
||||
...(dto.date !== undefined && { date: new Date(dto.date) }),
|
||||
...(dto.responsible !== undefined && { responsible: dto.responsible }),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async remove(where: Prisma.ChangeEquipmentStatusWhereUniqueInput): Promise<ChangeEquipmentStatus> {
|
||||
return this.prisma.changeEquipmentStatus.delete({ where });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { IsString, IsNotEmpty, IsEnum, IsOptional, IsDateString, IsUUID } from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { EquipmentStatus } from '@prisma/client';
|
||||
|
||||
export class CreateChangeEquipmentStatusDto {
|
||||
@ApiProperty()
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
equipmentId: string;
|
||||
|
||||
@ApiProperty({ enum: EquipmentStatus, enumName: 'EquipmentStatus' })
|
||||
@IsEnum(EquipmentStatus)
|
||||
newStatus: EquipmentStatus;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
number?: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsDateString({ strict: false })
|
||||
@IsNotEmpty()
|
||||
date: string;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
responsible?: string;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateChangeEquipmentStatusDto } from './create-change-equipment-status.dto';
|
||||
|
||||
export class UpdateChangeEquipmentStatusDto extends PartialType(CreateChangeEquipmentStatusDto) {}
|
||||
29
backend/src/equipment/dto/create-equipment.dto.ts
Normal file
29
backend/src/equipment/dto/create-equipment.dto.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { IsString, IsNotEmpty, IsEnum, IsOptional, IsDateString, IsUUID } from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { EquipmentStatus } from '@prisma/client';
|
||||
|
||||
export class CreateEquipmentDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
serialNumber: string;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsDateString({ strict: false })
|
||||
@IsOptional()
|
||||
dateOfInspection?: string;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsDateString({ strict: false })
|
||||
@IsOptional()
|
||||
commissionedAt?: string;
|
||||
|
||||
@ApiProperty({ enum: EquipmentStatus, enumName: 'EquipmentStatus' })
|
||||
@IsEnum(EquipmentStatus)
|
||||
status: EquipmentStatus;
|
||||
}
|
||||
4
backend/src/equipment/dto/update-equipment.dto.ts
Normal file
4
backend/src/equipment/dto/update-equipment.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateEquipmentDto } from './create-equipment.dto';
|
||||
|
||||
export class UpdateEquipmentDto extends PartialType(CreateEquipmentDto) {}
|
||||
54
backend/src/equipment/equipment.controller.ts
Normal file
54
backend/src/equipment/equipment.controller.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { EquipmentService } from './equipment.service';
|
||||
import { CreateEquipmentDto } from './dto/create-equipment.dto';
|
||||
import { UpdateEquipmentDto } from './dto/update-equipment.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
|
||||
const parseJson = <T>(value?: string): T | undefined => {
|
||||
if (!value) return undefined;
|
||||
try { return JSON.parse(value) as T; } catch { return undefined; }
|
||||
};
|
||||
|
||||
@ApiTags('equipment')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('equipment')
|
||||
export class EquipmentController {
|
||||
constructor(private readonly equipmentService: EquipmentService) {}
|
||||
|
||||
@Get()
|
||||
findAll(
|
||||
@Query('skip') skip?: string,
|
||||
@Query('take') take?: string,
|
||||
@Query('orderBy') orderBy?: string,
|
||||
@Query('where') where?: string,
|
||||
) {
|
||||
return this.equipmentService.findAll({
|
||||
skip: skip ? Number(skip) : 0,
|
||||
take: take ? Number(take) : 25,
|
||||
orderBy: parseJson(orderBy),
|
||||
where: parseJson(where),
|
||||
});
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.equipmentService.findOne({ id });
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: CreateEquipmentDto) {
|
||||
return this.equipmentService.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UpdateEquipmentDto) {
|
||||
return this.equipmentService.update({ where: { id }, data: dto });
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.equipmentService.remove({ id });
|
||||
}
|
||||
}
|
||||
10
backend/src/equipment/equipment.module.ts
Normal file
10
backend/src/equipment/equipment.module.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { EquipmentService } from './equipment.service';
|
||||
import { EquipmentController } from './equipment.controller';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Module({
|
||||
controllers: [EquipmentController],
|
||||
providers: [EquipmentService, PrismaService],
|
||||
})
|
||||
export class EquipmentModule {}
|
||||
65
backend/src/equipment/equipment.service.ts
Normal file
65
backend/src/equipment/equipment.service.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Equipment, Prisma } from '@prisma/client';
|
||||
import { CreateEquipmentDto } from './dto/create-equipment.dto';
|
||||
import { UpdateEquipmentDto } from './dto/update-equipment.dto';
|
||||
|
||||
@Injectable()
|
||||
export class EquipmentService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findAll(params: {
|
||||
skip?: number;
|
||||
take?: number;
|
||||
where?: Prisma.EquipmentWhereInput;
|
||||
orderBy?: Prisma.EquipmentOrderByWithRelationInput;
|
||||
}): Promise<{ data: Equipment[]; total: number }> {
|
||||
const { skip, take, where, orderBy } = params;
|
||||
const [data, total] = await this.prisma.$transaction([
|
||||
this.prisma.equipment.findMany({ skip, take, where, orderBy }),
|
||||
this.prisma.equipment.count({ where }),
|
||||
]);
|
||||
return { data, total };
|
||||
}
|
||||
|
||||
async findOne(where: Prisma.EquipmentWhereUniqueInput): Promise<Equipment | null> {
|
||||
return this.prisma.equipment.findUnique({ where });
|
||||
}
|
||||
|
||||
async create(dto: CreateEquipmentDto): Promise<Equipment> {
|
||||
return this.prisma.equipment.create({
|
||||
data: {
|
||||
name: dto.name,
|
||||
serialNumber: dto.serialNumber,
|
||||
status: dto.status,
|
||||
dateOfInspection: dto.dateOfInspection ? new Date(dto.dateOfInspection) : null,
|
||||
commissionedAt: dto.commissionedAt ? new Date(dto.commissionedAt) : null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async update(params: {
|
||||
where: Prisma.EquipmentWhereUniqueInput;
|
||||
data: UpdateEquipmentDto;
|
||||
}): Promise<Equipment> {
|
||||
const { where, data: dto } = params;
|
||||
return this.prisma.equipment.update({
|
||||
where,
|
||||
data: {
|
||||
...(dto.name !== undefined && { name: dto.name }),
|
||||
...(dto.serialNumber !== undefined && { serialNumber: dto.serialNumber }),
|
||||
...(dto.status !== undefined && { status: dto.status }),
|
||||
...(dto.dateOfInspection !== undefined && {
|
||||
dateOfInspection: dto.dateOfInspection ? new Date(dto.dateOfInspection) : null,
|
||||
}),
|
||||
...(dto.commissionedAt !== undefined && {
|
||||
commissionedAt: dto.commissionedAt ? new Date(dto.commissionedAt) : null,
|
||||
}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async remove(where: Prisma.EquipmentWhereUniqueInput): Promise<Equipment> {
|
||||
return this.prisma.equipment.delete({ where });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user