diff --git a/src/main/java/org/ntlab/hellorest/resources/CommentsHorinouchiRest.java b/src/main/java/org/ntlab/hellorest/resources/CommentsHorinouchiRest.java new file mode 100644 index 0000000..7cabb32 --- /dev/null +++ b/src/main/java/org/ntlab/hellorest/resources/CommentsHorinouchiRest.java @@ -0,0 +1,45 @@ +package org.ntlab.hellorest.resources; + +import org.springframework.stereotype.Component; + +import javax.ws.rs.*; +import java.util.ArrayList; + +@Component +@Path("/comments-horinouchi") +public class CommentsHorinouchiRest { + ArrayList comments = new ArrayList<>(); // 文字列の可変長リスト + + @GET + public String getComments() { // すべてのコメントを取得 + String result = ""; + for (String comment: comments) { // 拡張for文 + result = result + comment + "
"; + } + return result; + } + + @POST + public void addComment(@FormParam("comment-horinouchi") String comment) { // 新規コメントを追加 + comments.add(comment); + } + + @DELETE + public void clearComments() { // すべてのコメントを削除 + comments.clear(); + } + + @Path("/{cId}") + @GET + public String getComment(@PathParam("cId") String cId) { // 指定したコメントを取得 + int id = Integer.parseInt(cId); + return comments.get(id); + } + + @Path("/{cId}") + @PUT + public void addComment(@PathParam("cId") String cId, @FormParam("comment-horinouchi") String comment) { // 指定したコメントを上書き + int id = Integer.parseInt(cId); + comments.set(id, comment); + } +} \ No newline at end of file