File size: 1,774 Bytes
433085e
 
 
 
 
 
 
 
 
 
95ffa6b
433085e
22f0230
 
ac9dbd5
 
 
 
 
433085e
 
 
ac9dbd5
433085e
ac9dbd5
433085e
 
22f0230
 
 
433085e
 
 
 
 
 
22f0230
433085e
22f0230
 
433085e
 
 
22f0230
433085e
22f0230
 
433085e
22f0230
433085e
22f0230
 
433085e
22f0230
433085e
22f0230
 
433085e
22f0230
433085e
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Request, Response, Router } from "express";
import { BaseController } from "../../../../lib/controllers/controller.base";
import { Prefix } from "../../../common/decorators/prefix.decorator";
import { AdminsService } from "../services/admins.service";
import { createAdminSchema } from "../validations/create-admin.validation";
import {
  bodyValidator,
  paramsValidator,
} from "../../../../helpers/validation.helper";

@Prefix("/console/admins")
export class AdminsController extends BaseController {
  private adminsService = new AdminsService();

  setRoutes() {
    this.router.get("/", this.list);
    this.router.get("/:id", paramsValidator("id"), this.get);
    this.router.post("/", bodyValidator(createAdminSchema), this.create);
    this.router.patch(
      "/:id",
      paramsValidator("id"),
      bodyValidator(createAdminSchema),
      this.update
    );
    this.router.delete("/:id", paramsValidator("id"), this.delete);
  }

  list = (_, res: Response) => {
    this.adminsService
      .list({})
      .then((result) => {
        res.status(result.code).json(result);
      })
      .catch((err) => {
        res.status(500).json(err);
      });
  };

  get = async (req: Request, res: Response) => {
    const data = await this.adminsService.get({
      _id: req.params.id,
    });
    res.json(data);
  };

  create = async (req: Request, res: Response) => {
    const data = await this.adminsService.create(req.body);
    res.json(data);
  };

  update = async (req: Request, res: Response) => {
    const data = await this.adminsService.update(req.params.id, req.body);
    res.json(data);
  };

  delete = async (req: Request, res: Response) => {
    const data = await this.adminsService.remove(req.params.id);
    res.json(data);
  };
}