85 lines
3.9 KiB
TypeScript
85 lines
3.9 KiB
TypeScript
import { Controller, Get, Post, Patch, Delete, Param, Body, Query, Headers } from "@nestjs/common";
|
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from "@nestjs/swagger";
|
|
import { ProjectService } from "../providers/project.service";
|
|
import { CreateProjectDto } from "../dto/project/create-project.dto";
|
|
import { UpdateProjectDto } from "../dto/project/update-project.dto";
|
|
import { PaginationDto } from "../../../common/DTO/pagination.dto";
|
|
import { TMProject } from "../entities/project.entity";
|
|
import { AdminRoute } from "../../../common/decorators/admin.decorator";
|
|
import { PermissionsDec } from "../../../common/decorators/permission.decorator";
|
|
import { PermissionEnum } from "../../users/enums/permission.enum";
|
|
|
|
@AdminRoute()
|
|
@ApiTags("Task Manager - Projects")
|
|
@Controller("task-manager/projects")
|
|
export class ProjectController {
|
|
constructor(private readonly projectService: ProjectService) {}
|
|
|
|
@Get()
|
|
@PermissionsDec(PermissionEnum.PROJECT)
|
|
@ApiOperation({ summary: "Get all projects, optionally filtered by workspace" })
|
|
@ApiQuery({ name: "workspaceId", required: false, description: "Filter projects by workspace ID" })
|
|
@ApiResponse({ status: 200, description: "Paginated list of projects" })
|
|
findAll(
|
|
@Query() pagination: PaginationDto,
|
|
@Query("workspaceId") workspaceId: string,
|
|
@Headers("host") host: string,
|
|
@Headers("x-forwarded-proto") proto?: string,
|
|
) {
|
|
const protocol = proto ?? "http";
|
|
const baseUrl = `${protocol}://${host}/task-manager/projects`;
|
|
if (workspaceId) return this.projectService.findByWorkspace(workspaceId, pagination, baseUrl);
|
|
return this.projectService.findAll(pagination, baseUrl);
|
|
}
|
|
|
|
@Get(":id")
|
|
@PermissionsDec(PermissionEnum.PROJECT)
|
|
@ApiOperation({ summary: "Get a project by ID" })
|
|
@ApiParam({ name: "id", description: "Project ID" })
|
|
@ApiResponse({ status: 200, description: "Project found", type: TMProject })
|
|
@ApiResponse({ status: 404, description: "Project not found" })
|
|
findOne(@Param("id") id: string): Promise<TMProject> {
|
|
return this.projectService.findOneOrFail(id);
|
|
}
|
|
|
|
@Post()
|
|
@PermissionsDec(PermissionEnum.PROJECT)
|
|
@ApiOperation({ summary: "Create a new project" })
|
|
@ApiResponse({ status: 201, description: "Project created", type: TMProject })
|
|
@ApiResponse({ status: 400, description: "One or more users are not members of the workspace" })
|
|
@ApiResponse({ status: 404, description: "Workspace not found" })
|
|
create(@Body() body: CreateProjectDto): Promise<TMProject> {
|
|
return this.projectService.create(body);
|
|
}
|
|
|
|
@Patch(":id")
|
|
@PermissionsDec(PermissionEnum.PROJECT)
|
|
@ApiOperation({ summary: "Update a project" })
|
|
@ApiParam({ name: "id", description: "Project ID" })
|
|
@ApiResponse({ status: 200, description: "Project updated", type: TMProject })
|
|
@ApiResponse({ status: 400, description: "One or more users are not members of the workspace" })
|
|
@ApiResponse({ status: 404, description: "Project not found" })
|
|
update(@Param("id") id: string, @Body() body: UpdateProjectDto): Promise<TMProject> {
|
|
return this.projectService.update(id, body);
|
|
}
|
|
|
|
@Delete(":id")
|
|
@PermissionsDec(PermissionEnum.PROJECT)
|
|
@ApiOperation({ summary: "Delete a project" })
|
|
@ApiParam({ name: "id", description: "Project ID" })
|
|
@ApiResponse({ status: 200, description: "Project deleted", type: TMProject })
|
|
@ApiResponse({ status: 404, description: "Project not found" })
|
|
remove(@Param("id") id: string): Promise<TMProject> {
|
|
return this.projectService.remove(id);
|
|
}
|
|
|
|
@Get("/workspace/:wsId")
|
|
@ApiOperation({summary: "Get projects of a specific workspace"})
|
|
@ApiParam({ name: "wsId", description: "Workspace ID" })
|
|
@ApiResponse({ status: 200, description: "received projects successfully!"})
|
|
@ApiResponse({ status: 404, description: "WorkSpace not found" })
|
|
findByWorkspace(@Param("wsId") wsId: string): Promise<TMProject[]> {
|
|
return this.findByWorkspace(wsId);
|
|
}
|
|
}
|