package org.ntlab.citrusserver.resources; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.ntlab.citrusserver.entities.Book; import org.ntlab.citrusserver.repositories.AccountManager; import org.ntlab.citrusserver.repositories.BookManager; import org.ntlab.citrusserver.repositories.TodoManager; 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; private final TodoManager todoManager; @Autowired // スプリングブートにいうサイン public BooksRest(BookManager bm, AccountManager ac, TodoManager tm){//public クラス名()がコンストラクタ bookManager = bm; accountManager = ac; todoManager = tm; } 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){ accountCheck(account_id); tokenCheck(account_id, token); return bookManager.getBooks(account_id); } @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) { accountCheck(account_id); tokenCheck(account_id, token); return bookManager.createBook(account_id, title, color, publicity); } /// {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){ accountCheck(account_id); tokenCheck(account_id, token); return bookManager.getBook(account_id, book_id); } /// 本の削除 @Path("/{account_id}/books/{book_id}") @DELETE @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String deleteBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ accountCheck(account_id); tokenCheck(account_id, token); todoManager.deleteAllTodosByBookId(account_id, book_id);//削除時、Todoも消す bookManager.deleteBook(account_id, book_id); return "success"; } /// /{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){ accountCheck(account_id); tokenCheck(account_id, token); return bookManager.getTitle(account_id, book_id); } /// 本のタイトル変更 @Path("/{account_id}/books/{book_id}/title") @PUT @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putTitle(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("title") String title, @FormParam("token") String token){ accountCheck(account_id); tokenCheck(account_id, token); bookManager.putTitle(account_id, book_id, title); return "success"; } /// /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){ accountCheck(account_id); tokenCheck(account_id, token); return bookManager.getPublicity(account_id, book_id); } /// 公開情報を変更する @Path("/{account_id}/books/{book_id}/public") @PUT @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putPublicity(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("publicity") Boolean publicity, @FormParam("token") String token){ accountCheck(account_id); tokenCheck(account_id, token); bookManager.putPublicity(account_id, book_id, publicity); return "success"; } /// /accounts/{account_id}/books/{book_id}/color /// 色を変更する @Path("/{account_id}/books/{book_id}/color") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String putColor(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @FormParam("color") String color, @FormParam("token") String token){ accountCheck(account_id); tokenCheck(account_id, token); bookManager.putColor(account_id, book_id, color); return "success"; } ///--------------------------------------------------------------------- ///private ///--------------------------------------------------------------------- private void accountCheck(String account_id){ if(accountManager.getAccount(account_id) == null){ var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); throw new WebApplicationException(response.build()); } } private void tokenCheck(String account_id, String token){ if(!accountManager.checkToken(account_id, token)) { var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); throw new WebApplicationException(response.build()); } } }