File size: 1,553 Bytes
81d1e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Response } from "express";
import { ProjectsService } from "./projects.service";
import { ApiResponse } from "../../utils/apiResponse";
import { AuthRequest } from "../../types";

const projectsService = new ProjectsService();

export class ProjectsController {
  async getAll(req: any, res: Response) {
    const result = await projectsService.getAll(req.query);
    return ApiResponse.paginated(res, result.projects, result.total, result.page, result.limit);
  }

  async getAllAdmin(req: AuthRequest, res: Response) {
    const projects = await projectsService.getAllAdmin();
    return ApiResponse.success(res, projects);
  }

  async getById(req: any, res: Response) {
    const project = await projectsService.getById(req.params.id);
    return ApiResponse.success(res, project);
  }

  async create(req: AuthRequest, res: Response) {
    const project = await projectsService.create(req.body);
    return ApiResponse.success(res, project, "Project created", 201);
  }

  async update(req: AuthRequest, res: Response) {
    const project = await projectsService.update(req.params.id, req.body);
    return ApiResponse.success(res, project, "Project updated");
  }

  async delete(req: AuthRequest, res: Response) {
    await projectsService.delete(req.params.id);
    return ApiResponse.success(res, null, "Project deleted");
  }

  async togglePublish(req: AuthRequest, res: Response) {
    const project = await projectsService.togglePublish(req.params.id);
    return ApiResponse.success(res, project, "Publish status toggled");
  }
}