[Solution] Final Exam(Project) :Mongomart Application : M101JS: MongoDB for Node.js Developers

 [Solution] Final Exam(Project) :Mongomart Application : M101JS: MongoDB for Node.js Developers


Below you will get the solution for the final Exam of m101js-MongoDB for Node.js Develper.It contains the solution of Mongomart Application(Lab0,Lab1,Lab2,Lab3,Lab4,Lab5).

Lab 0 : 


Get the MongoMart application up and running. It doesn't do much (yet), but you should be able to run it and experiment with the UI.

Download the MongoMart application from the handout.
Install the dependencies.
Make sure you have a mongod running version 3.2.x of MongoDB.
Import the "item" collection: mongoimport --drop -d mongomart -c item data/items.json
Import the "cart" collection: mongoimport --drop -d mongomart -c cart data/cart.json
Run the application by typing "node mongomart.js" in the mongomart directory.
In your browser, visit http://localhost:3000. If you've set up and launched MongoMart correctly, you should see a page that lists the same product repeated five times. Which of the following products is it?

Solution : 


Lab0_solution_final_exam_mongomart_application,visionfortech,m101p




Lab 1 : 


The items.js file implements data access for the item collection. The item collection contains the product catalog for the MongoMart application.

The mongomart.js application instantiates an item data access object (DAO) with this call:

var items = new ItemDAO(db);
The DAO, items, is used throughout mongomart.js to implement several of the routes in this application.

In this lab, you will implement the methods in items.js necessary to support the root route or "/". This route is implemented in mongomart.js in the function that begins with this line:

