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 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> searchBooksByTitleAndAccount(@QueryParam("search_title") String search_title, @QueryParam("search_account_id") String search_account_id, @QueryParam("sort_by") Integer sort_by) { if(sort_by == null) { if (search_title != null && search_account_id != null) { return publicBookManager.searchBooksByTitleAndAccount(search_title, search_account_id); } else if (search_title != null) { return publicBookManager.searchBooksByTitle(search_title); } else if (search_account_id != null) { return publicBookManager.searchBooksByAccount(search_account_id); } else { return publicBookManager.getAllPublicBooks(); } } else { if(search_title != null && search_account_id != null) { return publicBookManager.searchBooksByTitleAndAccount(search_title, search_account_id, sort_by); } else if(search_title != null) { return publicBookManager.searchBooksByTitle(search_title, sort_by); } else if(search_account_id != null) { return publicBookManager.searchBooksByAccount(search_account_id, sort_by); } else { return publicBookManager.getAllPublicBooks(); } } } }