xxx
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
import { Controller, Delete, Get, Param, Req, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from 'src/core/guards/auth.guard';
|
||||
import { CylinderService } from './cylinder.service';
|
||||
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
|
||||
@@ -15,7 +25,23 @@ export class CylinderController {
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
deleteKey(@Req() req: AuthenticatedRequest, @Param() id: string) {
|
||||
return this.service.deleteKey(req.user, id);
|
||||
deleteKey(@Req() req: AuthenticatedRequest, @Param('id') id: string) {
|
||||
return this.service.deleteCylinder(req.user, id);
|
||||
}
|
||||
|
||||
@Put()
|
||||
updateCylinder(
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Body() b: Partial<Cylinder>,
|
||||
) {
|
||||
return this.service.updateCylinder(req.user, b);
|
||||
}
|
||||
|
||||
@Post()
|
||||
createCylinder(
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Body() b: Partial<Cylinder>,
|
||||
) {
|
||||
return this.service.createCylinder(req.user, b);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,28 @@ export class CylinderService {
|
||||
return c;
|
||||
}
|
||||
|
||||
async deleteKey(user: User, id: string) {
|
||||
async deleteCylinder(user: User, cylinderid: string) {
|
||||
const cylinder = await this.cylinderRepo.findOneOrFail({
|
||||
where: { id: id, system: { managers: { id: user.id } } },
|
||||
where: { id: cylinderid, system: { managers: { id: user.id } } },
|
||||
relations: ['keys'],
|
||||
});
|
||||
await this.keyRepo.softRemove(cylinder.keys);
|
||||
return this.cylinderRepo.softRemove(cylinder);
|
||||
}
|
||||
|
||||
async updateCylinder(user: User, cylinder: Partial<Cylinder>) {
|
||||
const original = await this.cylinderRepo.findOneOrFail({
|
||||
where: { id: cylinder.id, system: { managers: { id: user.id } } },
|
||||
relations: ['keys', 'system'],
|
||||
});
|
||||
|
||||
Object.keys(cylinder).forEach((k: string) => {
|
||||
original[k] = cylinder[k];
|
||||
});
|
||||
return this.cylinderRepo.save(original);
|
||||
}
|
||||
|
||||
createCylinder(user: User, cylinder: Partial<Cylinder>) {
|
||||
return this.cylinderRepo.save(this.cylinderRepo.create(cylinder));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import { KeyService } from './key.service';
|
||||
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
|
||||
import { AuthGuard } from 'src/core/guards/auth.guard';
|
||||
import { Key } from 'src/model/entitites';
|
||||
import { CreateKeySystemDto } from 'src/model/dto/create-key-system.dto';
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Controller('key')
|
||||
@@ -45,14 +44,6 @@ export class KeyController {
|
||||
return this.service.deleteKey(req.user, id);
|
||||
}
|
||||
|
||||
@Post('system')
|
||||
createKeySystem(
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Body() body: CreateKeySystemDto,
|
||||
) {
|
||||
return this.service.createKeySystem(req.user, body);
|
||||
}
|
||||
|
||||
@Post(':id/handover')
|
||||
handoutKey(
|
||||
@Req() req: AuthenticatedRequest,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateKeySystemDto } from 'src/model/dto';
|
||||
import { Cylinder, Key, User } from 'src/model/entitites';
|
||||
import { IUser } from 'src/model/interface';
|
||||
import {
|
||||
@@ -68,17 +67,6 @@ export class KeyService {
|
||||
});
|
||||
}
|
||||
|
||||
async createKeySystem(user: User, systemDTO: CreateKeySystemDto) {
|
||||
const sys = this.systemRepo.create(systemDTO);
|
||||
sys.managers = [user];
|
||||
try {
|
||||
const res = await this.systemRepo.save(sys);
|
||||
return res;
|
||||
} catch (e) {
|
||||
throw new HttpException(e.code, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
}
|
||||
|
||||
async handoverKey(user: IUser, data: any, keyID: string) {
|
||||
const key: Key = await this.keyrepository.findOneOrFail({
|
||||
where: { id: keyID, cylinder: { system: { managers: { id: user.id } } } },
|
||||
|
||||
8
api/src/modules/system/dto/create-system.dto.ts
Normal file
8
api/src/modules/system/dto/create-system.dto.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { IsNotEmpty, MaxLength, MinLength } from 'class-validator';
|
||||
|
||||
export class CreateSystemDto {
|
||||
@IsNotEmpty()
|
||||
@MinLength(2)
|
||||
@MaxLength(255)
|
||||
name: string;
|
||||
}
|
||||
4
api/src/modules/system/dto/update-system.dto.ts
Normal file
4
api/src/modules/system/dto/update-system.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateSystemDto } from './create-system.dto';
|
||||
|
||||
export class UpdateSystemDto extends PartialType(CreateSystemDto) {}
|
||||
50
api/src/modules/system/system.controller.ts
Normal file
50
api/src/modules/system/system.controller.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { SystemService } from './system.service';
|
||||
import { CreateSystemDto } from './dto/create-system.dto';
|
||||
import { UpdateSystemDto } from './dto/update-system.dto';
|
||||
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
|
||||
import { AuthGuard } from 'src/core/guards/auth.guard';
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Controller('system')
|
||||
export class SystemController {
|
||||
constructor(private readonly systemService: SystemService) {}
|
||||
|
||||
@Post()
|
||||
create(
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Body() createSystemDto: CreateSystemDto,
|
||||
) {
|
||||
return this.systemService.create(req.user, createSystemDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Req() req: AuthenticatedRequest) {
|
||||
return this.systemService.findAll(req.user);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.systemService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateSystemDto: UpdateSystemDto) {
|
||||
return this.systemService.update(id, updateSystemDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.systemService.remove(id);
|
||||
}
|
||||
}
|
||||
12
api/src/modules/system/system.module.ts
Normal file
12
api/src/modules/system/system.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SystemService } from './system.service';
|
||||
import { SystemController } from './system.controller';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { DatabaseModule } from 'src/shared/database/database.module';
|
||||
|
||||
@Module({
|
||||
controllers: [SystemController],
|
||||
providers: [SystemService],
|
||||
imports: [AuthModule, DatabaseModule],
|
||||
})
|
||||
export class SystemModule {}
|
||||
18
api/src/modules/system/system.service.spec.ts
Normal file
18
api/src/modules/system/system.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { SystemService } from './system.service';
|
||||
|
||||
describe('SystemService', () => {
|
||||
let service: SystemService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [SystemService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<SystemService>(SystemService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
42
api/src/modules/system/system.service.ts
Normal file
42
api/src/modules/system/system.service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateSystemDto } from './dto/create-system.dto';
|
||||
import { UpdateSystemDto } from './dto/update-system.dto';
|
||||
import { KeySystemRepository } from 'src/model/repositories';
|
||||
import { User } from 'src/model/entitites';
|
||||
|
||||
@Injectable()
|
||||
export class SystemService {
|
||||
constructor(private systemRepo: KeySystemRepository) {}
|
||||
|
||||
async create(user: User, createSystemDto: CreateSystemDto) {
|
||||
const sys = this.systemRepo.create(createSystemDto);
|
||||
sys.managers = [user];
|
||||
try {
|
||||
const res = await this.systemRepo.save(sys);
|
||||
return res;
|
||||
} catch (e) {
|
||||
throw new HttpException(e.code, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
}
|
||||
|
||||
findAll(user: User) {
|
||||
return this.systemRepo.find({
|
||||
where: { managers: { id: user.id } },
|
||||
order: { name: { direction: 'ASC' } },
|
||||
});
|
||||
}
|
||||
|
||||
findOne(id: string) {
|
||||
return `This action returns a #${id} system`;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
update(id: string, updateSystemDto: UpdateSystemDto) {
|
||||
return `This action updates a #${id} system`;
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const system = await this.systemRepo.findOne({ where: { id } });
|
||||
return this.systemRepo.softRemove(system);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user