Author can delete an essay

This commit is contained in:
moandji.ezana 2012-10-21 02:21:39 +02:00
parent ac7eba9e06
commit 9a8c064b1a
2 changed files with 40 additions and 2 deletions

View file

@ -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<EssayistSession> sessions;
@Inject
public EssayServlet(Users users, Templates templates) {
public EssayServlet(Users users, Provider<EssayistSession> 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");
}
}

View file

@ -24,7 +24,15 @@ final EssayContent essayContent = essay.getContentAs(EssayContent.class);
</%if>
<div class="span10">
<h3><a href="<% jamonContext.contextPath %>/<% Entities.getForUrl(essay.getEntity()) %>/essay/<% essay.getId() %>"><% essayContent.getTitle() %></a></h3>
<%if profile != null %><a href="<% jamonContext.contextPath %>/<% Entities.getForUrl(essay.getEntity()) %>/essays"><% profile.getBasic() != null ? profile.getBasic().getName() : essay.getEntity() %></a></%if> <% dateFormat.format(new Date(essay.getPublishedAt() * 1000)) %>
<%if profile != null %><a href="<% jamonContext.contextPath %>/<% Entities.getForUrl(essay.getEntity()) %>/essays"><% Entities.getName(profile, essay.getEntity()) %></a></%if> <% dateFormat.format(new Date(essay.getPublishedAt() * 1000)) %>
<p class="bodyText"><% essayContent.getExcerpt() %></p>
<%if jamonContext.getCurrentUser().owns(essay) %>
<div>
<form action="<% jamonContext.contextPath %>/<% Entities.getForUrl(essay.getEntity()) %>/essay/<% essay.getId() %>" method="post">
<button class="btn btn-danger"><i class="icon-trash icon-white"></i> Delete</button>
<input type="hidden" name="_method" value="DELETE" />
</form>
</div>
</%if>
</div>
</div>