From 9a8c064b1a822129152b0dcf4203946967600a79 Mon Sep 17 00:00:00 2001 From: "moandji.ezana" Date: Sun, 21 Oct 2012 02:21:39 +0200 Subject: [PATCH] Author can delete an essay --- .../tent/essayist/EssayServlet.java | 32 ++++++++++++++++++- .../tent/essayist/partials/EssayLink.jamon | 10 +++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/moandjiezana/tent/essayist/EssayServlet.java b/src/main/java/com/moandjiezana/tent/essayist/EssayServlet.java index 80239d6..5ad5a0e 100644 --- a/src/main/java/com/moandjiezana/tent/essayist/EssayServlet.java +++ b/src/main/java/com/moandjiezana/tent/essayist/EssayServlet.java @@ -1,5 +1,6 @@ package com.moandjiezana.tent.essayist; +import com.google.common.base.Throwables; import com.moandjiezana.tent.client.TentClient; import com.moandjiezana.tent.client.posts.Post; import com.moandjiezana.tent.client.users.Profile; @@ -8,6 +9,7 @@ import com.moandjiezana.tent.essayist.tent.Entities; import java.io.IOException; import javax.inject.Inject; +import javax.inject.Provider; import javax.inject.Singleton; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -19,10 +21,12 @@ public class EssayServlet extends HttpServlet { private Templates templates; private Users users; + private Provider sessions; @Inject - public EssayServlet(Users users, Templates templates) { + public EssayServlet(Users users, Provider sessions, Templates templates) { this.users = users; + this.sessions = sessions; this.templates = templates; } @@ -49,4 +53,30 @@ public class EssayServlet extends HttpServlet { templates.essay().render(resp.getWriter(), post, user.getProfile()); } + + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String[] parts = req.getPathInfo().split("/essay/"); + String authorEntity = parts[0]; + String fullAuthorEntity = Entities.expandFromUrl(authorEntity); + + User user = sessions.get().getUser(); + + if (!fullAuthorEntity.equals(user.getProfile().getCore().getEntity())) { + resp.sendError(HttpServletResponse.SC_FORBIDDEN, "You are not permitted to delete this Essay."); + return; + } + + TentClient tentClient = new TentClient(user.getProfile()); + tentClient.getAsync().setAccessToken(user.getAccessToken()); + tentClient.getAsync().setRegistrationResponse(user.getRegistration()); + + try { + tentClient.getAsync().deletePost(parts[1]).get(); + } catch (Exception e) { + Throwables.propagate(Throwables.getRootCause(e)); + } + + resp.sendRedirect(req.getContextPath() + "/" + authorEntity + "/essays"); + } } diff --git a/src/main/templates/com/moandjiezana/tent/essayist/partials/EssayLink.jamon b/src/main/templates/com/moandjiezana/tent/essayist/partials/EssayLink.jamon index fc13cdd..175247d 100644 --- a/src/main/templates/com/moandjiezana/tent/essayist/partials/EssayLink.jamon +++ b/src/main/templates/com/moandjiezana/tent/essayist/partials/EssayLink.jamon @@ -24,7 +24,15 @@ final EssayContent essayContent = essay.getContentAs(EssayContent.class);

<% essayContent.getTitle() %>

- <%if profile != null %><% profile.getBasic() != null ? profile.getBasic().getName() : essay.getEntity() %> <% dateFormat.format(new Date(essay.getPublishedAt() * 1000)) %> + <%if profile != null %><% Entities.getName(profile, essay.getEntity()) %> <% dateFormat.format(new Date(essay.getPublishedAt() * 1000)) %>

<% essayContent.getExcerpt() %>

+ <%if jamonContext.getCurrentUser().owns(essay) %> +
+
+ + +
+
+