diff --git a/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java b/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java index a32600b..907e5ee 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java @@ -29,23 +29,7 @@ } public ArrayList getAllPublicBooks(int sortBy){ - if(sortBy == 0){ - ArrayList books = getAllPublicBooks(); - books.sort(Comparator.comparing(Book::getTime).reversed()); - return books; - } else if(sortBy == 1){ - ArrayList books = new ArrayList<>(); - for(Book book : getAllPublicBooks()){ - books.add(new BookForSort(book, favoriteManager.getFavorited(book.getAccountId(), book.getBookId()).size())); - } - books.sort(Comparator.comparing(BookForSort::favoriteNum).reversed()); - ArrayList res = new ArrayList<>(); - for(BookForSort book : books){ - res.add(book.book()); - } - return res; - } - return null; + return sort(getAllPublicBooks(), sortBy); } public ArrayList searchBooksByTitle(String title){ @@ -63,24 +47,7 @@ public ArrayList searchBooksByTitle(String title, int sortBy){ - if(sortBy == 0){ - ArrayList result = searchBooksByTitle(title); - result.sort(Comparator.comparing(Book::getTime).reversed()); - return result; - }else if(sortBy == 1){ - ArrayList books = new ArrayList<>(); - for(Book book : searchBooksByTitle(title)){ - books.add(new BookForSort(book, favoriteManager.getFavorited(book.getAccountId(), book.getBookId()).size())); - } - books.sort(Comparator.comparing(BookForSort::favoriteNum).reversed()); - ArrayList res = new ArrayList<>(); - for(BookForSort book : books){ - res.add(book.book()); - } - return res; - - } - return null; + return sort(searchBooksByTitle(title), sortBy); } public ArrayList searchBooksByAccount(String accountId){ @@ -96,23 +63,7 @@ } public ArrayList searchBooksByAccount(String accountId, int sortBy){ - if(sortBy == 0){ - ArrayList result = searchBooksByAccount(accountId); - result.sort(Comparator.comparing(Book::getTime).reversed()); - return result; - } else if(sortBy == 1){ - ArrayList books = new ArrayList<>(); - for(Book book : searchBooksByAccount(accountId)){ - books.add(new BookForSort(book, favoriteManager.getFavorited(book.getAccountId(), book.getBookId()).size())); - } - books.sort(Comparator.comparing(BookForSort::favoriteNum).reversed()); - ArrayList res = new ArrayList<>(); - for(BookForSort book : books){ - res.add(book.book()); - } - return res; - } - return null; + return sort(searchBooksByAccount(accountId), sortBy); } public ArrayList searchBooksByTitleAndAccount(String title, String accountId){ @@ -127,23 +78,7 @@ } public ArrayList searchBooksByTitleAndAccount(String title, String accountId, int sortBy){ - if (sortBy == 0) { - ArrayList result = searchBooksByTitleAndAccount(title, accountId); - result.sort(Comparator.comparing(Book::getTime).reversed()); - return result; - } else if(sortBy == 1){ - ArrayList books = new ArrayList<>(); - for(Book book : searchBooksByTitleAndAccount(title, accountId)){ - books.add(new BookForSort(book, favoriteManager.getFavorited(book.getAccountId(), book.getBookId()).size())); - } - books.sort(Comparator.comparing(BookForSort::favoriteNum).reversed()); - ArrayList res = new ArrayList<>(); - for(BookForSort book : books){ - res.add(book.book()); - } - return res; - } - return null; + return sort(searchBooksByTitleAndAccount(title, accountId), sortBy); } @Override @@ -165,24 +100,25 @@ } -// private List sort(List books, int sortMode){ -// if(sortMode == 0){ -// ArrayList books = getAllPublicBooks(); -// books.sort(Comparator.comparing(Book::getTime).reversed()); -// return books; -// } else if(sortMode == 1){ -// ArrayList books = new ArrayList<>(); -// for(Book book : getAllPublicBooks()){ -// books.add(new BookForSort(book, favoriteManager.getFavorited(book.getAccountId(), book.getBookId()).size())); -// } -// books.sort(Comparator.comparing(BookForSort::favoriteNum).reversed()); -// ArrayList res = new ArrayList<>(); -// for(BookForSort book : books){ -// res.add(book.book()); -// } -// return res; -// } -// } + private ArrayList sort(ArrayList books, int sortMode){ + switch(sortMode){ + case 0: + books.sort(Comparator.comparing(Book::getTime).reversed()); + return books; + case 1: + ArrayList booksForSort = new ArrayList<>(); + for(Book book : books){ + booksForSort.add(new BookForSort(book, favoriteManager.getFavorited(book.getAccountId(), book.getBookId()).size())); + } + booksForSort.sort(Comparator.comparing(BookForSort::favoriteNum).reversed()); + ArrayList res = new ArrayList<>(); + for(BookForSort book : booksForSort){ + res.add(book.book()); + } + return res; + } + return new ArrayList<>(); + } }