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