Newer
Older
CitrusServer / src / main / java / org / ntlab / citrusserver / resources / PublicBooksRest.java
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.PublicBookManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;

@Path("/public_books")
@Component

public class PublicBooksRest {
    private final PublicBookManager publicBookManager;
    @Autowired
    public PublicBooksRest(PublicBookManager pbm) {
        publicBookManager = pbm;
    }

    //公開されている本の一覧を返す
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<Book> getAllPublicBooks() {
        return publicBookManager.getAllPublicBooks();
    }

//    //検索条件を指定して本を検索(タイトル)
//    @Path("/search")
//    @GET
//    @Produces(MediaType.APPLICATION_JSON)
//    public ArrayList<Book> searchBooksByTitle(@QueryParam("search_title") String search_title) {
//        return publicBookManager.searchBooksByTitle(search_title);
//    }

//    //検索条件を指定して本を検索(タイトル ソート可能)
//    @Path("/search")
//    @GET
//    @Produces(MediaType.APPLICATION_JSON)
//    public ArrayList<Book> searchBooksByTitle(@QueryParam("search_title") String search_title, @QueryParam("sort_by") Integer sort_by) {
//        return publicBookManager.searchBooksByTitle(search_title, sort_by);
//    }
//
//    //検索条件を指定して本を検索(アカウント)
//    @Path("/search")
//    @GET
//    @Produces(MediaType.APPLICATION_JSON)
//    public ArrayList<Book> searchBooksByAccount(@QueryParam("search_account_id") String search_account_id) {
//        return publicBookManager.searchBooksByAccount(search_account_id);
//    }
//
//    //検索条件を指定して本を検索(アカウント ソート可能)
//    @Path("/search")
//    @GET
//    @Produces(MediaType.APPLICATION_JSON)
//    public ArrayList<Book> searchBooksByAccount(@QueryParam("search_account_id") String search_account_id, @QueryParam("sort_by") int sort_by) {
//        return publicBookManager.searchBooksByAccount(search_account_id, sort_by);
//    }

    //検索条件を指定して本を検索(アカウントとタイトル)
    @Path("/search")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<Book> searchBooksByTitleAndAccount(@QueryParam("search_title") String search_title, @QueryParam("search_account_id") String search_account_id) {
        if(!search_title.isEmpty() && !search_account_id.isEmpty()) {
            return publicBookManager.searchBooksByTitleAndAccount(search_title, search_account_id);
        } else if(!search_title.isEmpty()) {
            return publicBookManager.searchBooksByTitle(search_title);
        } else if(!search_account_id.isEmpty()) {
            return publicBookManager.searchBooksByAccount(search_account_id);
        } else {
            return publicBookManager.getAllPublicBooks();
        }
    }

}