1313import org .acme .persistence .EligibilityCheckRepository ;
1414import org .acme .persistence .StorageService ;
1515
16+ import java .util .ArrayList ;
1617import java .util .List ;
1718import java .util .Map ;
1819import java .util .Optional ;
@@ -27,27 +28,27 @@ public class EligibilityCheckResource {
2728 StorageService storageService ;
2829
2930 @ GET
30- @ Path ("/check " )
31- public Response getAllChecks (@ Context SecurityIdentity identity ) {
31+ @ Path ("/checks " )
32+ public Response getPublicChecks (@ Context SecurityIdentity identity ) {
3233 String userId = AuthUtils .getUserId (identity );
3334 if (userId == null ) {
3435 return Response .status (Response .Status .UNAUTHORIZED ).build ();
3536 }
3637 Log .info ("Fetching all eligibility checks. User: " + userId );
37- List <EligibilityCheck > checks = eligibilityCheckRepository .getAllPublicChecks ();
38+ List <EligibilityCheck > checks = eligibilityCheckRepository .getPublicChecks ();
3839
3940 return Response .ok (checks , MediaType .APPLICATION_JSON ).build ();
4041 }
4142
4243 @ GET
43- @ Path ("/check /{checkId}" )
44- public Response getCheck (@ Context SecurityIdentity identity , @ PathParam ("checkId" ) String checkId ) {
44+ @ Path ("/checks /{checkId}" )
45+ public Response getPublicCheck (@ Context SecurityIdentity identity , @ PathParam ("checkId" ) String checkId ) {
4546 String userId = AuthUtils .getUserId (identity );
4647 if (userId == null ){
4748 return Response .status (Response .Status .UNAUTHORIZED ).build ();
4849 }
4950 Log .info ("Fetching all eligibility checks. User: " + userId );
50- Optional <EligibilityCheck > checkOpt = eligibilityCheckRepository .getCheck (checkId );
51+ Optional <EligibilityCheck > checkOpt = eligibilityCheckRepository .getPublicCheck (checkId );
5152
5253 if (checkOpt .isEmpty ()){
5354 return Response .status (Response .Status .NOT_FOUND ).build ();
@@ -61,20 +62,19 @@ public Response getCheck(@Context SecurityIdentity identity, @PathParam("checkId
6162 return Response .ok (check , MediaType .APPLICATION_JSON ).build ();
6263 }
6364
64- // Utility endpoint to create an Eligibility check
65- // In the future seperate endpoints will need to be created for publishing public checks and creating private checks
65+ // Utility endpoint, public checks come from the Library API schema and shouldn't usually be created through the app
6666 @ POST
67- @ Path ("/check " )
68- public Response createCheck (@ Context SecurityIdentity identity ,
67+ @ Path ("/checks " )
68+ public Response createPublicCheck (@ Context SecurityIdentity identity ,
6969 EligibilityCheck newCheck ) {
7070 String userId = AuthUtils .getUserId (identity );
7171
7272 //TODO: Add validations for user provided data
7373 newCheck .setOwnerId (userId );
74- newCheck .setPublic (true ); // By default all created checks are public
75- newCheck .setVersion ("1" );
74+ newCheck .setPublic (true );
75+ newCheck .setVersion (1 );
7676 try {
77- String checkId = eligibilityCheckRepository .saveNewCheck (newCheck );
77+ String checkId = eligibilityCheckRepository .savePublicCheck (newCheck );
7878 newCheck .setId (checkId );
7979 return Response .ok (newCheck , MediaType .APPLICATION_JSON ).build ();
8080 } catch (Exception e ){
@@ -84,15 +84,17 @@ public Response createCheck(@Context SecurityIdentity identity,
8484 }
8585 }
8686
87+ // Utility endpoint, public checks are static and come from the library api schema
88+ // and usually should not be updated through the app
8789 @ PUT
88- @ Path ("/check " )
89- public Response updateCheck (@ Context SecurityIdentity identity ,
90+ @ Path ("/checks " )
91+ public Response updatePublicCheck (@ Context SecurityIdentity identity ,
9092 EligibilityCheck updateCheck ){
9193 String userId = AuthUtils .getUserId (identity );
9294
9395 // TODO: Add authorization to update check
9496 try {
95- eligibilityCheckRepository .updateCheck (updateCheck );
97+ eligibilityCheckRepository .savePublicCheck (updateCheck );
9698 return Response .ok ().entity (updateCheck ).build ();
9799 } catch (Exception e ){
98100 return Response .status (Response .Status .INTERNAL_SERVER_ERROR )
@@ -114,7 +116,7 @@ public Response updateCheckDmn(@Context SecurityIdentity identity, SaveDmnReques
114116 }
115117
116118 String userId = AuthUtils .getUserId (identity );
117- Optional <EligibilityCheck > checkOpt = eligibilityCheckRepository .getCustomCheck (userId , checkId );
119+ Optional <EligibilityCheck > checkOpt = eligibilityCheckRepository .getWorkingCustomCheck (userId , checkId );
118120 if (checkOpt .isEmpty ()){
119121 return Response .status (Response .Status .NOT_FOUND ).build ();
120122 }
@@ -146,31 +148,47 @@ public Response updateCheckDmn(@Context SecurityIdentity identity, SaveDmnReques
146148 }
147149 }
148150
151+ // By default, returns the most recent versions of all published checks owned by the calling user
152+ // If the query parameter 'working' is set to true,
153+ // then all the working check objects owned by the user are returned
149154 @ GET
150- @ Path ("/account/check " )
151- public Response getCustomChecks (@ Context SecurityIdentity identity ) {
155+ @ Path ("/custom-checks " )
156+ public Response getCustomChecks (@ Context SecurityIdentity identity , @ QueryParam ( "working" ) Boolean working ) {
152157 String userId = AuthUtils .getUserId (identity );
153158 if (userId == null ) {
154159 return Response .status (Response .Status .UNAUTHORIZED ).build ();
155160 }
156161
157- Log .info ("Fetching all eligibility checks. User: " + userId );
158- List <EligibilityCheck > checks = eligibilityCheckRepository .getCustomChecks (userId );
162+ List <EligibilityCheck > checks ;
163+
164+ if (working != null && working ){
165+ Log .info ("Fetching all working custom checks. User: " + userId );
166+ checks = eligibilityCheckRepository .getWorkingCustomChecks (userId );
167+ } else {
168+ Log .info ("Fetching all published custom checks. User: " + userId );
169+ checks = eligibilityCheckRepository .getPublishedCustomChecks (userId );
170+ }
159171
160172 return Response .ok (checks , MediaType .APPLICATION_JSON ).build ();
161173 }
162174
163175 @ GET
164- @ Path ("/account/check /{checkId}" )
165- public Response getCustomCheck (@ Context SecurityIdentity identity ) {
176+ @ Path ("/custom-checks /{checkId}" )
177+ public Response getCustomCheck (@ Context SecurityIdentity identity , @ PathParam ( "checkId" ) String checkId , @ QueryParam ( "working" ) Boolean working ) {
166178 String userId = AuthUtils .getUserId (identity );
167179 if (userId == null ) {
168180 return Response .status (Response .Status .UNAUTHORIZED ).build ();
169181 }
170182
171- Log .info ("Fetching all eligibility checks. User: " + userId );
172- Optional <EligibilityCheck > checkOpt = eligibilityCheckRepository .getCustomCheck (userId , userId );
183+ Optional <EligibilityCheck > checkOpt ;
173184
185+ if (working != null && working ){
186+ Log .info ("Fetching working custom check: " + checkId + " User: " + userId );
187+ checkOpt = eligibilityCheckRepository .getWorkingCustomCheck (userId , userId );
188+ } else {
189+ Log .info ("Fetching published custom check: " + checkId + " User: " + userId );
190+ checkOpt = eligibilityCheckRepository .getPublishedCustomCheck (userId , userId );
191+ }
174192
175193 if (checkOpt .isEmpty ()){
176194 return Response .status (Response .Status .NOT_FOUND ).build ();
@@ -185,17 +203,17 @@ public Response getCustomCheck(@Context SecurityIdentity identity) {
185203 }
186204
187205 @ POST
188- @ Path ("/account/check " )
206+ @ Path ("/custom-checks " )
189207 public Response createCustomCheck (@ Context SecurityIdentity identity ,
190208 EligibilityCheck newCheck ) {
191209 String userId = AuthUtils .getUserId (identity );
192210
193211 //TODO: Add validations for user provided data
194212 newCheck .setOwnerId (userId );
195213 newCheck .setPublic (false );
196- newCheck .setVersion ("1" );
214+ newCheck .setVersion (1 );
197215 try {
198- String checkId = eligibilityCheckRepository .saveNewCustomCheck (newCheck );
216+ String checkId = eligibilityCheckRepository .saveWorkingCustomCheck (newCheck );
199217 newCheck .setId (checkId );
200218 return Response .ok (newCheck , MediaType .APPLICATION_JSON ).build ();
201219 } catch (Exception e ){
@@ -206,14 +224,14 @@ public Response createCustomCheck(@Context SecurityIdentity identity,
206224 }
207225
208226 @ PUT
209- @ Path ("/account/check " )
227+ @ Path ("/custom-checks " )
210228 public Response updateCustomCheck (@ Context SecurityIdentity identity ,
211229 EligibilityCheck updateCheck ){
212230 String userId = AuthUtils .getUserId (identity );
213231
214232 // TODO: Add authorization to update check
215233 try {
216- eligibilityCheckRepository .updateCustomCheck (updateCheck );
234+ eligibilityCheckRepository .saveWorkingCustomCheck (updateCheck );
217235 return Response .ok ().entity (updateCheck ).build ();
218236 } catch (Exception e ){
219237 return Response .status (Response .Status .INTERNAL_SERVER_ERROR )
0 commit comments