refactor: isolated the repository and logic in services
This commit is contained in:
@@ -1,31 +1,25 @@
|
||||
import { Injectable, NotFoundException, ConflictException } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { WorkspaceTypeRepository } from "../repositories/workspace-type.repository";
|
||||
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>,
|
||||
) {}
|
||||
constructor(private readonly workspaceTypeRepository: WorkspaceTypeRepository) {}
|
||||
|
||||
findAll(): Promise<TMWorkspaceType[]> {
|
||||
return this.workspaceTypeRepository.find({
|
||||
order: { order: "ASC" },
|
||||
});
|
||||
return this.workspaceTypeRepository.findAll();
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMWorkspaceType> {
|
||||
const workspaceType = await this.workspaceTypeRepository.findOne({ where: { id } });
|
||||
const workspaceType = await this.workspaceTypeRepository.findOneById(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 } });
|
||||
const existing = await this.workspaceTypeRepository.findOneByName(dto.name);
|
||||
if (existing) throw new ConflictException(`WorkspaceType with name "${dto.name}" already exists`);
|
||||
|
||||
const workspaceType = this.workspaceTypeRepository.create(dto);
|
||||
@@ -36,7 +30,7 @@ export class WorkspaceTypeService {
|
||||
await this.findOne(id);
|
||||
|
||||
if (dto.name) {
|
||||
const existing = await this.workspaceTypeRepository.findOne({ where: { name: dto.name } });
|
||||
const existing = await this.workspaceTypeRepository.findOneByName(dto.name);
|
||||
if (existing && existing.id !== id) throw new ConflictException(`WorkspaceType with name "${dto.name}" already exists`);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
import { WorkspaceRepository } from '../repositories/workspace.repository';
|
||||
import { TMWorkspace } from '../entities/workspace.entity';
|
||||
import { CreateWorkspaceDto } from '../dto/workspace/create-workspace.dto';
|
||||
import { UpdateWorkspaceDto } from '../dto/workspace/update-workspace.dto';
|
||||
@@ -9,23 +10,17 @@ import { User } from '../../users/entities/user.entity';
|
||||
@Injectable()
|
||||
export class WorkspaceService {
|
||||
constructor(
|
||||
@InjectRepository(TMWorkspace)
|
||||
private readonly workspaceRepository: Repository<TMWorkspace>,
|
||||
private readonly workspaceRepository: WorkspaceRepository,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<TMWorkspace[]> {
|
||||
return this.workspaceRepository.find({
|
||||
relations: ['workspaceType', 'projects', 'users'],
|
||||
});
|
||||
return this.workspaceRepository.findAll();
|
||||
}
|
||||
|
||||
async findOneOrFail(id: string): Promise<TMWorkspace> {
|
||||
const workspace = await this.workspaceRepository.findOne({
|
||||
where: { id },
|
||||
relations: ['workspaceType', 'projects', 'users'],
|
||||
});
|
||||
const workspace = await this.workspaceRepository.findOneById(id);
|
||||
if (!workspace) throw new NotFoundException(`Workspace #${id} not found`);
|
||||
return workspace;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user