diff --git a/src/main/java/org/ntlab/hellorest/resources/Comments.java b/src/main/java/org/ntlab/hellorest/resources/Comments.java new file mode 100644 index 0000000..d82bed4 --- /dev/null +++ b/src/main/java/org/ntlab/hellorest/resources/Comments.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") +public class Comments { + ArrayList comments = new ArrayList<>(); // 文字列の可変長リスト + + @GET + public String getComments() { // すべてのコメントを取得 + String result = ""; + for (String comment: comments) { // 拡張for文 + result = result + comment + "\n"; + } + return result; + } + + @Path("/{cId}") + @GET + public String getComment(@PathParam("cId") String cId) { // 指定したコメントを取得 + int id = Integer.parseInt(cId); + return comments.get(id); + } + + @POST + public void addComment(@FormParam("comment") String comment) { // 新規コメントを追加 + comments.add(comment); + } + + @Path("/{cId}") + @PUT + public void addComment(@PathParam("cId") String cId, @FormParam("comment") String comment) { // 指定したコメントを上書き + int id = Integer.parseInt(cId); + comments.set(id, comment); + } + + @DELETE + public void clearComments() { // すべてのコメントを削除 + comments.clear(); + } +}