Skip to content

Commit a8fb712

Browse files
committed
Refactot post and user controller to use services
1 parent ed64111 commit a8fb712

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

src/controllers/postController.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,57 @@
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"
45
import { Request, Response, NextFunction } from "express"
5-
import { IPostInputDTO } from "../dtos/postDTO"
6+
import { ICreatePostDTO, IUpdatePostDTO } from "../dtos/postDTO"
67

78

89
@injectable()
910
export default class PostController {
10-
public postRepo: IPostRepositoryInterface
11+
public postService: PostService
1112

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);
1418
}
1519

1620
public async index(req: Request, res: Response, next: NextFunction) {
17-
const posts = await this.postRepo.all();
21+
const posts = await this.postService.getAll();
1822
res.status(200).send(posts);
1923
}
2024

2125
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);
2327
res.status(200).send(post);
2428
}
2529

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);
2834
res.status(200).send(post);
2935
}
3036

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+
}
3446
}
3547

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+
}
3956
}
4057
}

src/controllers/userController.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@ import { TYPES } from "../types"
22
import IUserRepositoryInterface from "../repositories/interfaces/userInterface"
33
import { inject, injectable } from "inversify"
44
import { Request, Response, NextFunction } from "express"
5-
import { IPostInputDTO } from "../dtos/postDTO"
5+
import { ICreatePostDTO } from "../dtos/postDTO"
6+
import UserService from "../services/user"
7+
import { myContainer } from "../inversify.config"
68

79

810
@injectable()
911
export default class UserController {
10-
public userRepo: IUserRepositoryInterface
12+
public userService: UserService
1113

12-
constructor(@inject(TYPES.IUserRepositoryInterface) userRepo: IUserRepositoryInterface) {
13-
this.userRepo = userRepo;
14+
constructor() {
15+
this.userService = myContainer.resolve<UserService>(UserService);
1416
}
1517

1618
public async index(req: Request, res: Response, next: NextFunction) {
17-
const users = await this.userRepo.all();
19+
const users = await this.userService.getAll();
1820
res.status(200).send(users);
1921
}
2022

2123
public async show(req: Request, res: Response, next: NextFunction) {
22-
const user = await this.userRepo.find(+req.params.id);
24+
const user = await this.userService.find(+req.params.id);
2325
res.status(200).send(user);
2426
}
2527
}

0 commit comments

Comments
 (0)