-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented deletion of comments via Datastore #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: post-comments
Are you sure you want to change the base?
Changes from all commits
dc201bc
cb8faf0
eb4fb56
cd2282e
61d7a8e
6a0d454
1219285
f056155
ec9fa7b
e8bd138
767409e
bee7148
4569f51
dbc1fa3
5fce08a
5a5a47c
947663d
88ba334
c0dfad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,25 +14,46 @@ | |
|
|
||
| package com.google.sps.servlets; | ||
|
|
||
| import com.google.appengine.api.datastore.DatastoreService; | ||
| import com.google.appengine.api.datastore.DatastoreServiceFactory; | ||
| import com.google.appengine.api.datastore.Entity; | ||
| import com.google.appengine.api.datastore.FetchOptions; | ||
| import com.google.appengine.api.datastore.Key; | ||
| import com.google.appengine.api.datastore.PreparedQuery; | ||
| import com.google.appengine.api.datastore.Query; | ||
| import com.google.appengine.api.datastore.Query.SortDirection; | ||
| import com.google.gson.Gson; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import java.util.HashMap; | ||
| // import java.util.ArrayList; | ||
| import com.google.gson.Gson; | ||
|
|
||
| /** Servlet that returns some example content. TODO: modify this file to handle comments data */ | ||
| /** Servlet that adds/retrieves comments stored in a Datastore */ | ||
| @WebServlet("/data") | ||
| public class DataServlet extends HttpServlet { | ||
| HashMap<String, String> messages = new HashMap<String, String>(); | ||
| // ArrayList<String> messages = new ArrayList<String>(); | ||
| private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
| private static final int DEFAULT_MAX_NUM_COMMENTS = 7; | ||
|
|
||
| @Override | ||
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| response.setContentType("application/json;"); | ||
|
|
||
| Map<String, String> messages = new LinkedHashMap<>(); | ||
| Query query = new Query("Comment").addSort("timestamp", SortDirection.DESCENDING); | ||
| PreparedQuery results = datastore.prepare(query); | ||
|
|
||
| int maxNumComments = (int)request.getSession().getAttribute("maxNumComments"); | ||
| for (Entity entity : results.asIterable(FetchOptions.Builder.withLimit(maxNumComments))) { | ||
| String username = (String) entity.getProperty("username"); | ||
| String message = (String) entity.getProperty("message"); | ||
| messages.put(username, message); | ||
| } | ||
|
|
||
| String json = new Gson().toJson(messages); | ||
| response.getWriter().println(json); | ||
| } | ||
|
|
@@ -41,8 +62,23 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro | |
| public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This post is doing two things now. It is used to post a comment and also to set the configuration property maxNumComments. Have you consider using a different Servlet to post configuration? Maybe it is overkilled, thoughts?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think each of the two things it's doing are relatively small though, especially setting the configuration property. For this example it's probably overkill, but in the future I'll be careful to scope the post method carefully. |
||
| String username = request.getParameter("username"); | ||
| String message = request.getParameter("comment-or-question"); | ||
| messages.put(username, message); | ||
| // messages.add(username); | ||
| String maxNumCommentsString = request.getParameter("max-num-comments"); | ||
| int maxNumComments; | ||
| try { | ||
| maxNumComments = Integer.parseInt(maxNumCommentsString); | ||
| } catch (NumberFormatException e) { | ||
| System.err.format("Could not convert to int: %s%n", maxNumCommentsString); | ||
| System.err.format("Using default value of %d.%n", DEFAULT_MAX_NUM_COMMENTS); | ||
| maxNumComments = DEFAULT_MAX_NUM_COMMENTS; | ||
| } | ||
| request.getSession().setAttribute("maxNumComments", maxNumComments); | ||
|
|
||
| Entity commentEntity = new Entity("Comment"); | ||
| commentEntity.setProperty("username", username); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constants like "Comment", "username", "message" and "timestamp" are used multiple times. Consider defining a constant for those to make it less error prone.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point, I'll consider it |
||
| commentEntity.setProperty("message", message); | ||
| commentEntity.setProperty("timestamp", System.currentTimeMillis()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One notable omission here is that the comment is never added to the datastore. Do you want to add that here?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be useful for the function of the website, so I'll add it. |
||
|
|
||
| datastore.put(commentEntity); | ||
| response.sendRedirect("/index.html"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.google.sps.servlets; | ||
|
|
||
| import com.google.appengine.api.datastore.DatastoreService; | ||
| import com.google.appengine.api.datastore.DatastoreServiceFactory; | ||
| import com.google.appengine.api.datastore.Entity; | ||
| import com.google.appengine.api.datastore.Key; | ||
| import com.google.appengine.api.datastore.PreparedQuery; | ||
| import com.google.appengine.api.datastore.Query; | ||
| import java.io.IOException; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| /** Servlet that deletes data from the database. */ | ||
| @WebServlet("/delete-messages") | ||
| public class DeleteServlet extends HttpServlet { | ||
|
|
||
| @Override | ||
| public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
| Query query = new Query("Comment").setKeysOnly(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you missing an import here for
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops I was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that there are more missing imports here. (DatastoreServiceFactory, for instance). Try invoking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a note that we'll probably want to find a common place to keep these constants (like "Comment") so that if they need to change, they will only need to be changed in one spot. No need to do anything now - just something to think about...
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea that could be a static class member later. |
||
| PreparedQuery preparedQuery = datastore.prepare(query); | ||
| Iterator<Entity> entities = preparedQuery.asIterator(); | ||
| while (entities.hasNext()) { | ||
| Key key = entities.next().getKey(); | ||
| datastore.delete(key); | ||
| } | ||
| response.sendRedirect("/index.html"); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just another note here - that the session won't have this attribute on your first page load.
no need to update now, but something to ponder for future work.