File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed
src/main/java/com/learn/SpringBootRESTService Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 99import org .springframework .web .bind .annotation .RequestBody ;
1010import org .springframework .web .bind .annotation .RestController ;
1111
12+ import com .learn .SpringBootRESTService .Service .LibraryService ;
1213import com .learn .SpringBootRESTService .repository .LibraryRepository ;
1314
1415@ RestController
@@ -20,10 +21,14 @@ public class LibraryController {
2021 @ Autowired
2122 AddResponse addResponse ;
2223
24+ @ Autowired
25+ LibraryService libraryService ;
2326
2427 @ PostMapping ("/addBook" )
2528 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 )) {
2732 library .setId (id );
2833 repository .save (library );
2934 addResponse .setMsg ("Success!! Book is added" );
@@ -32,6 +37,14 @@ public ResponseEntity addBookImplementation(@RequestBody Library library) {
3237 headers .add ("Unique-Header" , "First" );
3338 //return addResponse; without response headers!
3439 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+ }
3548
3649 }
3750
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