Skip to content

Commit

Permalink
feature/book,bookinfo,record errorcode
Browse files Browse the repository at this point in the history
  • Loading branch information
tfer2442 committed Feb 18, 2023
1 parent 867fa1b commit 388085e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
29 changes: 22 additions & 7 deletions src/main/java/KNU/Navibook/server/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import KNU.Navibook.server.domain.User;
import KNU.Navibook.server.domain.Record;
import KNU.Navibook.server.domain.Book;
import KNU.Navibook.server.exceptions.BookConflictException;
import KNU.Navibook.server.exceptions.BookNotFoundException;
import KNU.Navibook.server.exceptions.UserNotFoundException;
import KNU.Navibook.server.service.*;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -41,6 +43,9 @@ public List<Book> bookAll() {
@GetMapping("/api/book/{bookInfo}") // bookInfoId로 book조회
@ResponseBody
public List<Book> bookSearch(@PathVariable("bookInfo") Long infoId){
if (bookInfoService.findOne(infoId) == null){
throw new BookNotFoundException(String.format("bookInfoId %s not found",infoId));
}
return bookService.findBook(bookInfoService.findOne(infoId));
}

Expand All @@ -54,13 +59,20 @@ public Book bookAdd(@RequestBody Map<String, Object> requestData) {
Long bookId = tmp1.longValue();
Long bookInfoId = tmp2.longValue();

if (bookService.findOne(bookId)!=null){ // book이 이미 존재할 때 error code 409
throw new BookConflictException(String.format("bookId %s not Acceptable", bookId));
}

Book book = new Book();
book.setId(bookId);

BookInfo bookinfo = bookInfoService.findOne(bookInfoId);
book.setBookInfo(bookinfo);

if (bookinfo==null){ // bookinfo가 없을 때 error code 404
throw new BookNotFoundException(String.format("bookInfoId %s not found", bookInfoId));
}

book.setBookInfo(bookinfo);
bookService.saveBook(book);

return book;
Expand All @@ -69,12 +81,15 @@ public Book bookAdd(@RequestBody Map<String, Object> requestData) {
@PostMapping("/api/book/delete") // 없는 값 삭제하면 badgate
@ResponseBody
public void bookDelete(@RequestBody Book book){
if (book==null){
throw new BookNotFoundException(String.format("book not found"));
}
bookService.deleteBook(book.getId());
}

//bookid에 맞는 book 없으면 오류
//bookshelfid에 맞는 bookshelf 없으면 오류
@PostMapping("/api/book/position")
@PostMapping("/api/book/location")
@ResponseBody
public Book bookAddPosition(@RequestBody Map<String, Object> requestData) {
Integer tmp1 = (Integer) requestData.get("bookId");
Expand All @@ -87,7 +102,7 @@ public Book bookAddPosition(@RequestBody Map<String, Object> requestData) {
Book book=bookService.findOne(bookId);
//잘못된 bookid일 경우 404
if (book==null){
throw new UserNotFoundException(String.format("bookId %s not found",bookId));
throw new BookNotFoundException(String.format("bookId %s not found",bookId));
}
BookShelf bookShelf = bookShelfService.findOne(bookShelfId);
//잘못된 bookshelfid일 경우 404
Expand All @@ -113,7 +128,7 @@ public Book bookBorrow(@RequestBody Map<String, Object> requestData) {
Book book=bookService.findOne(bookId);
//잘못된 bookid일 경우 404
if (book==null){
throw new UserNotFoundException(String.format("bookId %s not found",bookId));
throw new BookNotFoundException(String.format("bookId %s not found",bookId));
}
//잘못된 useid일 경우 404
User user= userService.findOne(userId);
Expand All @@ -122,7 +137,7 @@ public Book bookBorrow(@RequestBody Map<String, Object> requestData) {
}
//이미 대여중인 도서인 경우 404
if (book.getStatus()==Boolean.FALSE){
throw new UserNotFoundException(String.format("userID %s not found",userId));
throw new BookNotFoundException(String.format("bookID %s not found",bookId));
}

book.setStatus(Boolean.FALSE);
Expand Down Expand Up @@ -159,7 +174,7 @@ public Book bookReturn(@RequestBody Map<String, Object> requestData) {
Book book=bookService.findOne(bookId);
//잘못된 bookid일 경우 404
if (book==null){
throw new UserNotFoundException(String.format("bookId %s not found",bookId));
throw new BookNotFoundException(String.format("bookId %s not found",bookId));
}
//잘못된 useid일 경우 404
User user= userService.findOne(userId);
Expand All @@ -168,7 +183,7 @@ public Book bookReturn(@RequestBody Map<String, Object> requestData) {
}
//이미 있는 책인데 반납하려는 경우 404
if (book.getStatus()==Boolean.TRUE){
throw new UserNotFoundException(String.format("userID %s not found",userId));
throw new BookNotFoundException(String.format("bookID %s not found",bookId));
}
book.setStatus(Boolean.TRUE);
book.setBookShelf(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import KNU.Navibook.server.domain.Book;
import KNU.Navibook.server.domain.BookInfo;
import KNU.Navibook.server.exceptions.BookNotFoundException;
import KNU.Navibook.server.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -21,12 +22,18 @@ public Object returnBookInfoAll(@RequestParam(value="bookInfoId", required=false
if(id==null)
return bookInfoService.findAll();
else
if (bookInfoService.findOne(id)==null){
throw new BookNotFoundException(String.format("bookInfoId %s not found", id));
}
return bookInfoService.findOne(id);
}

@GetMapping("/{bookName}")
@ResponseBody
public List<BookInfo> returnBookInfo(@PathVariable("bookName") String name){
if (bookInfoService.findByBookNameContaining(name).isEmpty()){
throw new BookNotFoundException(String.format("bookInfo not found"));
}
return bookInfoService.findByBookNameContaining(name);
}

Expand All @@ -46,6 +53,9 @@ public BookInfo save(@RequestBody BookInfo bookInfo){
@PostMapping ("/edit")
@ResponseBody
public BookInfo edit(@RequestBody BookInfo bookInfo){
if (bookInfo==null){
throw new BookNotFoundException(String.format("bookInfo not found"));
}
return bookInfoService.save(bookInfo);
}

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/KNU/Navibook/server/controller/RecordController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import KNU.Navibook.server.domain.Record;
import KNU.Navibook.server.domain.User;
import KNU.Navibook.server.dto.RecordDTO;
import KNU.Navibook.server.exceptions.BookNotFoundException;
import KNU.Navibook.server.exceptions.UserNotFoundException;
import KNU.Navibook.server.service.BookService;
import KNU.Navibook.server.service.RecordService;
import KNU.Navibook.server.service.UserService;
Expand Down Expand Up @@ -68,6 +70,10 @@ public RecordDTO userRecord(@PathVariable("userId") String userId,
List<Record> records = recordService.findRecordByUser(user);
List<Record> pageRecords = new ArrayList<>();

if (user==null){
throw new UserNotFoundException(String.format("userId %s not found",userId));
}

if (orderBy.equals("book")){ // bookName 내림차순 정렬
Comparator<Record> bs = Comparator.comparing(a -> a.getBook().getBookInfo().getBookName());
Collections.sort(records, bs);
Expand All @@ -83,9 +89,6 @@ else if (orderBy.equals("giveDate")){ // GiveDate 내림차순 정렬, 반납
Collections.sort(records, gs);
Collections.reverse(records);
}
else{// 예외처리 해줘야 함.

}

for(int i = (page*10)-10; (records != null) && (records.size() > i) && (i < page*10); i++){
pageRecords.add(records.get(i));
Expand All @@ -107,6 +110,10 @@ public RecordDTO bookRecord(@PathVariable("bookId") Long bookId,
Collections.sort(records, ts);
Collections.reverse(records);

if (book==null){
throw new BookNotFoundException(String.format("bookId %s not found",bookId));
}

for(int i = (page*10)-10; (records != null) && (records.size() > i) && (i < page*10); i++){
pageRecords.add(records.get(i));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package KNU.Navibook.server.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.CONFLICT)
public class BookConflictException extends RuntimeException {
public BookConflictException(String message){
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package KNU.Navibook.server.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class BookNotFoundException extends RuntimeException {
public BookNotFoundException(String message){
super(message);
}

}

0 comments on commit 388085e

Please sign in to comment.