File tree 1 file changed +20
-7
lines changed
11-spring-boot-and-mvc/spring-boot-and-mvc/src/main/java/org/example/Controllers
1 file changed +20
-7
lines changed Original file line number Diff line number Diff line change @@ -26,15 +26,28 @@ public Book getBookById(@PathVariable("id") int id) {
26
26
}
27
27
28
28
@ PostMapping
29
- public void addBook (Book book ) {
30
- throw new UnsupportedOperationException ( "Not implemented yet" );
29
+ public void addBook (@ RequestBody Book book ) {
30
+ books . add ( book );
31
31
}
32
-
33
- public void updateBook (int id , Book book ) {
34
- throw new UnsupportedOperationException ("Not implemented yet" );
32
+ @ PutMapping ("/{id}" )
33
+ public void updateBook (@ PathVariable int id , @ RequestBody Book book ) {
34
+ for (int i = 0 ; i < books .size (); i ++) {
35
+ if (books .get (i ).getId () == id ) {
36
+ books .set (i , book ); // Update the book in the list
37
+ return ;
38
+ }
39
+ }
35
40
}
36
41
37
- public void deleteBook (int id ) {
38
- throw new UnsupportedOperationException ("Not implemented yet" );
42
+
43
+ @ DeleteMapping ("/{id}" )
44
+ public void deleteBook (@ PathVariable int id ) {
45
+ Iterator <Book > iter = books .iterator ();
46
+ while (iter .hasNext ()) {
47
+ Book book = iter .next ();
48
+ if (book .getId () == id ) {
49
+ iter .remove ();
50
+ }
51
+ }
39
52
}
40
53
}
You can’t perform that action at this time.
0 commit comments