-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostController.ts
More file actions
57 lines (48 loc) · 1.94 KB
/
postController.ts
File metadata and controls
57 lines (48 loc) · 1.94 KB
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
import PostService from "../services/post"
import PostPolicy from "../policies/Post"
import { myContainer } from "../inversify.config"
import { injectable } from "inversify"
import { Request, Response, NextFunction } from "express"
import { ICreatePostDTO, IUpdatePostDTO } from "../dtos/postDTO"
@injectable()
export default class PostController {
public postService: PostService
public postPolicy: PostPolicy
constructor() {
this.postService = myContainer.resolve<PostService>(PostService);
this.postPolicy = myContainer.resolve<PostPolicy>(PostPolicy);
}
public async index(req: Request, res: Response, next: NextFunction) {
const posts = await this.postService.getAll();
res.status(200).send(posts);
}
public async show(req: Request, res: Response, next: NextFunction) {
const post = await this.postService.find(+req.params.id);
res.status(200).send(post);
}
public async store(req, res: Response, next: NextFunction) {
let createPostInputData = req.body;
createPostInputData.user_id = req.user.id
const post = await this.postService.create(createPostInputData as ICreatePostDTO);
res.status(200).send(post);
}
public async update(req, res: Response, next: NextFunction) {
try {
await this.postPolicy.update(+req.user.id, +req.params.id)
const post = await this.postService.update(+req.params.id, req.body as IUpdatePostDTO)
res.status(200).send(post);
} catch (error) {
console.log(`error: ${error}`)
res.status(401).send(error);
}
}
public async delete(req, res: Response, next: NextFunction) {
try {
await this.postPolicy.delete(+req.user.id, +req.params.id)
await this.postService.delete(+req.params.id);
res.status(200).send();
} catch (error) {
res.status(401).send(error);
}
}
}