|
1 | 1 | import AppDataSource from "src/data-source";
|
2 |
| -import { NextFunction, Request, Response } from "express"; |
| 2 | +import express, { NextFunction, Request, Response } from "express"; |
3 | 3 | import Topic from "./topic.entity";
|
4 |
| -import ITopic from "./topic.interface"; |
| 4 | +import { ICreateTopic } from "./topic.interface"; |
5 | 5 |
|
6 | 6 | export default class TopicController {
|
7 |
| - async all(request: Request, response: Response, next: NextFunction) { |
8 |
| - return AppDataSource.manager.find(Topic); |
| 7 | + public path = "/topics"; |
| 8 | + public router = express.Router(); |
| 9 | + |
| 10 | + constructor() { |
| 11 | + this.initializeRoutes(); |
| 12 | + } |
| 13 | + |
| 14 | + private initializeRoutes() { |
| 15 | + this.router.get(this.path, this.getAllTopics); |
| 16 | + this.router.get(`${this.path}/:id`, this.getTopicById); |
| 17 | + this.router.post(`${this.path}`, this.createTopic); |
| 18 | + this.router.delete(`${this.path}/:id`, this.removeTopic); |
| 19 | + this.router.patch(`${this.path}/:id`, this.updateTopic); |
9 | 20 | }
|
10 | 21 |
|
11 |
| - async one(request: Request, response: Response, next: NextFunction) { |
12 |
| - return AppDataSource.manager.findOneBy(Topic, { |
13 |
| - id: Number(request.params.id), |
14 |
| - }); |
| 22 | + async getAllTopics(request: Request, response: Response, next: NextFunction) { |
| 23 | + try { |
| 24 | + const topics = await AppDataSource.manager.getRepository(Topic).find(); |
| 25 | + return response.json(topics); |
| 26 | + } catch (error) { |
| 27 | + return response.json(error); |
| 28 | + } |
15 | 29 | }
|
16 | 30 |
|
17 |
| - async save(request: Request, response: Response, next: NextFunction) { |
18 |
| - const topic: ITopic = request.body; |
19 |
| - return AppDataSource.manager.save(topic); |
| 31 | + async getTopicById(request: Request, response: Response, next: NextFunction) { |
| 32 | + const { id } = request.params; |
| 33 | + try { |
| 34 | + const topic = AppDataSource.manager.getRepository(Topic).findOneBy({ |
| 35 | + id: Number(id), |
| 36 | + }); |
| 37 | + if (!topic) { |
| 38 | + return response.json({ message: `Unable to find user with id ${id}` }); |
| 39 | + } |
| 40 | + return response.json(topic); |
| 41 | + } catch (error) { |
| 42 | + return response.json(error); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + async createTopic(request: Request, response: Response, next: NextFunction) { |
| 47 | + const topic: ICreateTopic = request.body; |
| 48 | + |
| 49 | + try { |
| 50 | + const createdTopic = AppDataSource.getRepository(Topic).create(topic); |
| 51 | + const results = await AppDataSource.getRepository(Topic).save( |
| 52 | + createdTopic |
| 53 | + ); |
| 54 | + return response.send(results); |
| 55 | + } catch (error) { |
| 56 | + return response.json(error); |
| 57 | + } |
20 | 58 | }
|
21 | 59 |
|
22 |
| - async remove(request: Request, response: Response, next: NextFunction) { |
23 |
| - let topicToRemove = await AppDataSource.manager.findOneBy(Topic, { |
24 |
| - id: Number(request.params.id), |
25 |
| - }); |
26 |
| - await AppDataSource.manager.remove(topicToRemove); |
| 60 | + async removeTopic(request: Request, response: Response, next: NextFunction) { |
| 61 | + const { id } = request.params; |
| 62 | + |
| 63 | + try { |
| 64 | + const removedUser = await AppDataSource.getRepository(Topic).delete(id); |
| 65 | + if (removedUser.affected === 0) { |
| 66 | + return response.json( |
| 67 | + `Unable to find topic with id: ${id} for deletion.` |
| 68 | + ); |
| 69 | + } |
| 70 | + return response.json({ |
| 71 | + message: `Successfully deleted records of topic with id: ${id}.`, |
| 72 | + }); |
| 73 | + } catch (error) { |
| 74 | + return response.json(error); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + async updateTopic(request: Request, response: Response, next: NextFunction) { |
| 79 | + const { id } = request.params; |
| 80 | + const update: Partial<Topic> = request.body; |
| 81 | + console.log(update); |
| 82 | + |
| 83 | + try { |
| 84 | + const topicToUpdate = await AppDataSource.getRepository(Topic).findOneBy({ |
| 85 | + id: Number(id), |
| 86 | + }); |
| 87 | + if (!topicToUpdate) { |
| 88 | + return response.json({ message: `Unable to find topic with id ${id}` }); |
| 89 | + } |
| 90 | + AppDataSource.getRepository(Topic).merge(topicToUpdate, update); |
| 91 | + const updatedUser = await AppDataSource.getRepository(Topic).save( |
| 92 | + topicToUpdate |
| 93 | + ); |
| 94 | + |
| 95 | + return response.json({ |
| 96 | + message: `Successfully updated data for topic with id: ${id}.`, |
| 97 | + updatedUser, |
| 98 | + }); |
| 99 | + } catch (error) { |
| 100 | + return response.json(error); |
| 101 | + } |
27 | 102 | }
|
28 | 103 | }
|
0 commit comments