Skip to content

Commit

Permalink
[MODINVOSTO-187] Handle unknown entity updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Nov 6, 2024
1 parent 61cfb60 commit 2f764a9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/java/org/folio/dao/DbUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.folio.dao;

import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;

import io.vertx.core.Future;
import io.vertx.ext.web.handler.HttpException;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;

public class DbUtils {

private static final String TABLE_NAME_TEMPLATE = "%s.%s";
Expand All @@ -10,4 +16,10 @@ public static String getTenantTableName(String tenantId, String tableName) {
return TABLE_NAME_TEMPLATE.formatted(convertToPsqlStandard(tenantId), tableName);
}

public static Future<Void> verifyEntityUpdate(RowSet<Row> updated) {
return updated.rowCount() == 1
? Future.succeededFuture()
: Future.failedFuture(new HttpException(NOT_FOUND.getStatusCode(), NOT_FOUND.getReasonPhrase()));
}

}
5 changes: 4 additions & 1 deletion src/main/java/org/folio/dao/invoice/InvoicePostgresDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.dao.DbUtils;
import org.folio.dbschema.ObjectMapperTool;
import org.folio.rest.jaxrs.model.Contents;
import org.folio.rest.jaxrs.model.DocumentMetadata;
Expand Down Expand Up @@ -67,7 +68,9 @@ public Future<String> createInvoice(Invoice invoice, Conn conn) {
@Override
public Future<Void> updateInvoice(Invoice invoice, Conn conn) {
return conn.update(INVOICE_TABLE, invoice, invoice.getId())
.onFailure(t -> log.error("updateInvoice failed for invoice with id {}", invoice.getId(), t))
.compose(DbUtils::verifyEntityUpdate)
.onSuccess(v -> log.info("updateInvoice:: Invoice with id: '{}' successfully updated", invoice.getId()))
.onFailure(t -> log.error("Update failed for invoice with id {}", invoice.getId(), t))
.mapEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.vertx.core.Future;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.dao.DbUtils;
import org.folio.rest.jaxrs.model.InvoiceLine;
import org.folio.rest.persist.Conn;
import org.folio.rest.persist.Criteria.Criterion;
Expand Down Expand Up @@ -36,6 +37,7 @@ public Future<String> createInvoiceLine(InvoiceLine invoiceLine, Conn conn) {
public Future<Void> updateInvoiceLine(String id, InvoiceLine invoiceLine, Conn conn) {
log.trace("updateInvoiceLine:: Updating invoice line: {}", invoiceLine);
return conn.update(INVOICE_LINE_TABLE, invoiceLine, id)
.compose(DbUtils::verifyEntityUpdate)
.onSuccess(v -> log.info("updateInvoiceLine:: Updated invoice line with id: {}", id))
.onFailure(t -> log.error("Failed to update invoice line with id: {}", id, t))
.mapEmpty();
Expand Down

0 comments on commit 2f764a9

Please sign in to comment.