1
- import { TYPES } from "../types"
2
- import IPostRepositoryInterface from "../repositories/interfaces/postInterface"
3
- import { inject , injectable } from "inversify"
1
+ import PostService from "../services/post"
2
+ import PostPolicy from "../policies/Post"
3
+ import { myContainer } from "../inversify.config"
4
+ import { injectable } from "inversify"
4
5
import { Request , Response , NextFunction } from "express"
5
- import { IPostInputDTO } from "../dtos/postDTO"
6
+ import { ICreatePostDTO , IUpdatePostDTO } from "../dtos/postDTO"
6
7
7
8
8
9
@injectable ( )
9
10
export default class PostController {
10
- public postRepo : IPostRepositoryInterface
11
+ public postService : PostService
11
12
12
- constructor ( @inject ( TYPES . IPostRepositoryInterface ) postRepo : IPostRepositoryInterface ) {
13
- this . postRepo = postRepo ;
13
+ public postPolicy : PostPolicy
14
+
15
+ constructor ( ) {
16
+ this . postService = myContainer . resolve < PostService > ( PostService ) ;
17
+ this . postPolicy = myContainer . resolve < PostPolicy > ( PostPolicy ) ;
14
18
}
15
19
16
20
public async index ( req : Request , res : Response , next : NextFunction ) {
17
- const posts = await this . postRepo . all ( ) ;
21
+ const posts = await this . postService . getAll ( ) ;
18
22
res . status ( 200 ) . send ( posts ) ;
19
23
}
20
24
21
25
public async show ( req : Request , res : Response , next : NextFunction ) {
22
- const post = await this . postRepo . find ( + req . params . id ) ;
26
+ const post = await this . postService . find ( + req . params . id ) ;
23
27
res . status ( 200 ) . send ( post ) ;
24
28
}
25
29
26
- public async store ( req : Request , res : Response , next : NextFunction ) {
27
- const post = await this . postRepo . create ( req . body as IPostInputDTO )
30
+ public async store ( req , res : Response , next : NextFunction ) {
31
+ let createPostInputData = req . body ;
32
+ createPostInputData . user_id = req . user . id
33
+ const post = await this . postService . create ( createPostInputData as ICreatePostDTO ) ;
28
34
res . status ( 200 ) . send ( post ) ;
29
35
}
30
36
31
- public async update ( req : Request , res : Response , next : NextFunction ) {
32
- const post = await this . postRepo . update ( + req . params . id , req . body as IPostInputDTO )
33
- res . status ( 200 ) . send ( post ) ;
37
+ public async update ( req , res : Response , next : NextFunction ) {
38
+ try {
39
+ await this . postPolicy . update ( + req . user . id , + req . params . id )
40
+ const post = await this . postService . update ( + req . params . id , req . body as IUpdatePostDTO )
41
+ res . status ( 200 ) . send ( post ) ;
42
+ } catch ( error ) {
43
+ console . log ( `error: ${ error } ` )
44
+ res . status ( 401 ) . send ( error ) ;
45
+ }
34
46
}
35
47
36
- public async delete ( req : Request , res : Response , next : NextFunction ) {
37
- await this . postRepo . delete ( + req . params . id ) ;
38
- res . status ( 200 ) . send ( ) ;
48
+ public async delete ( req , res : Response , next : NextFunction ) {
49
+ try {
50
+ await this . postPolicy . delete ( + req . user . id , + req . params . id )
51
+ await this . postService . delete ( + req . params . id ) ;
52
+ res . status ( 200 ) . send ( ) ;
53
+ } catch ( error ) {
54
+ res . status ( 401 ) . send ( error ) ;
55
+ }
39
56
}
40
57
}
0 commit comments