forked from folio-org/mod-circulation-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestPreferencesAPI.java
99 lines (79 loc) · 3.88 KB
/
RequestPreferencesAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.folio.rest.impl;
import static org.folio.HttpStatus.HTTP_UNPROCESSABLE_ENTITY;
import static io.vertx.core.Future.succeededFuture;
import java.util.Map;
import javax.ws.rs.core.Response;
import org.folio.rest.annotations.Validate;
import org.folio.rest.impl.util.OkapiResponseUtil;
import org.folio.rest.jaxrs.model.Errors;
import org.folio.rest.jaxrs.model.RequestPreference;
import org.folio.rest.jaxrs.model.RequestPreferences;
import org.folio.rest.jaxrs.resource.RequestPreferenceStorage;
import org.folio.rest.persist.PgUtil;
import org.folio.rest.tools.utils.ValidationHelper;
import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
public class RequestPreferencesAPI implements RequestPreferenceStorage {
private static final String REQUEST_PREFERENCE_TABLE = "user_request_preference";
@Override
@Validate
public void getRequestPreferenceStorageRequestPreference(String totalRecords, int offset,
int limit, String query, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.get(REQUEST_PREFERENCE_TABLE, RequestPreference.class, RequestPreferences.class,
query, offset, limit, okapiHeaders, vertxContext,
GetRequestPreferenceStorageRequestPreferenceResponse.class, asyncResultHandler);
}
@Override
@Validate
public void postRequestPreferenceStorageRequestPreference(RequestPreference entity,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.post(REQUEST_PREFERENCE_TABLE, entity, okapiHeaders, vertxContext, PostRequestPreferenceStorageRequestPreferenceResponse.class, uniqueUserViolationHandler(entity, asyncResultHandler));
}
@Override
@Validate
public void getRequestPreferenceStorageRequestPreferenceById(String id,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.getById(REQUEST_PREFERENCE_TABLE, RequestPreference.class, id, okapiHeaders, vertxContext,
GetRequestPreferenceStorageRequestPreferenceByIdResponse.class, asyncResultHandler);
}
@Override
@Validate
public void deleteRequestPreferenceStorageRequestPreferenceById(String id,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.deleteById(REQUEST_PREFERENCE_TABLE, id, okapiHeaders, vertxContext,
DeleteRequestPreferenceStorageRequestPreferenceByIdResponse.class, asyncResultHandler);
}
@Override
@Validate
public void putRequestPreferenceStorageRequestPreferenceById(String id, RequestPreference entity,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
PgUtil.put(REQUEST_PREFERENCE_TABLE, entity, id, okapiHeaders, vertxContext, PutRequestPreferenceStorageRequestPreferenceByIdResponse.class, uniqueUserViolationHandler(entity, asyncResultHandler));
}
private Handler<AsyncResult<Response>> uniqueUserViolationHandler(RequestPreference entity, Handler<AsyncResult<Response>> asyncResultHandler) {
return reply -> {
if (isUniqueUserIdViolation(reply)) {
asyncResultHandler.handle(
succeededFuture(Response.status(HTTP_UNPROCESSABLE_ENTITY.toInt())
.header("Content-Type", "application/json")
.entity(preferenceAlreadyExistsError(entity.getUserId())).build()));
} else {
asyncResultHandler.handle(reply);
}
};
}
private boolean isUniqueUserIdViolation(AsyncResult<Response> reply) {
return OkapiResponseUtil.containsErrorMessage(
reply, " value already exists in table ");
}
private Errors preferenceAlreadyExistsError(String userId) {
return ValidationHelper.createValidationErrorMessage(
"userId", userId,
"Request preference for specified user already exists");
}
}