|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import Tutorial from "../models/tutorial.model"; |
| 3 | +import tutorialRepository from "../repositories/tutorial.repository"; |
| 4 | + |
| 5 | +export default class TutorialController { |
| 6 | + async create(req: Request, res: Response) { |
| 7 | + if (!req.body.title) { |
| 8 | + res.status(400).send({ |
| 9 | + message: "Content can not be empty!" |
| 10 | + }); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + try { |
| 15 | + const tutorial: Tutorial = req.body; |
| 16 | + const savedTutorial = await tutorialRepository.save(tutorial); |
| 17 | + |
| 18 | + res.status(201).send(savedTutorial); |
| 19 | + } catch (err) { |
| 20 | + res.status(500).send({ |
| 21 | + message: "Some error occurred while retrieving tutorials." |
| 22 | + }); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + async findAll(req: Request, res: Response) { |
| 27 | + const title = typeof req.query.title === "string" ? req.query.title : ""; |
| 28 | + |
| 29 | + try { |
| 30 | + const tutorials = await tutorialRepository.retrieveAll({ title: title }); |
| 31 | + |
| 32 | + res.status(200).send(tutorials); |
| 33 | + } catch (err) { |
| 34 | + res.status(500).send({ |
| 35 | + message: "Some error occurred while retrieving tutorials." |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + async findOne(req: Request, res: Response) { |
| 41 | + const id: number = parseInt(req.params.id); |
| 42 | + |
| 43 | + try { |
| 44 | + const tutorial = await tutorialRepository.retrieveById(id); |
| 45 | + |
| 46 | + if (tutorial) res.status(200).send(tutorial); |
| 47 | + else |
| 48 | + res.status(404).send({ |
| 49 | + message: `Cannot find Tutorial with id=${id}.` |
| 50 | + }); |
| 51 | + } catch (err) { |
| 52 | + res.status(500).send({ |
| 53 | + message: `Error retrieving Tutorial with id=${id}.` |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + async update(req: Request, res: Response) { |
| 59 | + let tutorial: Tutorial = req.body; |
| 60 | + tutorial.id = parseInt(req.params.id); |
| 61 | + |
| 62 | + try { |
| 63 | + const num = await tutorialRepository.update(tutorial); |
| 64 | + |
| 65 | + if (num == 1) { |
| 66 | + res.send({ |
| 67 | + message: "Tutorial was updated successfully." |
| 68 | + }); |
| 69 | + } else { |
| 70 | + res.send({ |
| 71 | + message: `Cannot update Tutorial with id=${tutorial.id}. Maybe Tutorial was not found or req.body is empty!` |
| 72 | + }); |
| 73 | + } |
| 74 | + } catch (err) { |
| 75 | + res.status(500).send({ |
| 76 | + message: `Error updating Tutorial with id=${tutorial.id}.` |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async delete(req: Request, res: Response) { |
| 82 | + const id: number = parseInt(req.params.id); |
| 83 | + |
| 84 | + try { |
| 85 | + const num = await tutorialRepository.delete(id); |
| 86 | + |
| 87 | + if (num == 1) { |
| 88 | + res.send({ |
| 89 | + message: "Tutorial was deleted successfully!" |
| 90 | + }); |
| 91 | + } else { |
| 92 | + res.send({ |
| 93 | + message: `Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!`, |
| 94 | + }); |
| 95 | + } |
| 96 | + } catch (err) { |
| 97 | + res.status(500).send({ |
| 98 | + message: `Could not delete Tutorial with id==${id}.` |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + async deleteAll(req: Request, res: Response) { |
| 104 | + try { |
| 105 | + const num = await tutorialRepository.deleteAll(); |
| 106 | + |
| 107 | + res.send({ message: `${num} Tutorials were deleted successfully!` }); |
| 108 | + } catch (err) { |
| 109 | + res.status(500).send({ |
| 110 | + message: "Some error occurred while removing all tutorials." |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + async findAllPublished(req: Request, res: Response) { |
| 116 | + try { |
| 117 | + const tutorials = await tutorialRepository.retrieveAll({ published: true }); |
| 118 | + |
| 119 | + res.status(200).send(tutorials); |
| 120 | + } catch (err) { |
| 121 | + res.status(500).send({ |
| 122 | + message: "Some error occurred while retrieving tutorials." |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments