diff --git a/src/main/java/org/ntlab/amaryllis/server/resources/CommentsRest.java b/src/main/java/org/ntlab/amaryllis/server/resources/CommentsRest.java index a21bc5d..0735f08 100644 --- a/src/main/java/org/ntlab/amaryllis/server/resources/CommentsRest.java +++ b/src/main/java/org/ntlab/amaryllis/server/resources/CommentsRest.java @@ -1,4 +1,81 @@ package org.ntlab.amaryllis.server.resources; +import org.ntlab.amaryllis.server.entities.Comment; +import org.springframework.stereotype.Component; + +import javax.ws.rs.Path; + +import org.ntlab.amaryllis.server.entities.Account; +import org.ntlab.amaryllis.server.entities.Voicememo; +import org.ntlab.amaryllis.server.models.Accounts; +import org.ntlab.amaryllis.server.models.Voicememos; +import org.ntlab.amaryllis.server.utils.Base64Decode; +import org.springframework.stereotype.Component; + +import javax.ws.rs.*; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; +import java.awt.*; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import static org.ntlab.amaryllis.server.models.Voicememos.createVid; +import static org.ntlab.amaryllis.server.utils.Base64Decode.saveAsFile; + +@Component +@Path("/voicememos") public class CommentsRest { + Voicememos voicememos = Voicememos.getInstance(); + Accounts accounts = Accounts.getInstance(); + + @Path("/{vid}/comments") + @POST + @Produces(MediaType.APPLICATION_JSON) + public String addComment(@PathParam("vid") String vid, @FormParam("token") String client_token, @FormParam("uid") String uid, @FormParam("comment") String comment){ + if (vid == null || client_token == null || uid == null) { + throw new WebApplicationException(400); + } + Account account = accounts.getAccount(uid); + if(account == null){ + throw new WebApplicationException(400); + } + String server_token = account.getToken(); + if (client_token.equals(server_token)) { + Voicememo v = voicememos.getVoicememo(vid); + if(v == null){ + throw new WebApplicationException(400); + } + Comment newComment = v.createComment(uid, comment); + String commentNo = newComment.getCommentId(); + return commentNo; + } + throw new WebApplicationException(400); + } + + @Path("/{vid}/comments/{commentNo}") + @DELETE + @Produces(MediaType.APPLICATION_JSON) + public void deleteComment(@PathParam("vid") String vid, @FormParam("token") String client_token, @FormParam("uid") String uid, @PathParam("commentNo") String commentNo){ + if (vid == null || client_token == null || uid == null || commentNo == null) { + throw new WebApplicationException(400); + } + Account account = accounts.getAccount(uid); + if(account == null){ + throw new WebApplicationException(400); + } + String server_token = account.getToken(); + if (client_token.equals(server_token)) { + Voicememo v = voicememos.getVoicememo(vid); + if(v == null){ + throw new WebApplicationException(400); + } + v.removeComment(commentNo); + return; + } + throw new WebApplicationException(400); + } + }