Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require("express");
  2. const { Op } = require("sequelize");
  3.  
  4. const SkillTypeService = require("../../services/skillType");
  5. const NotFoundError = require("../../core/exceptions/notfound");
  6.  
  7. const SkillType = express.Router();
  8.  
  9. /**
  10.  * Retrieve skill type by id and forward it via request.
  11.  */
  12. SkillType.use("/skillTypes/:id", async (req, res, next) => {
  13.   try {
  14.     const id = req.params.id;
  15.     const skillType = await SkillTypeService.findByPk(id);
  16.  
  17.     if (!skillType) {
  18.       throw new NotFoundError("Skill type not found");
  19.     }
  20.  
  21.     req.skillType = skillType;
  22.     next();
  23.   } catch (error) {
  24.     next(error);
  25.   }
  26. });
  27.  
  28. SkillType.get("/skillTypes", async (req, res, next) => {
  29.   let options = {};
  30.  
  31.   if (req.query.name) {
  32.     options = {
  33.       where: { name: { [Op.like]: req.query.name + "%" } }
  34.     };
  35.   }
  36.  
  37.   const skillTypes = await SkillTypeService.findAll(options);
  38.  
  39.   res.json(skillTypes);
  40. });
  41.  
  42. SkillType.post("/skillTypes", async (req, res, next) => {
  43.   try {
  44.     const skillType = req.skillType;
  45.     const createdSkillType = await SkillTypeService.create(skillType);
  46.  
  47.     res.json(createdSkillType);
  48.   } catch (error) {
  49.     next(error);
  50.   }
  51. });
  52.  
  53. SkillType.get("/skillTypes/:id", (req, res, next) => {
  54.   res.json(req.skillType);
  55. });
  56.  
  57. SkillType.patch("/skillTypes/:id", async (req, res, next) => {
  58.   try {
  59.     const skillTypeUpdated = await SkillTypeService.saveById(
  60.       req.params.id,
  61.       req.body
  62.     );
  63.  
  64.     res.json(skillTypeUpdated);
  65.   } catch (error) {
  66.     next(error);
  67.   }
  68. });
  69.  
  70. SkillType.delete("/skillTypes/:id", async (req, res, next) => {
  71.   try {
  72.     const skillTypeId = req.params.id;
  73.     await SkillTypeService.delete(skillTypeId);
  74.  
  75.     res.json(req.skillType);
  76.   } catch (error) {
  77.     next(error);
  78.   }
  79. });
  80.  
  81. module.exports = SkillType;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement