-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooksController.java
More file actions
97 lines (59 loc) · 2.71 KB
/
BooksController.java
File metadata and controls
97 lines (59 loc) · 2.71 KB
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
package com.book.store.athena.controllers;
import com.book.store.athena.api.OpenLibraryService;
import com.book.store.athena.model.dto.books.FindAllBooksDTO;
import com.book.store.athena.model.dto.books.CreateBooksDTO;
import com.book.store.athena.model.dto.books.SearchBooksDTO;
import com.book.store.athena.model.dto.books.UpdateBooksDTO;
import com.book.store.athena.model.dto.client.FindUserBooksByIdDTO;
import com.book.store.athena.services.BooksService;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;
@RestController
@RequestMapping("/books")
public class BooksController {
private final BooksService booksService;
private final OpenLibraryService openLibraryService;
public BooksController(BooksService booksService, OpenLibraryService openLibraryService) {
this.booksService = booksService;
this.openLibraryService = openLibraryService;
}
@PostMapping("/create")
protected ResponseEntity <Void> createBook (@RequestBody @Valid CreateBooksDTO books) {
booksService.create(books);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@GetMapping("/collection")
protected ResponseEntity <Set<FindAllBooksDTO>> findAllBooks () {
var bookList = booksService.findAll();
return ResponseEntity.status(HttpStatus.OK).body(bookList);
}
@GetMapping("/search")
protected ResponseEntity <List<Object>> searchBooks (@RequestBody @Valid SearchBooksDTO books) {
var result = openLibraryService.search(books);
return ResponseEntity.status(HttpStatus.OK).body(result);
}
@GetMapping("/favorite-books/{id}")
protected ResponseEntity <Set<FindUserBooksByIdDTO>> findAllFavorites (@PathVariable Long id) {
var favorites = booksService.findUserBooksById(id);
return ResponseEntity.status(HttpStatus.OK).body(favorites);
}
@PutMapping("/update/{id}")
protected ResponseEntity <Void> updateBook (@PathVariable Long id, @Valid @RequestBody UpdateBooksDTO books) {
booksService.update(id, books);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
@PutMapping("/reactivate/{id}")
protected ResponseEntity <Void> reactivateBook (@PathVariable Long id) {
booksService.reactivate(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
@DeleteMapping("/delete/{id}")
protected ResponseEntity <Void> deleteBook (@PathVariable Long id) {
booksService.disable(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
}