@Override public Response createUser(User user) { // if username does not exist (need to figure out how to check if a user exists) if (!userHashMap.containsKey(user.getUsername())) { userHashMap.put(user.getUsername(), user); return Response.status(201) .link("/services/resources/user/" + user.getUsername(), "resource") .build(); } else return Response.status(409).build(); }
public final void initialiseContent() { // TO DO: // (Re)-initialise data structures so that the Web service's state is // the same same as when the Web service was initially created. BasicConfigurator.configure(); _idCounter = new AtomicLong(0); userHashMap.put("Bertmern", new User("Bertmern", "Brerce", "Werne")); userHashMap.put("Spodermern", new User("Spodermern", "Terby", "Mergwer")); // set up a test blog entry BlogEntry testBlogEntry = new BlogEntry("Here is item 0 in blogEntryMap"); testBlogEntry.setId(_idCounter.getAndIncrement()); testBlogEntry.setTimePosted(DateTime.now()); User testBlogCreator = userHashMap.get("Spodermern"); testBlogCreator.addBlogEntry(testBlogEntry); Comment testComment = new Comment("I'm a test comment", DateTime.now()); testBlogEntry.addComment(testComment); blogEntryMap.put(0l, testBlogEntry); // to prove sets of blog entries, make another blog entry BlogEntry testBlogEntry2 = new BlogEntry("Here is item 1 - second item - pls werk"); testBlogEntry2.setId(_idCounter.getAndIncrement()); testBlogEntry2.setTimePosted(DateTime.now()); User testBlogCreator2 = userHashMap.get("Bertmern"); testBlogCreator2.addBlogEntry(testBlogEntry2); Comment testComment2 = new Comment("Plz comment moar", DateTime.now()); testBlogEntry2.addComment(testComment2); blogEntryMap.put(1l, testBlogEntry2); // Set up loads of posts for Bertmern to check BlogSet BlogEntry blogEntry1 = new BlogEntry("1 post"); BlogEntry blogEntry2 = new BlogEntry("2 post"); BlogEntry blogEntry3 = new BlogEntry("3 post"); BlogEntry blogEntry4 = new BlogEntry("blue post"); createBlogEntry(blogEntry1, "Bertmern"); createBlogEntry(blogEntry2, "Bertmern"); createBlogEntry(blogEntry3, "Bertmern"); createBlogEntry(blogEntry4, "Bertmern"); }
@Override public Response createBlogEntry(BlogEntry blogEntry, String username) { if (userHashMap.containsKey(username)) { User blogAuthor = userHashMap.get(username); // then user exists and request should succeed blogEntry.setId(_idCounter.getAndIncrement()); blogEntry.setTimePosted(DateTime.now()); blogAuthor.addBlogEntry(blogEntry); blogEntryMap.put(blogEntry.getId(), blogEntry); return Response.status(201) .link("/services/resources/blog/" + blogEntry.getId(), "resource") .build(); } else { // user does not exist and request should fail return Response.status(412).build(); } }