Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require("express");
- const { Op } = require("sequelize");
- const SkillTypeService = require("../../services/skillType");
- const NotFoundError = require("../../core/exceptions/notfound");
- const SkillType = express.Router();
- /**
- * Retrieve skill type by id and forward it via request.
- */
- SkillType.use("/skillTypes/:id", async (req, res, next) => {
- try {
- const id = req.params.id;
- const skillType = await SkillTypeService.findByPk(id);
- if (!skillType) {
- throw new NotFoundError("Skill type not found");
- }
- req.skillType = skillType;
- next();
- } catch (error) {
- next(error);
- }
- });
- SkillType.get("/skillTypes", async (req, res, next) => {
- let options = {};
- if (req.query.name) {
- options = {
- where: { name: { [Op.like]: req.query.name + "%" } }
- };
- }
- const skillTypes = await SkillTypeService.findAll(options);
- res.json(skillTypes);
- });
- SkillType.post("/skillTypes", async (req, res, next) => {
- try {
- const skillType = req.skillType;
- const createdSkillType = await SkillTypeService.create(skillType);
- res.json(createdSkillType);
- } catch (error) {
- next(error);
- }
- });
- SkillType.get("/skillTypes/:id", (req, res, next) => {
- res.json(req.skillType);
- });
- SkillType.patch("/skillTypes/:id", async (req, res, next) => {
- try {
- const skillTypeUpdated = await SkillTypeService.saveById(
- req.params.id,
- req.body
- );
- res.json(skillTypeUpdated);
- } catch (error) {
- next(error);
- }
- });
- SkillType.delete("/skillTypes/:id", async (req, res, next) => {
- try {
- const skillTypeId = req.params.id;
- await SkillTypeService.delete(skillTypeId);
- res.json(req.skillType);
- } catch (error) {
- next(error);
- }
- });
- module.exports = SkillType;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement