diff --git a/src/main/java/org/ntlab/nemophila/resources/shops/ShopPostsRest.java b/src/main/java/org/ntlab/nemophila/resources/shops/ShopPostsRest.java index 695e94c..1f4a1aa 100644 --- a/src/main/java/org/ntlab/nemophila/resources/shops/ShopPostsRest.java +++ b/src/main/java/org/ntlab/nemophila/resources/shops/ShopPostsRest.java @@ -1,12 +1,16 @@ package org.ntlab.nemophila.resources.shops; +import org.ntlab.nemophila.models.accounts.Account; +import org.ntlab.nemophila.models.accounts.AccountManager; +import org.ntlab.nemophila.models.accounts.Post; +import org.ntlab.nemophila.models.shops.Shop; +import org.ntlab.nemophila.models.shops.ShopManager; import org.springframework.stereotype.Component; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; +import javax.ws.rs.*; +import javax.ws.rs.core.Response; +import java.util.ArrayList; @Component @Path("/shops") @@ -14,7 +18,52 @@ @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{sid}/posts") - public String getAccount(@PathParam("sid") String sid) { + public ArrayList getShopPosts(@PathParam("sid") String sid) { + ShopManager sm = ShopManager.getInstance(); + Shop shop = sm.getShop(sid); + + if (shop != null) { + return shop.getPosts(); + } + + return null; + } + + @PUT + @Path("{sid}/posts/{uid}/{pid}") + public void putShopPosts(@PathParam("sid") String sid, + @PathParam("uid") String uid, + @PathParam("pid") String pid, + @FormParam("token") String token){ + ShopManager sm = ShopManager.getInstance(); + Shop shop = sm.getShop(sid); + AccountManager am = AccountManager.getInstance(); + Account ac = am.getAccount(uid); + + shop.addPost(ac.getPost(pid)); + + + } + + @DELETE + @Path("{sid}/posts/{uid}/{pid}") + public String deleteShopPosts(@PathParam("sid") String sid, + @PathParam("uid") String uid, + @PathParam("pid") String pid, + @FormParam("token") String token){ + ShopManager sm = ShopManager.getInstance(); + Shop shop = sm.getShop(sid); + AccountManager am = AccountManager.getInstance(); + Account ac = am.getAccount(uid); + + shop.deletePost(ac.getPost(pid)); + + if (!shop.getPosts().isEmpty()){ + var response = Response.status(Response.Status.NO_CONTENT); + response.status(409).entity("投稿が存在しています。"); + throw new WebApplicationException(response.build()); + } + return null; } } \ No newline at end of file