'use strict'; const Dealer = require('../model/dealer'), { AppParamError } = require('../helpers/app_error'), assert = require('assert'); async function getDealers_get(req, res) { const dealers = await Dealer.find().sort({ country: 1, companyName: 1 }).lean(); res.json(dealers); } async function createDealer_post(req, res) { const body = req.body; assert(body && body.companyName && body.country, AppParamError.create()); delete body._id; const dealer = new Dealer(body); const saved = await dealer.save(); res.json(saved); } async function updateDealer_put(req, res) { const { id } = req.params; const body = req.body; assert(id, AppParamError.create()); const updated = await Dealer.findByIdAndUpdate(id, body, { new: true, runValidators: true }); if (!updated) AppParamError.throw(); res.json(updated); } async function deleteDealer_delete(req, res) { const { id } = req.params; assert(id, AppParamError.create()); const deleted = await Dealer.findByIdAndDelete(id); if (!deleted) AppParamError.throw(); res.json({ ok: true }); } module.exports = { getDealers_get, createDealer_post, updateDealer_put, deleteDealer_delete };