feat: add generated code
This commit is contained in:
40
backend/prisma/schema.prisma
Normal file
40
backend/prisma/schema.prisma
Normal file
@@ -0,0 +1,40 @@
|
||||
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 @unique
|
||||
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?
|
||||
|
||||
@@unique([equipmentId, newStatus])
|
||||
@@index([equipmentId])
|
||||
}
|
||||
15
backend/src/app.module.ts
Normal file
15
backend/src/app.module.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { PrismaService } from './prisma/prisma.service';
|
||||
import { EquipmentModule } from './equipment/equipment.module';
|
||||
import { ChangeEquipmentStatusModule } from './change-equipment-status/change-equipment-status.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
AuthModule,
|
||||
EquipmentModule,
|
||||
ChangeEquipmentStatusModule,
|
||||
],
|
||||
providers: [PrismaService],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,82 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags, ApiQuery, ApiParam } 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';
|
||||
import { EquipmentStatus } from '@prisma/client';
|
||||
|
||||
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()
|
||||
@ApiQuery({ name: 'skip', required: false, type: String })
|
||||
@ApiQuery({ name: 'take', required: false, type: String })
|
||||
@ApiQuery({ name: 'orderBy', required: false, type: String })
|
||||
@ApiQuery({ name: 'where', required: false, type: String })
|
||||
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(':equipmentId/:newStatus')
|
||||
@ApiParam({ name: 'equipmentId', type: String })
|
||||
@ApiParam({ name: 'newStatus', enum: EquipmentStatus, enumName: 'EquipmentStatus' })
|
||||
findOne(
|
||||
@Param('equipmentId') equipmentId: string,
|
||||
@Param('newStatus') newStatus: EquipmentStatus,
|
||||
) {
|
||||
return this.changeEquipmentStatusService.findOne({
|
||||
equipmentId_newStatus: { equipmentId, newStatus }
|
||||
});
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: CreateChangeEquipmentStatusDto) {
|
||||
return this.changeEquipmentStatusService.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':equipmentId/:newStatus')
|
||||
@ApiParam({ name: 'equipmentId', type: String })
|
||||
@ApiParam({ name: 'newStatus', enum: EquipmentStatus, enumName: 'EquipmentStatus' })
|
||||
update(
|
||||
@Param('equipmentId') equipmentId: string,
|
||||
@Param('newStatus') newStatus: EquipmentStatus,
|
||||
@Body() dto: UpdateChangeEquipmentStatusDto,
|
||||
) {
|
||||
return this.changeEquipmentStatusService.update({
|
||||
where: { equipmentId_newStatus: { equipmentId, newStatus } },
|
||||
data: dto
|
||||
});
|
||||
}
|
||||
|
||||
@Delete(':equipmentId/:newStatus')
|
||||
@ApiParam({ name: 'equipmentId', type: String })
|
||||
@ApiParam({ name: 'newStatus', enum: EquipmentStatus, enumName: 'EquipmentStatus' })
|
||||
remove(
|
||||
@Param('equipmentId') equipmentId: string,
|
||||
@Param('newStatus') newStatus: EquipmentStatus,
|
||||
) {
|
||||
return this.changeEquipmentStatusService.remove({
|
||||
equipmentId_newStatus: { equipmentId, newStatus }
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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,41 @@
|
||||
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 { Type } from 'class-transformer';
|
||||
import { IsString, IsNotEmpty, IsEnum, IsISO8601, IsOptional } from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { EquipmentStatus } from '@prisma/client';
|
||||
|
||||
export class CreateChangeEquipmentStatusDto {
|
||||
@ApiProperty({ description: 'Оборудование' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
equipmentId: string;
|
||||
|
||||
@ApiProperty({
|
||||
enum: EquipmentStatus,
|
||||
enumName: 'EquipmentStatus',
|
||||
description: 'Новый статус'
|
||||
})
|
||||
@IsEnum(EquipmentStatus)
|
||||
@IsNotEmpty()
|
||||
newStatus: EquipmentStatus;
|
||||
|
||||
@ApiProperty({ description: 'Номер', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
number?: string | null;
|
||||
|
||||
@ApiProperty({ description: 'Дата изменения статуса' })
|
||||
@IsISO8601()
|
||||
@Type(() => Date)
|
||||
@IsNotEmpty()
|
||||
date: Date;
|
||||
|
||||
@ApiProperty({ description: 'Ответственный', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
responsible?: string | null;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateChangeEquipmentStatusDto } from './create-change-equipment-status.dto';
|
||||
|
||||
export class UpdateChangeEquipmentStatusDto extends PartialType(CreateChangeEquipmentStatusDto) {}
|
||||
38
backend/src/equipment/dto/create-equipment.dto.ts
Normal file
38
backend/src/equipment/dto/create-equipment.dto.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsString, IsNotEmpty, IsEnum, IsISO8601, IsOptional } from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { EquipmentStatus } from '@prisma/client';
|
||||
|
||||
export class CreateEquipmentDto {
|
||||
@ApiProperty({ description: 'Название оборудования' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty({ description: 'Заводской (серийный) номер' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
serialNumber: string;
|
||||
|
||||
@ApiProperty({ description: 'Дата поверки', required: false })
|
||||
@IsISO8601()
|
||||
@Type(() => Date)
|
||||
@IsOptional()
|
||||
dateOfInspection?: Date | null;
|
||||
|
||||
@ApiProperty({ description: 'Дата изготовления', required: false })
|
||||
@IsISO8601()
|
||||
@Type(() => Date)
|
||||
@IsOptional()
|
||||
commissionedAt?: Date | null;
|
||||
|
||||
@ApiProperty({
|
||||
enum: EquipmentStatus,
|
||||
enumName: 'EquipmentStatus',
|
||||
description: 'Текущий статус',
|
||||
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) {}
|
||||
61
backend/src/equipment/equipment.controller.ts
Normal file
61
backend/src/equipment/equipment.controller.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags, ApiQuery, ApiParam } 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()
|
||||
@ApiQuery({ name: 'skip', required: false, type: String })
|
||||
@ApiQuery({ name: 'take', required: false, type: String })
|
||||
@ApiQuery({ name: 'orderBy', required: false, type: String })
|
||||
@ApiQuery({ name: 'where', required: false, type: String })
|
||||
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')
|
||||
@ApiParam({ name: 'id', type: String })
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.equipmentService.findOne({ id });
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: CreateEquipmentDto) {
|
||||
return this.equipmentService.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiParam({ name: 'id', type: String })
|
||||
update(@Param('id') id: string, @Body() dto: UpdateEquipmentDto) {
|
||||
return this.equipmentService.update({ where: { id }, data: dto });
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiParam({ name: 'id', type: String })
|
||||
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 {}
|
||||
41
backend/src/equipment/equipment.service.ts
Normal file
41
backend/src/equipment/equipment.service.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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