northwind-rest-api
Version:
Local REST API Exposing Northwind Database.
137 lines (124 loc) • 4.17 kB
JavaScript
const fs = require("fs");
const locations = require("../helpers/locations");
function getAllProducts() {
return new Promise((resolve, reject) => {
fs.readFile(locations.productsJsonFile, "utf-8", (err, fileContent) => {
if (err) return reject(err);
const products = JSON.parse(fileContent);
resolve(products);
});
});
};
function saveAllProducts(products) {
return new Promise((resolve, reject) => {
const fileContent = JSON.stringify(products, null, 4);
fs.writeFile(locations.productsJsonFile, fileContent, err => {
if (err) return reject(err);
resolve();
});
});
}
function getAllEmployees() {
return new Promise((resolve, reject) => {
fs.readFile(locations.employeesJsonFile, "utf-8", (err, fileContent) => {
if (err) return reject(err);
const employees = JSON.parse(fileContent);
resolve(employees);
});
});
};
function saveAllEmployees(employees) {
return new Promise((resolve, reject) => {
const fileContent = JSON.stringify(employees, null, 4);
fs.writeFile(locations.employeesJsonFile, fileContent, err => {
if (err) return reject(err);
resolve();
});
});
}
function getAllSuppliers() {
return new Promise((resolve, reject) => {
fs.readFile(locations.suppliersJsonFile, "utf-8", (err, fileContent) => {
if (err) return reject(err);
const suppliers = JSON.parse(fileContent);
resolve(suppliers);
});
});
};
function saveAllSuppliers(suppliers) {
return new Promise((resolve, reject) => {
const fileContent = JSON.stringify(suppliers, null, 4);
fs.writeFile(locations.suppliersJsonFile, fileContent, err => {
if (err) return reject(err);
resolve();
});
});
}
function getAllCategories() {
return new Promise((resolve, reject) => {
fs.readFile(locations.categoriesJsonFile, "utf-8", (err, fileContent) => {
if (err) return reject(err);
const categories = JSON.parse(fileContent);
resolve(categories);
});
});
};
function saveAllCategories(categories) {
return new Promise((resolve, reject) => {
const fileContent = JSON.stringify(categories, null, 4);
fs.writeFile(locations.categoriesJsonFile, fileContent, err => {
if (err) return reject(err);
resolve();
});
});
}
function getAllUsers() {
return new Promise((resolve, reject) => {
fs.readFile(locations.usersJsonFile, "utf-8", (err, fileContent) => {
if (err) return reject(err);
const users = JSON.parse(fileContent);
resolve(users);
});
});
};
function saveAllUsers(users) {
return new Promise((resolve, reject) => {
const fileContent = JSON.stringify(users, null, 4);
fs.writeFile(locations.usersJsonFile, fileContent, err => {
if (err) return reject(err);
resolve();
});
});
}
function getAllContactUsMessages() {
return new Promise((resolve, reject) => {
fs.readFile(locations.contactUsJsonFile, "utf-8", (err, fileContent) => {
if (err) return reject(err);
const users = JSON.parse(fileContent);
resolve(users);
});
});
};
function saveAllContactUsMessages(users) {
return new Promise((resolve, reject) => {
const fileContent = JSON.stringify(users, null, 4);
fs.writeFile(locations.contactUsJsonFile, fileContent, err => {
if (err) return reject(err);
resolve();
});
});
}
module.exports = {
getAllProducts,
saveAllProducts,
getAllEmployees,
saveAllEmployees,
getAllSuppliers,
saveAllSuppliers,
getAllCategories,
saveAllCategories,
getAllUsers,
saveAllUsers,
getAllContactUsMessages,
saveAllContactUsMessages
};