Skip to content

Commit

Permalink
feature/allRecord, bookRecord API
Browse files Browse the repository at this point in the history
  • Loading branch information
tfer2442 committed Feb 9, 2023
1 parent 0930dda commit 1076fde
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
72 changes: 67 additions & 5 deletions src/main/java/KNU/Navibook/server/controller/RecordController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package KNU.Navibook.server.controller;

import KNU.Navibook.server.domain.Book;
import KNU.Navibook.server.domain.Record;
import KNU.Navibook.server.domain.User;
import KNU.Navibook.server.service.BookService;
import KNU.Navibook.server.service.RecordService;
import KNU.Navibook.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -16,8 +18,44 @@ public class RecordController {
RecordService recordService;
@Autowired
UserService userService;
@Autowired
BookService bookService;


@GetMapping("/api/record") // record 전체 검색
@ResponseBody
public List<Record> allRecord(@RequestParam("page") Integer page, @RequestParam("orderBy") String orderBy) {
List<Record> records = recordService.findRecords();
List<Record> pageRecords = new ArrayList<>();

if (orderBy.equals("book")){ // bookName 오름차순 정렬
Comparator<Record> bs = Comparator.comparing(a -> a.getBook().getBookInfo().getBookName());
Collections.sort(records, bs);
}
else if (orderBy.equals("takeDate")){ // TakeDate 내림차순 정렬
Comparator<Record> ts = Comparator.comparing(Record::getTakeDate);
Collections.sort(records, ts);
Collections.reverse(records);
}
else if (orderBy.equals("giveDate")){ // GiveDate 내림차순 정렬
Comparator<Record> gs = Comparator.comparing(Record::getGiveDate);
Collections.sort(records, gs);
Collections.reverse(records);
}
else if (orderBy.equals("user")){ // userId 오름차순 정렬
Comparator<Record> us = Comparator.comparing(a -> a.getUser().getId());
Collections.sort(records, us);
}else{ // 예외처리 해줘야 함.

}

for(int i = (page*10)-10; (records != null) && (records.size() > i) && (i < page*10); i++){
pageRecords.add(records.get(i));
}

@GetMapping("/api/record/user/{userId}")
return pageRecords;
}
@GetMapping("/api/record/user/{userId}") // userId로 userRecord 검색
@ResponseBody
public List<Record> userRecord(@PathVariable("userId") String userId,
@RequestParam("page") Integer page, @RequestParam("orderBy") String orderBy)
Expand All @@ -26,17 +64,23 @@ public List<Record> userRecord(@PathVariable("userId") String userId,
List<Record> records = recordService.findRecordByUser(user);
List<Record> pageRecords = new ArrayList<>();

if (orderBy.equals("book")){ // bookName 오름차순 정렬
if (orderBy.equals("book")){ // bookName 내림차순 정렬
Comparator<Record> bs = Comparator.comparing(a -> a.getBook().getBookInfo().getBookName());
Collections.sort(records, bs);
Collections.reverse(records);
}
else if (orderBy.equals("takeDate")){ // TakeDate 오름차순 정렬
else if (orderBy.equals("takeDate")){ // TakeDate 내림차순 정렬
Comparator<Record> ts = Comparator.comparing(Record::getTakeDate);
Collections.sort(records, ts);
Collections.reverse(records);
}
else if (orderBy.equals("giveDate")){ // GiveDate 오름차순 정렬
Comparator<Record> gs = Comparator.comparing(Record::getGiveDate);
else if (orderBy.equals("giveDate")){ // GiveDate 내림차순 정렬, 반납 안했으면 제일 앞으로 옴.
Comparator<Record> gs = Comparator.comparing(Record::getGiveDate, Comparator.nullsLast(Comparator.naturalOrder()));
Collections.sort(records, gs);
Collections.reverse(records);
}
else{// 예외처리 해줘야 함.

}

for(int i = (page*10)-10; (records != null) && (records.size() > i) && (i < page*10); i++){
Expand All @@ -45,6 +89,24 @@ else if (orderBy.equals("giveDate")){ // GiveDate 오름차순 정렬

return pageRecords;
}
@GetMapping("/api/record/book/{bookId}") //bookId로 bookRecord 검색
@ResponseBody
public List<Record> bookRecord(@PathVariable("bookId") Long bookId,
@RequestParam("page") Integer page)
{
Book book = bookService.findOne(bookId);
List<Record> records = recordService.findRecordByBook(book);
List<Record> pageRecords = new ArrayList<>();

Comparator<Record> ts = Comparator.comparing(Record::getTakeDate);
Collections.sort(records, ts);
Collections.reverse(records);

for(int i = (page*10)-10; (records != null) && (records.size() > i) && (i < page*10); i++){
pageRecords.add(records.get(i));
}

return pageRecords;
}
}

6 changes: 3 additions & 3 deletions src/main/java/KNU/Navibook/server/service/RecordService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class RecordService {

public List<Record> findRecords(){
return recordRepository.findAll();
}
public List<Record> findRecordByBook(Book book) { return recordRepository.findBybook(book);} // 책으로 Record찾기.
public List<Record> findRecordByUser(User user) { return recordRepository.findByuser(user);} // 유저로 Record찾기.
} // 모든 Record 찾기
public List<Record> findRecordByBook(Book book) { return recordRepository.findBybook(book);} // 책으로 Record 찾기
public List<Record> findRecordByUser(User user) { return recordRepository.findByuser(user);} // 유저로 Record 찾기
public Record saveRecord(Record record){
return recordRepository.save(record);
}
Expand Down

0 comments on commit 1076fde

Please sign in to comment.