feat: add workspace-type service
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { Injectable, NotFoundException, ConflictException } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { TMWorkspaceType } from "../entities/workspace-type.entity";
|
||||
import { CreateWorkspaceTypeDto } from "../dto/workspace-type/create-workspace-type.dto";
|
||||
import { UpdateWorkspaceTypeDto } from "../dto/workspace-type/update-workspace-type.dto";
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceTypeService {
|
||||
constructor(
|
||||
@InjectRepository(TMWorkspaceType)
|
||||
private readonly workspaceTypeRepository: Repository<TMWorkspaceType>,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<TMWorkspaceType[]> {
|
||||
return this.workspaceTypeRepository.find({
|
||||
order: { order: "ASC" },
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMWorkspaceType> {
|
||||
const workspaceType = await this.workspaceTypeRepository.findOne({ where: { id } });
|
||||
if (!workspaceType) throw new NotFoundException(`WorkspaceType #${id} not found`);
|
||||
return workspaceType;
|
||||
}
|
||||
|
||||
async create(dto: CreateWorkspaceTypeDto): Promise<TMWorkspaceType> {
|
||||
const existing = await this.workspaceTypeRepository.findOne({ where: { name: dto.name } });
|
||||
if (existing) throw new ConflictException(`WorkspaceType with name "${dto.name}" already exists`);
|
||||
|
||||
const workspaceType = this.workspaceTypeRepository.create(dto);
|
||||
return this.workspaceTypeRepository.save(workspaceType);
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateWorkspaceTypeDto): Promise<TMWorkspaceType> {
|
||||
await this.findOne(id);
|
||||
|
||||
if (dto.name) {
|
||||
const existing = await this.workspaceTypeRepository.findOne({ where: { name: dto.name } });
|
||||
if (existing && existing.id !== id) throw new ConflictException(`WorkspaceType with name "${dto.name}" already exists`);
|
||||
}
|
||||
|
||||
await this.workspaceTypeRepository.update(id, dto);
|
||||
return this.findOne(id);
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.workspaceTypeRepository.delete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user