Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is building a node.js module that is designed to track inventoru for a stor

ID: 3806577 • Letter: T

Question

This is building a node.js module that is designed to track inventoru for a store. This is JavaScript.

I need the inventory that exposes the following functionality:

addItem - A function that takes 2 parameters: an item's name and price and tracks it internally.

items - A function that returns a list of items, sorted by price from least expensive to most expensive.

getMostExpensive - Returns an object that contains the item's name and price for the most expensive item that has been added.

getLeastExpensive - Returns an object that contains the item's name and price for the most least expensive item that has been added.

removeItem - Remove an item by name.

getItemByName - Returns an item's name and price by name.

getItemByPrice - Returns an item's name and price by price.

The module should not expose any functions or bariables that would allow the above functions to have the integrityof their data compromised.

Explanation / Answer

/*
   Copyright 2017 ramachandrajr <https://github.com/ramachandrajr>

   Permission is hereby granted, free of charge, to any person obtaining a copy of this software
   and associated documentation files (the "Software"), to deal in the Software without restriction,
   including without limitation the rights to use, copy, modify, merge, publish, distribute,
   sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all copies or
   substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
   BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


// please do not publish this module, code is for local use. To make it a usable
// public module it takes many days of work. I am not going to be using objects
// it will make code too long but usually oop is the way to go in real world modules.

const mongoose = require("mongoose");

// Some mongoose config i.e., the url need to be pulled in from the user but we'll
// be using a local variable for now for the purpose of simplicity.
const URL = "mongodb://localhost/shop_db";
mongoose.connect(URL);

const itemSchema = mongoose.Schema({
   name: String,
   price: Number
});

const Item = mongoose.model("Item", itemSchema);


/**
* Adds an item to the database.
*/
let addItem = function(name, price) {
   return new Promise(function(resolve, reject) {
       Item.create({
           name: name,
           price: price
       }, function(err, createdItem) {
           if (err)
               reject(err);
           resolve(createdItem);
       });
   });
};

/**
* Finds all items.
*/
let items = function() {
   return new Promise(function(resolve, reject) {
       Item.find({}, function(err, foundItems) {
           if (err)
               reject(err);
           resolve(foundItems);
       });
   });
};

/**
* Get's most expensive
*/
let getMostExpensive = function() {
   return new Promise(function(resolve, reject) {
       Item.findOne({}, {
           sort: {
               // Descending price sort.
               price: -1
           }
       }, function(err, foundItem) {
           if (err)
               reject(err);
           resolve(foundItem);
       })
   });
};

/**
* Get's least expensive
*/
let getLeastExpensive = function() {
   return new Promise(function(resolve, reject) {
       Item.findOne({}, {
           sort: {
               // Ascending price sort.
               price: 1
           }
       }, function(err, foundItem) {
           if (err)
               reject(err);
           resolve(foundItem);
       })
   });
};

/**
* Remove an item.
* ! Warning: This is a really bad way of doing it. You may want to find a
* way of storing mongoose id someplace on your web application to pass in
* to this function.
*/
let removeItem = function(name) {
   // I am containing this function to delete just one, but if mongooseid
   // is supplied it would have been so much easier.
   Item.findOneAndRemove({ name : name }, function(err) {
       if (err)
           reject(err);
       // Just in case if you'd like to use this on the outside.
       resolve(true);
   });
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote