-
Notifications
You must be signed in to change notification settings - Fork 4
/
BookRestController.java
145 lines (119 loc) · 4.96 KB
/
BookRestController.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.example.myFirstSpring.restcontroller;
import com.example.myFirstSpring.model.Book;
import com.example.myFirstSpring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Optional;
@RestController
@RequestMapping("api/book")
public class BookRestController {
//here we are creating our end-point rest API
@Autowired
BookService bookService;
@GetMapping("populate")
public ResponseEntity<HashMap<String, Book> > populate(@RequestParam("qty") int qty) {
//
HttpHeaders headers = new HttpHeaders();
headers.add("operation", "populate");
headers.add("version", "api 1.0");
HashMap<String, Book> fakeBooks = bookService.populateFakeBooks(qty);
if (fakeBooks.size() > 0 ) {
headers.add("statusOperation", "success");
return ResponseEntity.accepted().headers(headers).body(fakeBooks);
} else {
headers.add("statusOperation", "not populated");
return ResponseEntity.accepted().body(null);
}
}
//CRUD: read
@GetMapping("books")
public ResponseEntity<Iterable<Book>> getAllBooks() {
//
HttpHeaders headers = new HttpHeaders();
headers.add("operation", "getAllBooks");
headers.add("version", "api 1.0");
headers.add("statusOperation", "success");
return ResponseEntity.accepted().headers(headers).body(bookService.getAllBooks());
}
//CRUD: read, find one book by id
@GetMapping("getBook")
public ResponseEntity<Book> findBookById(@RequestParam("id") String id) {
//
HttpHeaders headers = new HttpHeaders();
headers.add("operation", "findBookById");
headers.add("version", "api 1.0");
Optional<Book> bookFound = bookService.findBookById(id);
if (bookFound.isPresent()) {
headers.add("statusOperation", "success");
return ResponseEntity.accepted().headers(headers).body(bookFound.get());
} else {
headers.add("statusOperation", "not found");
return ResponseEntity.accepted().body(null);
}
}
//CRUD: create
@PostMapping(path = "createBook", consumes = "application/JSON")
public ResponseEntity<Book> addBook(@RequestBody Book book) {
//
HttpHeaders headers = new HttpHeaders();
headers.add("operation", "createBook");
headers.add("version", "api 1.0");
headers.add("statusOperation", "success");
Book bookCreated = bookService.createBook(book);
if (bookCreated != null) {
headers.add("statusOperation", "success");
return ResponseEntity.accepted().headers(headers).body(bookCreated);
} else {
headers.add("statusOperation", "not created");
return ResponseEntity.accepted().body(null);
}
}
//CRUD: delete
@DeleteMapping("deleteBook")
public ResponseEntity<Book> deleteBook(@RequestParam String id) {
//
HttpHeaders headers = new HttpHeaders();
headers.add("operation", "deleteBook");
headers.add("version", "api 1.0");
Optional<Book> bookFound = bookService.findBookById(id);
boolean isBook = bookFound.isPresent();
if (isBook) {
//Optional<Book> deletedBook = bookservice.deleteBookById(id);
bookService.deleteBookById(id);
headers.add("operationStatus", "deleted");
return ResponseEntity.accepted().headers(headers).body(bookFound.get());
} else {
headers.add("operationStatus", "not found");
return ResponseEntity.accepted().body(null);
}
}
//CRUD: update
@PutMapping("/updateBook/{id}")
public ResponseEntity<Book> updateBook(@PathVariable String id, @RequestBody Book dataBook) {
HttpHeaders headers = new HttpHeaders();
headers.add("operation", "updateBook");
headers.add("version", "api 1.0");
Optional<Book> bookFound = bookService.findBookById(id);
if (bookFound.isPresent()){
bookService.updateBook(bookFound.get(), dataBook);
headers.add("operationStatus","updated");
return ResponseEntity.accepted().headers(headers).body(bookFound.get());
} else {
headers.add("operationStatus","not found");
return ResponseEntity.accepted().headers(headers).body(null);}
}
//CRUD: delete book by title
@DeleteMapping("deleteBookByTitle")
public ResponseEntity<Book> deleteBookByTitle(@RequestParam String title) {
//
HttpHeaders headers = new HttpHeaders();
headers.add("operation", "deleteBookByTitle");
headers.add("version", "api 1.0");
//findBookByTitle(String title)
//deleteBookByTitle(S tring title)
return null;
}
}