|
| 1 | +const { where } = require('sequelize/types'); |
| 2 | +const type = require('../models/type'); |
| 3 | +const enginesRouter = require('../router/enginesRouter'); |
| 4 | +const { types } = require('./../models') |
1 | 5 |
|
| 6 | +class Types { |
| 7 | + static getAll = async (req, res, next) => { |
| 8 | + try { |
| 9 | + let data = await types.findAll() |
| 10 | + res.status(200).json({ |
| 11 | + data |
| 12 | + }) |
| 13 | + } catch (error) { |
| 14 | + next({ |
| 15 | + code: 500, message: error.message |
| 16 | + }) |
| 17 | + }; |
| 18 | + }; |
| 19 | + static getDetail = async (req, res, next) => { |
| 20 | + try { |
| 21 | + let data = await types.findByPk(req.params.id) |
| 22 | + |
| 23 | + if(!data) { |
| 24 | + next({code: 404, message: 'Types Not Found, try search id'}) |
| 25 | + } else { |
| 26 | + res.status(200).json({ |
| 27 | + data |
| 28 | + }) |
| 29 | + }; |
| 30 | + } catch (error) { |
| 31 | + next({ |
| 32 | + code: 500, message: error.message |
| 33 | + }) |
| 34 | + } |
| 35 | + }; |
| 36 | + static postTypes = async (req, res, next) =>{ |
| 37 | + try { |
| 38 | + let { name, foundedYear, foundedCountry } = req.body; |
| 39 | + |
| 40 | + if (!name || !foundedYear || !foundedCountry) { |
| 41 | + next({ |
| 42 | + code: 400, message: 'post invalid' |
| 43 | + }) |
| 44 | + } else { |
| 45 | + let data = await enginesRouter.create({name, foundedYear, foundedCountry}) |
| 46 | + res.status(201).json({ |
| 47 | + data |
| 48 | + }) |
| 49 | + } |
| 50 | + } catch (error) { |
| 51 | + next({code: 500, message: error.message}) |
| 52 | + }; |
| 53 | + }; |
| 54 | + static patchType = async (req, res, next) => { |
| 55 | + try { |
| 56 | + let { name, foundedYear, foundedCountry } = req.body; |
| 57 | + let {id} = req.params; |
| 58 | + const data = await types.update({name, foundedYear, foundedCountry}, { |
| 59 | + where: {id} |
| 60 | + }); |
| 61 | + res.status(201).json({data}) |
| 62 | + } catch (error) { |
| 63 | + next({ |
| 64 | + code: 500, message: error.message |
| 65 | + }) |
| 66 | + } |
| 67 | + }; |
| 68 | + static deleteType = async (req, res, next) => { |
| 69 | + try { |
| 70 | + let {id} = req.params; |
| 71 | + const data = await type.findByPk(id) |
| 72 | + data.destroy() |
| 73 | + res.sendStatus(204); |
| 74 | + } catch (error) { |
| 75 | + next({ |
| 76 | + code: 500, message: error.message |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
2 | 81 |
|
| 82 | +module.exports = Types; |
0 commit comments