Newer
Older
CitrusServer / src / main / java / org / ntlab / citrusserver / resources / BooksRest.java
package org.ntlab.citrusserver.resources;

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
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.getAccount(account_id) == null){
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else{
                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) {

        if (accountManager.getAccount(account_id) == null){
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else {
                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){

        if (accountManager.getAccount(account_id) == null){
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else{
                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){

        if(accountManager.getAccount(account_id) == null){
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else{
                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){

        if (accountManager.getAccount(account_id) == null) {
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else{
                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){

        if(accountManager.getAccount(account_id) == null){
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else{
                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){

        if (accountManager.getAccount(account_id) == null) {
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else{
                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){

        if(accountManager.getAccount(account_id) == null){
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else{
                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){

        if(accountManager.getAccount(account_id) == null){
            var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません");
            throw new WebApplicationException(response.build());
        }
        else{
            if(!accountManager.checkToken(account_id, token)) {
                var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
                throw new WebApplicationException(response.build());
            }
            else{
                bookManager.putColor(account_id, book_id, color);
                return "success";
            }
        }
    }
}