forked from PoolC/Mincho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookController.java
106 lines (92 loc) · 3.99 KB
/
BookController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.poolc.api.book.controller;
import lombok.RequiredArgsConstructor;
import org.poolc.api.book.client.BookClient;
import org.poolc.api.book.dto.request.CreateBookRequest;
import org.poolc.api.book.dto.request.UpdateBookRequest;
import org.poolc.api.book.service.BookService;
import org.poolc.api.member.domain.Member;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.Min;
@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
public class BookController {
private final BookClient bookClient;
private final BookService bookService;
@GetMapping("/search")
public ResponseEntity<?> searchBooks(@RequestParam String query,
@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page) {
try {
return new ResponseEntity<>(bookClient.searchBooks(query, page), HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/all")
public ResponseEntity<?> getAllBooks(@RequestParam(value = "page", defaultValue = "0") @Min(0) Integer page) {
try {
return new ResponseEntity<>(bookService.getAllBooks(page), HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/{id}")
public ResponseEntity<?> getBook(@PathVariable Long id) {
try {
return new ResponseEntity<>(bookService.getBook(id), HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/new")
public ResponseEntity<?> addBook(@AuthenticationPrincipal Member member,
@Valid @RequestBody CreateBookRequest request) {
try {
bookService.createBook(member, request);
return new ResponseEntity<>(HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteBook(@AuthenticationPrincipal Member member, @PathVariable Long id) {
try {
bookService.deleteBook(member, id);
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PutMapping("/{id}")
public ResponseEntity<?> updateBook(@AuthenticationPrincipal Member member, @PathVariable Long id,
@Valid @RequestBody UpdateBookRequest request) {
try {
bookService.updateBook(member, id, request);
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/{id}/borrow")
public ResponseEntity<?> borrowBook(@AuthenticationPrincipal Member member, @PathVariable Long id) {
try {
bookService.borrow(member, id);
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/{id}/return")
public ResponseEntity<?> returnBook(@AuthenticationPrincipal Member member, @PathVariable Long id) {
try {
bookService.returnBook(member, id);
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}