feat: add generated code
This commit is contained in:
39
backend/prisma/schema.prisma
Normal file
39
backend/prisma/schema.prisma
Normal file
@@ -0,0 +1,39 @@
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
enum EquipmentStatus {
|
||||
Active
|
||||
Repair
|
||||
Reserve
|
||||
WriteOff
|
||||
}
|
||||
|
||||
model Equipment {
|
||||
id String @id @default(uuid())
|
||||
name String
|
||||
serialNumber String
|
||||
dateOfInspection DateTime? @db.Date
|
||||
commissionedAt DateTime? @db.Date
|
||||
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 @db.Date
|
||||
responsible String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
9
backend/src/app.module.ts
Normal file
9
backend/src/app.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
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,73 @@
|
||||
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,52 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { ChangeEquipmentStatus, Prisma } from '@prisma/client';
|
||||
|
||||
@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(
|
||||
data: Prisma.ChangeEquipmentStatusCreateInput,
|
||||
): Promise<ChangeEquipmentStatus> {
|
||||
return this.prisma.changeEquipmentStatus.create({ data });
|
||||
}
|
||||
|
||||
async update(params: {
|
||||
where: Prisma.ChangeEquipmentStatusWhereUniqueInput;
|
||||
data: Prisma.ChangeEquipmentStatusUpdateInput;
|
||||
}): Promise<ChangeEquipmentStatus> {
|
||||
return this.prisma.changeEquipmentStatus.update(params);
|
||||
}
|
||||
|
||||
async remove(
|
||||
where: Prisma.ChangeEquipmentStatusWhereUniqueInput,
|
||||
): Promise<ChangeEquipmentStatus> {
|
||||
return this.prisma.changeEquipmentStatus.delete({ where });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsString,
|
||||
IsNotEmpty,
|
||||
IsEnum,
|
||||
IsISO8601,
|
||||
IsOptional,
|
||||
} from 'class-validator';
|
||||
import { EquipmentStatus } from '@prisma/client';
|
||||
|
||||
export class CreateChangeEquipmentStatusDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
equipmentId: string;
|
||||
|
||||
@ApiProperty({ enum: EquipmentStatus, enumName: 'EquipmentStatus' })
|
||||
@IsEnum(EquipmentStatus)
|
||||
newStatus: EquipmentStatus;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
number?: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsISO8601()
|
||||
@Type(() => Date)
|
||||
date: Date;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
responsible?: string;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateChangeEquipmentStatusDto } from './create-change-equipment-status.dto';
|
||||
|
||||
export class UpdateChangeEquipmentStatusDto extends PartialType(
|
||||
CreateChangeEquipmentStatusDto,
|
||||
) {}
|
||||
43
backend/src/equipment/dto/create-equipment.dto.ts
Normal file
43
backend/src/equipment/dto/create-equipment.dto.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsString,
|
||||
IsNotEmpty,
|
||||
IsEnum,
|
||||
IsISO8601,
|
||||
IsOptional,
|
||||
} from 'class-validator';
|
||||
import { EquipmentStatus } from '@prisma/client';
|
||||
|
||||
export class CreateEquipmentDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
serialNumber: string;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsISO8601()
|
||||
@Type(() => Date)
|
||||
@IsOptional()
|
||||
dateOfInspection?: Date;
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsISO8601()
|
||||
@Type(() => Date)
|
||||
@IsOptional()
|
||||
commissionedAt?: Date;
|
||||
|
||||
@ApiProperty({
|
||||
enum: EquipmentStatus,
|
||||
enumName: 'EquipmentStatus',
|
||||
required: false,
|
||||
})
|
||||
@IsEnum(EquipmentStatus)
|
||||
@IsOptional()
|
||||
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) {}
|
||||
68
backend/src/equipment/equipment.controller.ts
Normal file
68
backend/src/equipment/equipment.controller.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
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 {}
|
||||
45
backend/src/equipment/equipment.service.ts
Normal file
45
backend/src/equipment/equipment.service.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Equipment, Prisma } from '@prisma/client';
|
||||
|
||||
@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(data: Prisma.EquipmentCreateInput): Promise<Equipment> {
|
||||
return this.prisma.equipment.create({ data });
|
||||
}
|
||||
|
||||
async update(params: {
|
||||
where: Prisma.EquipmentWhereUniqueInput;
|
||||
data: Prisma.EquipmentUpdateInput;
|
||||
}): Promise<Equipment> {
|
||||
return this.prisma.equipment.update(params);
|
||||
}
|
||||
|
||||
async remove(
|
||||
where: Prisma.EquipmentWhereUniqueInput,
|
||||
): Promise<Equipment> {
|
||||
return this.prisma.equipment.delete({ where });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user