UNPKG

northwind-rest-api

Version:

Local REST API Exposing 'Northwind Traders' Database.

42 lines (35 loc) 1.25 kB
const express = require("express"); const ContactUsMsg = require("../models/contact-us"); const contactUsLogic = require("../business-logic/contact-us-logic"); const verifyAdmin = require("../middleware/verify-admin"); const router = express.Router(); // Add contact-us message: router.post("/contact-us/", async (request, response) => { try { // Data: const contactUsMsg = new ContactUsMsg(request.body); // Validation: const errors = contactUsMsg.validate(); if (errors) return response.status(400).send(errors); // Logic: const addedContactUsMsg = await contactUsLogic.addContactUsMsg(contactUsMsg); // Success: response.status(201).json(addedContactUsMsg); } catch (err) { response.status(500).send(err.message); } }); // Get all contact-us messages: router.get("/contact-us", verifyAdmin, async (request, response) => { try { // Logic: const contactUsMessages = await contactUsLogic.getAllContactUsMessages(); // Success: response.json(contactUsMessages); } catch (err) { response.status(500).send(err.message); } }); module.exports = router;