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 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) { //タイトルとアカウントIDでの検索
                return publicBookManager.searchBooksByTitleAndAccount(search_title, search_account_id);
            } else if (search_title != null) { //タイトルのみでの検索
                return publicBookManager.searchBooksByTitle(search_title);
            } else if (search_account_id != null) { //アカウントIDのみでの検索
                return publicBookManager.searchBooksByAccount(search_account_id);
            } else { //タイトルもアカウントIDもない場合(すべての本を返す)
                return publicBookManager.getAllPublicBooks();
            }
        } else { //ソートする場合
                if(search_title != null && search_account_id != null) { //タイトルとアカウントIDでの検索
                    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) { //アカウントIDのみでの検索
                    return publicBookManager.searchBooksByAccount(search_account_id, sort_by);
                } else { //タイトルもアカウントIDもない場合(すべての本を返す)
                    return publicBookManager.getAllPublicBooks();
                }
            }
        }

}