Wednesday, August 20, 2014

M101J: MongoDB for Java Developers Homework 3.2

Step 1:
Download Handout

Step 2:
Problem:
we need to enhance the project and we should be able to insert the entries in post collection. changes that we need to made are marked as XXX in BlogPostDAO
.

Solution:
open BlogPostDAO.java
 
1) Go to findByPermalink method
add below line at XXX
 
post = postsCollection.findOne(new BasicDBObject("permalink", permalink));
 
 2) Go to findByDateDescending method
add below line at XXX
 
DBCursor cursor = postsCollection.find()
 .sort(new BasicDBObject().append("date", -1)).limit(limit); 
 
    //DBCursor cursor1 = postsCollection.find();
        posts = new LinkedList<DBObject>();
        for (DBObject value : cursor) {
            posts.add(value);
        }
        System.out.println("Value from the DB" + posts);

3) Go to addPost method
add below Line at XXX
 
Date now = new Date();
  List comments = new ArrayList<Object>();
  post.append("author", username).append("title", title)
    .append("body", body).append("permalink", permalink)
    .append("tags", tags).append("date", now)
    .append("comments", comments);
  postsCollection.insert(post);
4) Go to addPostComment method
add below Line at XXX
  BasicDBObject comment = new BasicDBObject().append("author", name)
    .append("body", body);
  if (email != null) {
   comment.append("email", email);
  }
  postsCollection.update(new BasicDBObject("permalink", permalink),
    new BasicDBObject("$push", new BasicDBObject("comments",
      comment)), true, false);



you require only above changes.

now re compile using mvn compile exec:java -Dexec.mainClass=course.BlogController
or run the run.sh file

login to http://localhost:8082/login

you should be able to insert post in the blog.
 
 
MongoProc 

1) now go to mongoproc and login with course ID 
 
2)go to your homework and Click on Test.

3)if you receive below feedback then everything is correct. you should click on turn in
and you score will be submitted. 







Feedback Welcome. enjoy you homework.

 
 

 


1 comment:

  1. Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback.
    北美Java代写

    ReplyDelete