File tree 2 files changed +48
-1
lines changed
src/main/java/com/learn/SpringBootRESTService
2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 9
9
import org .springframework .web .bind .annotation .RequestBody ;
10
10
import org .springframework .web .bind .annotation .RestController ;
11
11
12
+ import com .learn .SpringBootRESTService .Service .LibraryService ;
12
13
import com .learn .SpringBootRESTService .repository .LibraryRepository ;
13
14
14
15
@ RestController
@@ -20,10 +21,14 @@ public class LibraryController {
20
21
@ Autowired
21
22
AddResponse addResponse ;
22
23
24
+ @ Autowired
25
+ LibraryService libraryService ;
23
26
24
27
@ PostMapping ("/addBook" )
25
28
public ResponseEntity addBookImplementation (@ RequestBody Library library ) {
26
- String id = library .getIsbn ()+library .getAisle ();
29
+ String id = libraryService .buildId (library .getIsbn (),library .getAisle ());
30
+
31
+ if (!libraryService .checkBookExists (id )) {
27
32
library .setId (id );
28
33
repository .save (library );
29
34
addResponse .setMsg ("Success!! Book is added" );
@@ -32,6 +37,14 @@ public ResponseEntity addBookImplementation(@RequestBody Library library) {
32
37
headers .add ("Unique-Header" , "First" );
33
38
//return addResponse; without response headers!
34
39
return new ResponseEntity <AddResponse >(addResponse ,headers ,HttpStatus .CREATED );
40
+ }
41
+ else
42
+ {
43
+ addResponse .setMsg ("Book Already Exists" );
44
+ addResponse .setId (id );
45
+
46
+ return new ResponseEntity <AddResponse >(addResponse ,HttpStatus .ACCEPTED );
47
+ }
35
48
36
49
}
37
50
Original file line number Diff line number Diff line change
1
+ package com .learn .SpringBootRESTService .Service ;
2
+
3
+ import java .util .Optional ;
4
+
5
+ import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .stereotype .Service ;
7
+
8
+ import com .learn .SpringBootRESTService .Library ;
9
+ import com .learn .SpringBootRESTService .repository .LibraryRepository ;
10
+ @ Service
11
+ public class LibraryService {
12
+ @ Autowired
13
+ LibraryRepository libraryRepository ;
14
+
15
+ public String buildId (String isbn , int aisle ) {
16
+ return isbn +aisle ;
17
+ }
18
+
19
+ public boolean checkBookExists (String id ) {
20
+
21
+ Optional <Library > lib = libraryRepository .findById (id );
22
+ if (lib .isPresent ()) {
23
+ return true ;
24
+ }
25
+ else {
26
+ return false ;
27
+ }
28
+
29
+
30
+ }
31
+
32
+
33
+
34
+ }
You can’t perform that action at this time.
0 commit comments