99import io .swagger .annotations .ApiResponse ;
1010import io .swagger .annotations .ApiResponses ;
1111import static org .couchbase .quickstart .configs .CollectionNames .PROFILE ;
12+
13+ import org .couchbase .quickstart .configs .CollectionNames ;
14+ import org .couchbase .quickstart .configs .DBProperties ;
1215import org .couchbase .quickstart .models .Profile ;
1316import org .couchbase .quickstart .models .ProfileRequest ;
14- import org .couchbase .quickstart .models .ProfileResult ;
1517import org .springframework .http .HttpStatus ;
1618import org .springframework .http .MediaType ;
1719import org .springframework .http .ResponseEntity ;
@@ -29,10 +31,12 @@ public class ProfileController {
2931
3032 private Cluster cluster ;
3133 private Collection profileCol ;
34+ private DBProperties dbProperties ;
3235
33- public ProfileController (Cluster cluster , Bucket bucket ) {
36+ public ProfileController (Cluster cluster , Bucket bucket , DBProperties dbProperties ) {
3437 this .cluster = cluster ;
3538 this .profileCol = bucket .collection (PROFILE );
39+ this .dbProperties = dbProperties ;
3640 }
3741
3842
@@ -50,30 +54,6 @@ public ResponseEntity<Profile> save(@RequestBody final ProfileRequest userProfil
5054 return ResponseEntity .status (HttpStatus .CREATED ).body (profile );
5155 }
5256
53-
54- @ CrossOrigin (value ="*" )
55- @ GetMapping (path = "/profiles/" , produces = MediaType .APPLICATION_JSON_VALUE )
56- @ ApiOperation (value = "Search for user profiles" , produces = MediaType .APPLICATION_JSON_VALUE )
57- @ ApiResponses (
58- value = {
59- @ ApiResponse (code = 200 , message = "Returns the list of user profiles" ),
60- @ ApiResponse (code = 500 , message = "Error occurred in getting user profiles" , response = Error .class )
61- })
62- public ResponseEntity <List <Profile >> getProfiles (
63- @ RequestParam (required =false , defaultValue = "5" ) int limit ,
64- @ RequestParam (required =false , defaultValue = "0" ) int skip ,
65- @ RequestParam String search ) {
66-
67- final List <Profile > profiles = cluster .query ("SELECT p.* FROM user_profile._default.profile p WHERE lower(p.firstName) LIKE $search OR lower(p.lastName) LIKE $search LIMIT $limit OFFSET $skip" ,
68- queryOptions ().parameters (JsonObject .create ()
69- .put ("search" , "%" + search .toLowerCase ()+"%" )
70- .put ("limit" , limit )
71- .put ("skip" , skip ))
72- .scanConsistency (QueryScanConsistency .REQUEST_PLUS ))
73- .rowsAs (Profile .class );
74- return ResponseEntity .status (HttpStatus .OK ).body (profiles );
75- }
76-
7757 @ CrossOrigin (value ="*" )
7858 @ GetMapping (path = "/{id}" , produces = MediaType .APPLICATION_JSON_VALUE )
7959 @ ApiOperation (value = "Get a user profile by Id" , produces = MediaType .APPLICATION_JSON_VALUE )
@@ -99,9 +79,9 @@ public ResponseEntity<Profile> update( @PathVariable("id") String id, @RequestBo
9979
10080 try {
10181 profileCol .upsert (id , profile );
102- return ResponseEntity .status (HttpStatus .CREATED ).body (new ProfileResult ( profile , "" ) );
82+ return ResponseEntity .status (HttpStatus .CREATED ).body (profile );
10383 } catch (Exception e ){
104- return ResponseEntity .status (HttpStatus .EXPECTATION_FAILED ).body (new ProfileResult ( new Profile (), String . format ( "Error: %s" , e . getMessage ())) );
84+ return ResponseEntity .status (HttpStatus .EXPECTATION_FAILED ).body (null );
10585 }
10686 }
10787
@@ -120,8 +100,32 @@ public ResponseEntity delete(@PathVariable UUID id){
120100 profileCol .remove (id .toString ());
121101 return ResponseEntity .status (HttpStatus .OK ).body (null );
122102 } catch (Exception e ){
123- return ResponseEntity .status (HttpStatus .EXPECTATION_FAILED ).body (new ProfileResult ( new Profile (), String . format ( "Error: %s" , e . getMessage ())) );
103+ return ResponseEntity .status (HttpStatus .EXPECTATION_FAILED ).body (null );
124104 }
125105 }
126106
107+ @ CrossOrigin (value ="*" )
108+ @ GetMapping (path = "/profiles/" , produces = MediaType .APPLICATION_JSON_VALUE )
109+ @ ApiOperation (value = "Search for user profiles" , produces = MediaType .APPLICATION_JSON_VALUE )
110+ @ ApiResponses (
111+ value = {
112+ @ ApiResponse (code = 200 , message = "Returns the list of user profiles" ),
113+ @ ApiResponse (code = 500 , message = "Error occurred in getting user profiles" , response = Error .class )
114+ })
115+ public ResponseEntity <List <Profile >> getProfiles (
116+ @ RequestParam (required =false , defaultValue = "5" ) int limit ,
117+ @ RequestParam (required =false , defaultValue = "0" ) int skip ,
118+ @ RequestParam String search ) {
119+
120+ final List <Profile > profiles = cluster .query ("SELECT p.* FROM $bucketName._default.$collectionName p WHERE lower(p.firstName) LIKE $search OR lower(p.lastName) LIKE $search LIMIT $limit OFFSET $skip" ,
121+ queryOptions ().parameters (JsonObject .create ()
122+ .put ("bucketName" , dbProperties .getBucketName ())
123+ .put ("collectionName" , CollectionNames .PROFILE )
124+ .put ("search" , "%" + search .toLowerCase ()+"%" )
125+ .put ("limit" , limit )
126+ .put ("skip" , skip ))
127+ .scanConsistency (QueryScanConsistency .REQUEST_PLUS ))
128+ .rowsAs (Profile .class );
129+ return ResponseEntity .status (HttpStatus .OK ).body (profiles );
130+ }
127131}
0 commit comments