1 package usecase.vote;
2
3 import common.http.ParameterConverter;
4 import common.http.error.JSONError;
5 import common.http.interceptor.InterceptableServlet;
6 import usecase.auth.AuthorizationConstraints;
7
8 import javax.inject.Inject;
9 import javax.servlet.ServletException;
10 import javax.servlet.annotation.WebServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import java.io.IOException;
14
15 import static usecase.auth.AuthorizationConstraints.Types.REQUIRE_AUTHENTICATION;
16
17
18
19
20 @WebServlet("/unvote")
21 @AuthorizationConstraints(REQUIRE_AUTHENTICATION)
22 class UnvoteServlet extends InterceptableServlet {
23 @Inject private VoteService service;
24
25 @Override
26 @JSONError
27 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
28 ParameterConverter converter = new ParameterConverter(request);
29 int id = converter.getIntParameter("id").orElse(0);
30 String type = request.getParameter("type");
31
32 if("post".equalsIgnoreCase(type)){
33 service.unvotePost(id);
34 }else if("comment".equalsIgnoreCase(type)){
35 service.unvoteComment(id);
36 } else {
37 throw new IllegalArgumentException();
38 }
39 }
40
41 @Override
42 @JSONError
43 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
44 doGet(request,response);
45 }
46 }