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.AccountManager; 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; private final AccountManager accountManager; @Autowired // スプリングブートにいうサイン public BooksRest(BookManager bm, AccountManager ac){//public クラス名()がコンストラクタ bookManager = bm; accountManager = ac; } 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){ if(!accountManager.checkToken(account_id, token)) return null; return bookManager.getBooks(account_id, token); } @Path("/{account_id}/books") @POST @Produces(MediaType.APPLICATION_JSON) // intとかstringとかがたくさん返ってくるから、json public voidじゃないときは、返さなあかんから、 @Produces(MediaType.APPLICATION_JSON) これがいる @Consumes(MediaType.APPLICATION_FORM_URLENCODED) // postmanのbodyに入力する値がある時 public Book createBook(@PathParam("account_id") String account_id, @FormParam("title") String title, @FormParam("color") String color, @FormParam("publicity") Boolean publicity, @FormParam("token") String token) { if(!accountManager.checkToken(account_id, token)) return null; 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){ if(!accountManager.checkToken(account_id, token)) return null; 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){ if(!accountManager.checkToken(account_id, token)) return; 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){ if(!accountManager.checkToken(account_id, token)) return null; 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, @FormParam("token") String token){ if(!accountManager.checkToken(account_id, token)) return; 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){ if(!accountManager.checkToken(account_id, token)) return null; 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, @FormParam("token") String token){ if(!accountManager.checkToken(account_id, token)) return; 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, @FormParam("token") String token){ if(!accountManager.checkToken(account_id, token)) return; bookManager.putColor(account_id, book_id, color, token); } }