router.get("/", function(req, res) {

The methods you will implement in items.js are: getCategories(), getItems(), and getNumItems(). The comments in each of these methods describe in detail what you need to do to implement each method. When you believe you are finished, restart the mongomart.js application and evaluate your application to see whether it matches the following description.

If you have completed this lab correctly, the landing page for Mongomart should now show the following five products: Gray Hooded Sweatshirt, Coffee Mug, Stress Ball, Track Jacket, and Women's T-shirt. On the left side of the page you should see nine product categories listed, beginning with "All" and ending with "Umbrellas". At the bottom of the page you should see tabs for pages 1-5 with the statement "23 Products" immediately underneath.

If your application matches the above description, please answer the following question.

Select the "Apparel" category by clicking on it with your mouse. This category contains six products. MongoMart will paginate the products in this category into two pages. Which of the following items is listed on the second page? There should be only one item on this page. Please make sure you are sorting query results as specified in the Lab 1 instructions or you might get the wrong answer.

Solution : 
// Lab 1A
this.getCategories = function(callback) {
    "use strict";

    var pipeline = [
        {"$group": {_id: "$category",
                    num: {"$sum" : 1}
                   } },
        {"$sort": {_id: 1} }
    ];

    this.db.collection("item").aggregate(pipeline).toArray(function(err, categories) {
        assert.equal(null, err);

        var total = 0;
        for (var i=0; i<categories.length; i++) {
            total += categories[i].num;
        }

        categories.unshift({_id: "All", num: total});

        callback(categories);
    });
}


// Lab 1B
this.getItems = function(category, page, itemsPerPage, callback) {
    "use strict";

    var queryDoc;
    if (category == "All") {
        queryDoc = {};
    } else {
        queryDoc = {category: category};
    }

    var cursor = this.db.collection("item").find(queryDoc);
    cursor.skip(page*itemsPerPage);
    cursor.limit(itemsPerPage);
    cursor.toArray(function(err, pageItems) {
        assert.equal(null, err);
        callback(pageItems);
    });
}


// Lab 1C
this.getNumItems = function(category, callback) {
    "use strict";

    var queryDoc;
    if (category == "All") {
        queryDoc = {};
    } else {
        queryDoc = {category: category};
    }

    this.db.collection("item").find(queryDoc).count(function(err, count) {
        assert.equal(null, err);
        callback(count);
    });
}

Lab0_solution_final_exam_mongomart_application,visionfortech







Lab 2 :


In this lab, you will implement the methods in items.js necessary to support the route for text search or "/search". This route is implemented in mongomart.js in the function that begins with this line:

router.get("/search", function(req, res) {

The methods you will implement in items.js are: searchItems(), and getNumSearchItems(). The comments in each of these methods describe in detail what you need to do to implement each method. When you are finished, restart the mongomart.js application and answer the question below.

How many products match the query, "leaf"?

Solution : 
// Lab 2A
this.searchItems = function(query, page, itemsPerPage, callback) {
    "use strict";

    var queryDoc;
    if (query.trim() == "") {
        queryDoc = {};
    } else {
        queryDoc = { "$text": {"$search": query} };
    }

    var cursor = this.db.collection("item").find(queryDoc);
    cursor.skip(page*itemsPerPage);
    cursor.limit(itemsPerPage);
    cursor.toArray(function(err, pageItems) {
        assert.equal(null, err);
        callback(pageItems);
    });
}

// Lab 2B
this.getNumSearchItems = function(query, callback) {
    "use strict";

    var queryDoc;
    if (query.trim() == "") {
        queryDoc = {};
    } else {
        queryDoc = { "$text": {"$search": query} };
    }

    this.db.collection("item").find(queryDoc).count(function(err, count) {
        assert.equal(null, err);
        callback(count);
    });
}
Lab2_solution_final_exam_mongomart_application,visionfortech







Lab 3 :


In this lab, you will implement the method in items.js necessary to support the route for viewing a single item. This route is implemented in mongomart.js in the function that begins with this line:

router.get("/item/:itemId", function(req, res) {

This route is implemented using a parameter for the item as part of the url. In Express, you may define a route with url parameters by placing a ":" before each portion of the url path that should be interpreted by Express as a variable. In this case, :itemId indicates that our callback for this route expects to use the value found in this portion of the url to do its job. You may access url parameters via the params property of the request object passed as the first parameter to our route callback function as we do here:

var itemId = parseInt(req.params.itemId);

To complete the functionality to support the single item view, you will need to implement the getItem() method in items.js. The comments in this method describe what you need to do to implement it. When you are finished, restart the mongomart.js application and answer the question below.

Which of the following are true of the single item view for the Track Jacket product?

Solution : 
this.getItem = function(itemId, callback) {
    "use strict";

    this.db.collection("item").find({_id: itemId}).toArray(function(err, docs) {
        assert.equal(null, err);

        var itemDoc = null;
        if (docs.length > 0) {
            itemDoc = docs[0];
        }

        callback(itemDoc);

    });
};
Lab3_solution_final_exam_mongomart_application,visionfortech







Lab 4 :


In this lab, you will implement the method in items.js necessary to support the route for adding a review to a single item. This route is implemented in mongomart.js in the function that begins with this line:

router.post("/item/:itemId/reviews", function(req, res) {

Note that this route uses a url parameter much like that used in the route for the single item view. However, this route supports POST rather than GET requests. To access the form values passed in the POST request, we use the "body" property of the request object passed to the callback function for this route.

To complete the functionality to support adding reviews, you will need to implement the addReview() method in items.js. The comments in this method describe what you need to do to implement it.

If you have completed this problem successfully, you should be able to add reviews to products. To test your code, experiment with adding reviews to the Leaf Sticker. To add a review, complete the "Add a Review" form on any individual product's page and click "Submit Review". You should see the review appear beneath the "Latest Reviews" heading. When correctly created, reviews will contain the following information: - The reviewer name - The date and time the review was made - The number of stars provided by the reviewer - The reviewer comments

When you are satisfied that your code is working, navigate to the Track Jacket page. Unless you've modified the data in some way you should see reviews from Shannon, Bob, and Jorge only. If you see reviews other than these, please drop the item collection and mongoimport it again, following the instructions in Lab 0.

Add a five-star review with a brief comment. Type any name and review comments you wish, but make sure you select five stars for your review.

After submitting this review, under "Product Description" the Track Jacket page now shows an average of how many stars?

Solution : 
this.addReview = function(itemId, comment, name, stars, callback) {
    "use strict";

    var reviewDoc = {
        name: name,
        comment: comment,
        stars: stars,
        date: Date.now()
    }

    this.db.collection("item").updateOne(
        {_id: itemId},
        {"$push": {reviews: reviewDoc}},
        function(err, doc) {
            assert.equal(null, err);
            callback(doc);
        });
};
\Lab4_solution_final_exam_mongomart_application,visionfortech







Lab 5 :


The cart.js file implements data access for the cart collection. The cart collection contains the shopping carts for users of the MongoMart application.

The mongomart.js application instantiates a cart data access object (DAO) with this call:

var cart = new CartDAO(db);
The DAO, cart, is used throughout mongomart.js to implement several of the routes in this application.

In this lab, you will implement the method in cart.js necessary to support the route for viewing a shopping cart. This route is implemented in mongomart.js in the function that begins with this line:

router.get("/cart", function(req, res) {
The method you will implement in cart.js is: getCart(). The comments in this method describe what you need to do to implement it. When you are finished, restart mongomart.js application and answer the question below.

Click the "Cart" button in the upper right corner of the MongoMart home page. There are already several items in the user cart. Which of the following values is closest to the total value of items in the cart REPORTED ON THAT PAGE?

Solution :
this.getCart = function(userId, callback) {
    "use strict";

    // This could also be implemented using a technique similar to what we did with getItem() in Lab 3.
    this.db.collection("cart").find({userId: userId}).limit(1).next(function(err, doc) {
        assert.equal(null, err);
        assert.ok(doc);

        callback(doc);
    });
}
Lab5_solution_final_exam_mongomart_application,visionfortech
















Enjoy....!!!!

Feel free to comment below your experience with above approach and If you still find any problem  with above steps Let me know I would love to help you to resolve your  problem.

 If you want to take your Technological Knowledge to the Next Level and For More Technological information Stay tuned to Visionfortech

Related Post : 

1) [SOLUTION] WEEK 1 : INTRODUCTION : M101JS: MongoDB for Node.js Developers

2) [SOLUTION] WEEK 2 : CRUD : M101JS: MongoDB for Node.js Developers

3) [SOLUTION] Week 3 :The Node.js Driver : M101JS: MongoDB for Node.js Developers

4) [SOLUTION] WEEK 4 : SCHEMA DESIGN : M101JS: MongoDB for Node.js Developers

5) [SOLUTION] WEEK 5 : INDEXES AND PERFORMANCE : M101JS: MongoDB for Node.js Developers

6) [SOLUTION] WEEK 6 : THE AGGREGATION FRAMEWORK : MongoDB for Node.js Developers

7) [SOLUTION] WEEK 7 :APPLICATION ENGINEERING : MongoDB for Node.js Developers

8) [SOLUTION] FINAL EXAM(PROJECT) :MONGOMART APPLICATION : M101JS: MongoDB for Node.js Developers

Comments

  1. One thing you missed is in lab 1B: cursor.sort({_id:1}); should be added to get desired answer.

    ReplyDelete
    Replies
    1. Seconded, important if you're working the code for understanding.

      Delete
  2. In Lab 2, you have to create a text index on title and description fields of item collection. Use db.item.createIndex({title:"text",description:"text"}) in mongo shell

    ReplyDelete
  3. Thank You!!!! Please share how to go by this step by step cause sometimes the instructions are too hard to understand

    ReplyDelete
    Replies
    1. Please tell me which instruction you found hard to understand so that i can improve that and will modify that to easy instruction.

      Delete

Post a Comment