|
| 1 | +import{Request,Response}from"express"; |
| 2 | +importTutorialfrom"../models/tutorial.model"; |
| 3 | +importtutorialRepositoryfrom"../repositories/tutorial.repository"; |
| 4 | + |
| 5 | +exportdefaultclassTutorialController{ |
| 6 | +asynccreate(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 | +consttutorial:Tutorial=req.body; |
| 16 | +if(!tutorial.published)tutorial.published=false; |
| 17 | + |
| 18 | +constsavedTutorial=awaittutorialRepository.save(tutorial); |
| 19 | + |
| 20 | +res.status(201).send(savedTutorial); |
| 21 | +}catch(err){ |
| 22 | +res.status(500).send({ |
| 23 | +message:"Some error occurred while retrieving tutorials." |
| 24 | +}); |
| 25 | +} |
| 26 | +} |
| 27 | + |
| 28 | +asyncfindAll(req:Request,res:Response){ |
| 29 | +consttitle=typeofreq.query.title==="string" ?req.query.title :""; |
| 30 | + |
| 31 | +try{ |
| 32 | +consttutorials=awaittutorialRepository.retrieveAll({ title}); |
| 33 | + |
| 34 | +res.status(200).send(tutorials); |
| 35 | +}catch(err){ |
| 36 | +res.status(500).send({ |
| 37 | +message:"Some error occurred while retrieving tutorials." |
| 38 | +}); |
| 39 | +} |
| 40 | +} |
| 41 | + |
| 42 | +asyncfindOne(req:Request,res:Response){ |
| 43 | +constid:number=parseInt(req.params.id); |
| 44 | + |
| 45 | +try{ |
| 46 | +consttutorial=awaittutorialRepository.retrieveById(id); |
| 47 | + |
| 48 | +if(tutorial)res.status(200).send(tutorial); |
| 49 | +else |
| 50 | +res.status(404).send({ |
| 51 | +message:`Cannot find Tutorial with id=${id}.` |
| 52 | +}); |
| 53 | +}catch(err){ |
| 54 | +res.status(500).send({ |
| 55 | +message:`Error retrieving Tutorial with id=${id}.` |
| 56 | +}); |
| 57 | +} |
| 58 | +} |
| 59 | + |
| 60 | +asyncupdate(req:Request,res:Response){ |
| 61 | +lettutorial:Tutorial=req.body; |
| 62 | +tutorial.id=parseInt(req.params.id); |
| 63 | + |
| 64 | +try{ |
| 65 | +constnum=awaittutorialRepository.update(tutorial); |
| 66 | + |
| 67 | +if(num==1){ |
| 68 | +res.send({ |
| 69 | +message:"Tutorial was updated successfully." |
| 70 | +}); |
| 71 | +}else{ |
| 72 | +res.send({ |
| 73 | +message:`Cannot update Tutorial with id=${tutorial.id}. Maybe Tutorial was not found or req.body is empty!` |
| 74 | +}); |
| 75 | +} |
| 76 | +}catch(err){ |
| 77 | +res.status(500).send({ |
| 78 | +message:`Error updating Tutorial with id=${tutorial.id}.` |
| 79 | +}); |
| 80 | +} |
| 81 | +} |
| 82 | + |
| 83 | +asyncdelete(req:Request,res:Response){ |
| 84 | +constid:number=parseInt(req.params.id); |
| 85 | + |
| 86 | +try{ |
| 87 | +constnum=awaittutorialRepository.delete(id); |
| 88 | + |
| 89 | +if(num==1){ |
| 90 | +res.send({ |
| 91 | +message:"Tutorial was deleted successfully!" |
| 92 | +}); |
| 93 | +}else{ |
| 94 | +res.send({ |
| 95 | +message:`Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!`, |
| 96 | +}); |
| 97 | +} |
| 98 | +}catch(err){ |
| 99 | +res.status(500).send({ |
| 100 | +message:`Could not delete Tutorial with id==${id}.` |
| 101 | +}); |
| 102 | +} |
| 103 | +} |
| 104 | + |
| 105 | +asyncdeleteAll(req:Request,res:Response){ |
| 106 | +try{ |
| 107 | +constnum=awaittutorialRepository.deleteAll(); |
| 108 | + |
| 109 | +res.send({message:`${num} Tutorials were deleted successfully!`}); |
| 110 | +}catch(err){ |
| 111 | +res.status(500).send({ |
| 112 | +message:"Some error occurred while removing all tutorials." |
| 113 | +}); |
| 114 | +} |
| 115 | +} |
| 116 | + |
| 117 | +asyncfindAllPublished(req:Request,res:Response){ |
| 118 | +try{ |
| 119 | +consttutorials=awaittutorialRepository.retrieveAll({published:true}); |
| 120 | + |
| 121 | +res.status(200).send(tutorials); |
| 122 | +}catch(err){ |
| 123 | +res.status(500).send({ |
| 124 | +message:"Some error occurred while retrieving tutorials." |
| 125 | +}); |
| 126 | +} |
| 127 | +} |
| 128 | +} |