refactor: made project service to use pagination
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { ProjectRepository } from "../repositories/project.repository";
|
||||
import { TMProject } from "../entities/project.entity";
|
||||
import { CreateProjectDto } from "../dto/project/create-project.dto";
|
||||
import { UpdateProjectDto } from "../dto/project/update-project.dto";
|
||||
import { User } from "../../users/entities/user.entity";
|
||||
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
import { ProjectRepository } from '../repositories/project.repository';
|
||||
import { WorkspaceRepository } from '../repositories/workspace.repository';
|
||||
import { TMProject } from '../entities/project.entity';
|
||||
import { CreateProjectDto } from '../dto/project/create-project.dto';
|
||||
import { UpdateProjectDto } from '../dto/project/update-project.dto';
|
||||
import { PaginationDto } from '../../../common/DTO/pagination.dto';
|
||||
import { PaginatedResult } from '../../../common/interfaces/paginated-result.interface';
|
||||
import { buildPageFormat } from '../../../common/helpers/pagination.helper';
|
||||
import { User } from '../../users/entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ProjectService {
|
||||
@@ -17,12 +20,34 @@ export class ProjectService {
|
||||
private readonly userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
findAll(): Promise<TMProject[]> {
|
||||
return this.projectRepository.findAll();
|
||||
async findAll(pagination: PaginationDto, baseUrl: string): Promise<PaginatedResult<TMProject>> {
|
||||
const page = pagination.page ?? 1;
|
||||
const limit = pagination.limit ?? 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [data, totalItems] = await this.projectRepository.findAll(skip, limit);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: buildPageFormat(page, limit, totalItems, baseUrl),
|
||||
};
|
||||
}
|
||||
|
||||
findByWorkspace(workspaceId: string): Promise<TMProject[]> {
|
||||
return this.projectRepository.findByWorkspace(workspaceId);
|
||||
async findByWorkspace(
|
||||
workspaceId: string,
|
||||
pagination: PaginationDto,
|
||||
baseUrl: string,
|
||||
): Promise<PaginatedResult<TMProject>> {
|
||||
const page = pagination.page ?? 1;
|
||||
const limit = pagination.limit ?? 10;
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [data, totalItems] = await this.projectRepository.findByWorkspace(workspaceId, skip, limit);
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: buildPageFormat(page, limit, totalItems, baseUrl),
|
||||
};
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<TMProject> {
|
||||
@@ -46,7 +71,9 @@ export class ProjectService {
|
||||
const workspaceUserIds = workspace.users.map((u) => u.id);
|
||||
const invalidUsers = userIds.filter((id) => !workspaceUserIds.includes(id));
|
||||
if (invalidUsers.length) {
|
||||
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
|
||||
throw new BadRequestException(
|
||||
`Users [${invalidUsers.join(', ')}] are not members of this workspace`,
|
||||
);
|
||||
}
|
||||
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||
}
|
||||
@@ -72,7 +99,9 @@ export class ProjectService {
|
||||
const workspaceUserIds = workspace.users.map((u) => u.id);
|
||||
const invalidUsers = userIds.filter((uid) => !workspaceUserIds.includes(uid));
|
||||
if (invalidUsers.length) {
|
||||
throw new BadRequestException(`Users [${invalidUsers.join(", ")}] are not members of this workspace`);
|
||||
throw new BadRequestException(
|
||||
`Users [${invalidUsers.join(', ')}] are not members of this workspace`,
|
||||
);
|
||||
}
|
||||
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||
} else {
|
||||
@@ -86,7 +115,6 @@ export class ProjectService {
|
||||
async remove(id: string): Promise<TMProject> {
|
||||
const project = await this.findOne(id);
|
||||
await this.projectRepository.delete(id);
|
||||
|
||||
return project;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user