-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(call-number-types): Publish domain events on changes (#1118)
* feat(call-number-types): Publish domain events on changes Implements: MODINVSTOR-1275
- Loading branch information
1 parent
8ed0e49
commit 3621165
Showing
11 changed files
with
266 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/org/folio/persist/CallNumberTypeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.folio.persist; | ||
|
||
import static org.folio.rest.persist.PgUtil.postgresClient; | ||
import static org.folio.services.callnumber.CallNumberTypeService.CALL_NUMBER_TYPE_TABLE; | ||
|
||
import io.vertx.core.Context; | ||
import java.util.Map; | ||
import org.folio.rest.jaxrs.model.CallNumberType; | ||
|
||
public class CallNumberTypeRepository extends AbstractRepository<CallNumberType> { | ||
|
||
public CallNumberTypeRepository(Context context, Map<String, String> okapiHeaders) { | ||
super(postgresClient(context, okapiHeaders), CALL_NUMBER_TYPE_TABLE, CallNumberType.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/org/folio/services/callnumber/CallNumberTypeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.folio.services.callnumber; | ||
|
||
import static org.folio.rest.persist.PgUtil.deleteById; | ||
import static org.folio.rest.persist.PgUtil.get; | ||
import static org.folio.rest.persist.PgUtil.getById; | ||
import static org.folio.rest.persist.PgUtil.post; | ||
import static org.folio.rest.persist.PgUtil.put; | ||
|
||
import io.vertx.core.Context; | ||
import io.vertx.core.Future; | ||
import java.util.Map; | ||
import javax.ws.rs.core.Response; | ||
import org.folio.persist.CallNumberTypeRepository; | ||
import org.folio.rest.exceptions.BadRequestException; | ||
import org.folio.rest.jaxrs.model.CallNumberType; | ||
import org.folio.rest.jaxrs.model.CallNumberTypes; | ||
import org.folio.rest.jaxrs.resource.CallNumberTypes.DeleteCallNumberTypesByIdResponse; | ||
import org.folio.rest.jaxrs.resource.CallNumberTypes.GetCallNumberTypesByIdResponse; | ||
import org.folio.rest.jaxrs.resource.CallNumberTypes.GetCallNumberTypesResponse; | ||
import org.folio.rest.jaxrs.resource.CallNumberTypes.PostCallNumberTypesResponse; | ||
import org.folio.rest.jaxrs.resource.CallNumberTypes.PutCallNumberTypesByIdResponse; | ||
import org.folio.services.domainevent.CallNumberTypeDomainEventPublisher; | ||
|
||
public class CallNumberTypeService { | ||
|
||
public static final String CALL_NUMBER_TYPE_TABLE = "call_number_type"; | ||
|
||
private static final String SYSTEM_CALL_NUMBER_TYPE_SOURCE = "system"; | ||
|
||
private final Context context; | ||
private final Map<String, String> okapiHeaders; | ||
private final CallNumberTypeRepository repository; | ||
private final CallNumberTypeDomainEventPublisher domainEventService; | ||
|
||
public CallNumberTypeService(Context context, Map<String, String> okapiHeaders) { | ||
this.context = context; | ||
this.okapiHeaders = okapiHeaders; | ||
|
||
this.repository = new CallNumberTypeRepository(context, okapiHeaders); | ||
this.domainEventService = new CallNumberTypeDomainEventPublisher(context, okapiHeaders); | ||
} | ||
|
||
public Future<Response> getByQuery(String cql, int offset, int limit) { | ||
return get(CALL_NUMBER_TYPE_TABLE, CallNumberType.class, CallNumberTypes.class, | ||
cql, offset, limit, okapiHeaders, context, GetCallNumberTypesResponse.class); | ||
} | ||
|
||
public Future<Response> getByTypeId(String id) { | ||
return getById(CALL_NUMBER_TYPE_TABLE, CallNumberType.class, id, okapiHeaders, context, | ||
GetCallNumberTypesByIdResponse.class); | ||
} | ||
|
||
public Future<Response> create(CallNumberType type) { | ||
return post(CALL_NUMBER_TYPE_TABLE, type, okapiHeaders, context, PostCallNumberTypesResponse.class) | ||
.onSuccess(domainEventService.publishCreated()); | ||
} | ||
|
||
public Future<Response> update(String id, CallNumberType type) { | ||
return getIfNotSystemCallNumberType(id, "System call number type couldn't be updated") | ||
.compose(oldType -> put(CALL_NUMBER_TYPE_TABLE, type, id, okapiHeaders, context, | ||
PutCallNumberTypesByIdResponse.class) | ||
.onSuccess(domainEventService.publishUpdated(oldType)) | ||
); | ||
} | ||
|
||
public Future<Response> delete(String id) { | ||
return getIfNotSystemCallNumberType(id, "System call number type couldn't be deleted") | ||
.compose(oldType -> deleteById(CALL_NUMBER_TYPE_TABLE, id, okapiHeaders, context, | ||
DeleteCallNumberTypesByIdResponse.class) | ||
.onSuccess(domainEventService.publishRemoved(oldType)) | ||
); | ||
} | ||
|
||
private Future<CallNumberType> getIfNotSystemCallNumberType(String id, String errorMessage) { | ||
return repository.getById(id) | ||
.compose(callNumberType -> { | ||
if (isSystemSource(callNumberType)) { | ||
return Future.failedFuture(new BadRequestException(errorMessage)); | ||
} | ||
return Future.succeededFuture(callNumberType); | ||
}); | ||
} | ||
|
||
private boolean isSystemSource(CallNumberType callNumberType) { | ||
return SYSTEM_CALL_NUMBER_TYPE_SOURCE.equals(callNumberType.getSource()); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/folio/services/domainevent/CallNumberTypeDomainEventPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.folio.services.domainevent; | ||
|
||
import static io.vertx.core.Future.succeededFuture; | ||
import static org.folio.InventoryKafkaTopic.CALL_NUMBER_TYPE; | ||
import static org.folio.rest.tools.utils.TenantTool.tenantId; | ||
|
||
import io.vertx.core.Context; | ||
import io.vertx.core.Future; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.folio.persist.CallNumberTypeRepository; | ||
import org.folio.rest.jaxrs.model.CallNumberType; | ||
|
||
public class CallNumberTypeDomainEventPublisher | ||
extends AbstractDomainEventPublisher<CallNumberType, CallNumberType> { | ||
|
||
public CallNumberTypeDomainEventPublisher(Context context, Map<String, String> okapiHeaders) { | ||
super(new CallNumberTypeRepository(context, okapiHeaders), | ||
new CommonDomainEventPublisher<>(context, okapiHeaders, | ||
CALL_NUMBER_TYPE.fullTopicName(tenantId(okapiHeaders)))); | ||
} | ||
|
||
@Override | ||
protected Future<List<Pair<String, CallNumberType>>> getRecordIds(Collection<CallNumberType> types) { | ||
return succeededFuture(types.stream() | ||
.map(type -> pair(type.getId(), type)) | ||
.toList() | ||
); | ||
} | ||
|
||
@Override | ||
protected CallNumberType convertDomainToEvent(String instanceId, CallNumberType type) { | ||
return type; | ||
} | ||
|
||
@Override | ||
protected String getId(CallNumberType type) { | ||
return type.getId(); | ||
} | ||
} |
Oops, something went wrong.