refactor: made project service to use pagination
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
import { IPageFormat } from "../interfaces/IPagination";
|
||||||
|
|
||||||
|
export function buildPageFormat(
|
||||||
|
page: number,
|
||||||
|
limit: number,
|
||||||
|
totalItems: number,
|
||||||
|
baseUrl: string,
|
||||||
|
): IPageFormat {
|
||||||
|
const totalPages = Math.ceil(totalItems / limit);
|
||||||
|
|
||||||
|
const prevPage =
|
||||||
|
page > 1
|
||||||
|
? `${baseUrl}?page=${page - 1}&limit=${limit}`
|
||||||
|
: false;
|
||||||
|
|
||||||
|
const nextPage =
|
||||||
|
page < totalPages
|
||||||
|
? `${baseUrl}?page=${page + 1}&limit=${limit}`
|
||||||
|
: false;
|
||||||
|
|
||||||
|
return {
|
||||||
|
page,
|
||||||
|
limit,
|
||||||
|
totalItems,
|
||||||
|
totalPages,
|
||||||
|
prevPage,
|
||||||
|
nextPage,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import { IPageFormat } from "./IPagination";
|
||||||
|
|
||||||
|
export interface PaginatedResult<T> {
|
||||||
|
data: T[];
|
||||||
|
meta: IPageFormat;
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import { Controller, Get, Post, Patch, Delete, Param, Body, Query } from '@nestjs/common';
|
import { Controller, Get, Post, Patch, Delete, Param, Body, Query, Req } from '@nestjs/common';
|
||||||
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
|
||||||
|
import { Request } from 'express';
|
||||||
import { ProjectService } from '../providers/project.service';
|
import { ProjectService } from '../providers/project.service';
|
||||||
import { CreateProjectDto } from '../dto/project/create-project.dto';
|
import { CreateProjectDto } from '../dto/project/create-project.dto';
|
||||||
import { UpdateProjectDto } from '../dto/project/update-project.dto';
|
import { UpdateProjectDto } from '../dto/project/update-project.dto';
|
||||||
|
import { PaginationDto } from '../../../common/DTO/pagination.dto';
|
||||||
import { TMProject } from '../entities/project.entity';
|
import { TMProject } from '../entities/project.entity';
|
||||||
|
|
||||||
@ApiTags('Task Manager - Projects')
|
@ApiTags('Task Manager - Projects')
|
||||||
@@ -13,10 +15,15 @@ export class ProjectController {
|
|||||||
@Get()
|
@Get()
|
||||||
@ApiOperation({ summary: 'Get all projects, optionally filtered by workspace' })
|
@ApiOperation({ summary: 'Get all projects, optionally filtered by workspace' })
|
||||||
@ApiQuery({ name: 'workspaceId', required: false, description: 'Filter projects by workspace ID' })
|
@ApiQuery({ name: 'workspaceId', required: false, description: 'Filter projects by workspace ID' })
|
||||||
@ApiResponse({ status: 200, description: 'List of projects', type: [TMProject] })
|
@ApiResponse({ status: 200, description: 'Paginated list of projects' })
|
||||||
findAll(@Query('workspaceId') workspaceId?: string): Promise<TMProject[]> {
|
findAll(
|
||||||
if (workspaceId) return this.projectService.findByWorkspace(workspaceId);
|
@Query() pagination: PaginationDto,
|
||||||
return this.projectService.findAll();
|
@Query('workspaceId') workspaceId: string,
|
||||||
|
@Req() req: Request,
|
||||||
|
) {
|
||||||
|
const baseUrl = `${req.protocol}://${req.get('host')}${req.path}`;
|
||||||
|
if (workspaceId) return this.projectService.findByWorkspace(workspaceId, pagination, baseUrl);
|
||||||
|
return this.projectService.findAll(pagination, baseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
@@ -50,7 +57,7 @@ export class ProjectController {
|
|||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
@ApiOperation({ summary: 'Delete a project' })
|
@ApiOperation({ summary: 'Delete a project' })
|
||||||
@ApiParam({ name: 'id', description: 'Project ID' })
|
@ApiParam({ name: 'id', description: 'Project ID' })
|
||||||
@ApiResponse({ status: 200, description: 'Project deleted' })
|
@ApiResponse({ status: 200, description: 'Project deleted', type: TMProject })
|
||||||
@ApiResponse({ status: 404, description: 'Project not found' })
|
@ApiResponse({ status: 404, description: 'Project not found' })
|
||||||
remove(@Param('id') id: string): Promise<TMProject> {
|
remove(@Param('id') id: string): Promise<TMProject> {
|
||||||
return this.projectService.remove(id);
|
return this.projectService.remove(id);
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
|
import { BadRequestException, 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 { ProjectRepository } from "../repositories/project.repository";
|
import { ProjectRepository } from '../repositories/project.repository';
|
||||||
import { TMProject } from "../entities/project.entity";
|
import { WorkspaceRepository } from '../repositories/workspace.repository';
|
||||||
import { CreateProjectDto } from "../dto/project/create-project.dto";
|
import { TMProject } from '../entities/project.entity';
|
||||||
import { UpdateProjectDto } from "../dto/project/update-project.dto";
|
import { CreateProjectDto } from '../dto/project/create-project.dto';
|
||||||
import { User } from "../../users/entities/user.entity";
|
import { UpdateProjectDto } from '../dto/project/update-project.dto';
|
||||||
import { WorkspaceRepository } from "../repositories/workspace.repository";
|
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()
|
@Injectable()
|
||||||
export class ProjectService {
|
export class ProjectService {
|
||||||
@@ -17,12 +20,34 @@ export class ProjectService {
|
|||||||
private readonly userRepository: Repository<User>,
|
private readonly userRepository: Repository<User>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
findAll(): Promise<TMProject[]> {
|
async findAll(pagination: PaginationDto, baseUrl: string): Promise<PaginatedResult<TMProject>> {
|
||||||
return this.projectRepository.findAll();
|
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[]> {
|
async findByWorkspace(
|
||||||
return this.projectRepository.findByWorkspace(workspaceId);
|
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> {
|
async findOne(id: string): Promise<TMProject> {
|
||||||
@@ -46,7 +71,9 @@ export class ProjectService {
|
|||||||
const workspaceUserIds = workspace.users.map((u) => u.id);
|
const workspaceUserIds = workspace.users.map((u) => u.id);
|
||||||
const invalidUsers = userIds.filter((id) => !workspaceUserIds.includes(id));
|
const invalidUsers = userIds.filter((id) => !workspaceUserIds.includes(id));
|
||||||
if (invalidUsers.length) {
|
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) });
|
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 workspaceUserIds = workspace.users.map((u) => u.id);
|
||||||
const invalidUsers = userIds.filter((uid) => !workspaceUserIds.includes(uid));
|
const invalidUsers = userIds.filter((uid) => !workspaceUserIds.includes(uid));
|
||||||
if (invalidUsers.length) {
|
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) });
|
project.users = await this.userRepository.findBy({ id: In(userIds) });
|
||||||
} else {
|
} else {
|
||||||
@@ -86,7 +115,6 @@ export class ProjectService {
|
|||||||
async remove(id: string): Promise<TMProject> {
|
async remove(id: string): Promise<TMProject> {
|
||||||
const project = await this.findOne(id);
|
const project = await this.findOne(id);
|
||||||
await this.projectRepository.delete(id);
|
await this.projectRepository.delete(id);
|
||||||
|
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from "@nestjs/common";
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from "@nestjs/typeorm";
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Repository } from "typeorm";
|
import { Repository } from 'typeorm';
|
||||||
import { TMProject } from "../entities/project.entity";
|
import { TMProject } from '../entities/project.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProjectRepository {
|
export class ProjectRepository {
|
||||||
@@ -10,23 +10,27 @@ export class ProjectRepository {
|
|||||||
private readonly repository: Repository<TMProject>,
|
private readonly repository: Repository<TMProject>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
findAll() {
|
findAll(skip: number, take: number) {
|
||||||
return this.repository.find({
|
return this.repository.findAndCount({
|
||||||
relations: ["workspace", "taskPhases", "users"],
|
relations: ['workspace', 'taskPhases', 'users'],
|
||||||
|
skip,
|
||||||
|
take,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
findByWorkspace(workspaceId: string) {
|
findByWorkspace(workspaceId: string, skip: number, take: number) {
|
||||||
return this.repository.find({
|
return this.repository.findAndCount({
|
||||||
where: { workspaceId },
|
where: { workspaceId },
|
||||||
relations: ["taskPhases", "users"],
|
relations: ['taskPhases', 'users'],
|
||||||
|
skip,
|
||||||
|
take,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
findOneById(id: string) {
|
findOneById(id: string) {
|
||||||
return this.repository.findOne({
|
return this.repository.findOne({
|
||||||
where: { id },
|
where: { id },
|
||||||
relations: ["workspace", "taskPhases", "users"],
|
relations: ['workspace', 'taskPhases', 'users'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user