package org.ntlab.citrusserver.resources; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import org.ntlab.citrusserver.entities.Book; import org.ntlab.citrusserver.repositories.BookManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashMap; @Path("/accounts") @Component public class BooksRest { // BookRestはクラス private final BookManager bookManager; @Autowired // スプリングブートにいうサイン public BooksRest(BookManager bm){ //public クラス名()がコンストラクタ bookManager = bm; } private final HashMap<String, HashMap<Integer, Book>> books = new HashMap<>(); /// {account_id}/books /// その人の本のタイトルとかを返す @Path("/{account_id}/books") @GET @Produces(MediaType.APPLICATION_JSON) public HashMap<Integer, Book> getBooks(@PathParam("account_id") String account_id, @QueryParam("token") String token){ return bookManager.getBooks(account_id, token); } @Path("/{account_id}/books") @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public int createBook(@PathParam("account_id") String account_id, @FormParam("title") String title, @FormParam("color") String color, @FormParam("publicity") Boolean publicity, @QueryParam("token") String token) { return bookManager.createBook(account_id, title, color, publicity, token); } /// {account_id}/books/{book_id} /// 本の情報を取得 @Path("/{account_id}/books/{book_id}") @GET @Produces(MediaType.APPLICATION_JSON) public Book getBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ return bookManager.getBook(account_id, book_id, token); } /// 本の削除 @Path("/{account_id}/books/{book_id}") @DELETE @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void deleteTodoById(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ bookManager.deleteBook(account_id, book_id, token); } /// /{account_id}/books/{book_id}/title /// 本のタイトルを返す @Path("/{account_id}/books/{book_id}/title") @GET @Produces(MediaType.TEXT_PLAIN) public String getTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ return bookManager.getTitle(account_id, book_id, token); } /// 本のタイトル変更 @Path("/{account_id}/books/{book_id}/title") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void putTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("title") String title, @QueryParam("token") String token){ bookManager.putTitle(account_id, book_id, title, token); } /// /accounts/{account_id}/books/{book_id}/public /// 本の公開状態を返す @Path("/{account_id}/books/{book_id}/public") @GET @Produces(MediaType.TEXT_PLAIN) public Boolean getPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ return bookManager.getPublicity(account_id, book_id, token); } /// 公開情報を変更する @Path("/{account_id}/books/{book_id}/public") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void putPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("publicity") Boolean publicity, @QueryParam("token") String token){ bookManager.putPublicity(account_id, book_id, publicity, token); } /// /accounts/{account_id}/books/{book_id}/color /// 公開情報を変更する @Path("/{account_id}/books/{book_id}/color") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void putColor(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("color") String color, @QueryParam("token") String token){ bookManager.putColor(account_id, book_id, color, token); } }