Skip to content

Commit

Permalink
[MODINVOSTO-187] Refactor invoice update and invoice line create/update
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Nov 4, 2024
1 parent c14110d commit bda0c1c
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 35 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/folio/dao/lines/InvoiceLinesDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@
import java.util.List;

public interface InvoiceLinesDAO {

Future<List<InvoiceLine>> getInvoiceLines(Criterion criterion, Conn conn);

Future<String> createInvoiceLine(InvoiceLine invoiceLine, Conn conn);

Future<Void> updateInvoiceLine(String id, InvoiceLine invoiceLine, Conn conn);

}
29 changes: 23 additions & 6 deletions src/main/java/org/folio/dao/lines/InvoiceLinesPostgresDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.folio.rest.jaxrs.model.InvoiceLine;
import org.folio.rest.persist.Conn;
import org.folio.rest.persist.Criteria.Criterion;
import org.folio.rest.persist.interfaces.Results;

import java.util.List;

Expand All @@ -16,12 +17,28 @@ public class InvoiceLinesPostgresDAO implements InvoiceLinesDAO {

@Override
public Future<List<InvoiceLine>> getInvoiceLines(Criterion criterion, Conn conn) {
log.trace("InvoiceLinesPostgresDAO getInvoiceLines, criterion={}", criterion);
log.trace("getInvoiceLines:: Getting invoice lines with criterion: {}", criterion);
return conn.get(INVOICE_LINE_TABLE, InvoiceLine.class, criterion, false)
.map(results -> {
log.trace("getInvoiceLines success, criterion={}", criterion);
return results.getResults();
})
.onFailure(t -> log.error("getInvoiceLines failed, criterion={}", criterion, t));
.map(Results::getResults)
.onSuccess(lines -> log.trace("getInvoiceLines:: Got {} invoice lines with criterion: {}", lines.size(), criterion))
.onFailure(t -> log.error("Failed to get invoice lines with criterion: {}", criterion, t));
}

@Override
public Future<String> createInvoiceLine(InvoiceLine invoiceLine, Conn conn) {
log.trace("createInvoiceLine:: Creating invoice line: {}", invoiceLine);
return conn.save(INVOICE_LINE_TABLE, invoiceLine)
.onSuccess(invoiceLineId -> log.info("createInvoiceLine:: Created invoice line with id: {}", invoiceLineId))
.onFailure(t -> log.error("Failed to create invoice line: {}", invoiceLine, t));
}

@Override
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)
.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();
}

}
10 changes: 6 additions & 4 deletions src/main/java/org/folio/rest/impl/InvoiceStorageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.folio.rest.jaxrs.model.InvoiceLineCollection;
import org.folio.rest.jaxrs.resource.InvoiceStorage;
import org.folio.rest.persist.PgUtil;
import org.folio.service.InvoiceLineStorageService;
import org.folio.service.InvoiceStorageService;
import org.folio.spring.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,10 +29,13 @@ public class InvoiceStorageImpl implements InvoiceStorage {
public static final String INVOICE_ID_FIELD_NAME = "invoiceId";
public static final String INVOICE_LINE_TABLE = "invoice_lines";
public static final String INVOICE_PREFIX = "/invoice-storage/invoices/";
public static final String INVOICE_LINES_PREFIX = "/invoice-storage/invoice-lines/";
public static final String INVOICE_TABLE = "invoices";

@Autowired
private InvoiceStorageService invoiceStorageService;
@Autowired
private InvoiceLineStorageService invoiceLineStorageService;

public InvoiceStorageImpl() {
SpringContextUtil.autowireDependencies(this, Vertx.currentContext());
Expand Down Expand Up @@ -117,16 +121,14 @@ public void getInvoiceStorageInvoiceLines(String totalRecords, int offset, int l
@Override
public void postInvoiceStorageInvoiceLines(InvoiceLine entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.post(INVOICE_LINE_TABLE, entity, okapiHeaders, vertxContext, PostInvoiceStorageInvoiceLinesResponse.class,
asyncResultHandler);
invoiceLineStorageService.createInvoiceLine(entity, asyncResultHandler, vertxContext, okapiHeaders);
}

@Validate
@Override
public void putInvoiceStorageInvoiceLinesById(String id, InvoiceLine entity, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
PgUtil.put(INVOICE_LINE_TABLE, entity, id, okapiHeaders, vertxContext, PutInvoiceStorageInvoiceLinesByIdResponse.class,
asyncResultHandler);
invoiceLineStorageService.updateInvoiceLine(id, entity, okapiHeaders, asyncResultHandler, vertxContext);
}

@Validate
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/org/folio/service/InvoiceLineStorageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.folio.service;

import static org.folio.rest.impl.InvoiceStorageImpl.INVOICE_LINES_PREFIX;
import static org.folio.rest.utils.ResponseUtils.buildErrorResponse;
import static org.folio.rest.utils.ResponseUtils.buildNoContentResponse;
import static org.folio.rest.utils.ResponseUtils.buildResponseWithLocation;
import static org.folio.rest.utils.RestConstants.OKAPI_URL;

import javax.ws.rs.core.Response;
import java.util.Map;

import org.folio.dao.lines.InvoiceLinesDAO;
import org.folio.rest.jaxrs.model.InvoiceLine;
import org.folio.rest.persist.DBClient;

import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import io.vertx.ext.web.handler.HttpException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RequiredArgsConstructor
public class InvoiceLineStorageService {

private final InvoiceLinesDAO invoiceLinesDAO;

public void createInvoiceLine(InvoiceLine invoiceLine, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext, Map<String, String> headers) {
try {
vertxContext.runOnContext(v -> {
log.info("createInvoiceLine:: Creating a new invoiceLine by id: {}", invoiceLine.getId());
new DBClient(vertxContext, headers).getPgClient()
.withTrans(conn -> invoiceLinesDAO.createInvoiceLine(invoiceLine, conn))
.onSuccess(s -> {
log.info("createInvoiceLine:: Successfully created a new invoiceLine by id: {}", invoiceLine.getId());
asyncResultHandler.handle(buildResponseWithLocation(headers.get(OKAPI_URL), INVOICE_LINES_PREFIX + invoiceLine.getId(), invoiceLine));
})
.onFailure(f -> {
log.error("Error occurred while creating a new invoiceLine with id: {}", invoiceLine.getId(), f);
asyncResultHandler.handle(buildErrorResponse(f));
});
});
} catch (Exception e) {
log.error("Error occurred while creating a new invoiceLine with id: {}", invoiceLine.getId(), e);
asyncResultHandler.handle(buildErrorResponse(
new HttpException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase())
));
}
}

public void updateInvoiceLine(String id, InvoiceLine invoiceLine, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
try {
vertxContext.runOnContext(v -> {
log.info("updateInvoiceLine:: Updating invoice line with id: {}", id);
new DBClient(vertxContext, okapiHeaders).getPgClient()
.withTrans(conn -> invoiceLinesDAO.updateInvoiceLine(id, invoiceLine, conn))
.onSuccess(s -> {
log.info("updateInvoiceLine:: Successfully updated invoice line with id: {}", id);
asyncResultHandler.handle(buildNoContentResponse());
})
.onFailure(f -> {
log.error("Error occurred while updating invoice line with id: {}", id, f);
asyncResultHandler.handle(buildErrorResponse(f));
});
});
} catch (Exception e) {
log.error("Error occurred while updating invoice line with id: {}", id, e);
asyncResultHandler.handle(buildErrorResponse(
new HttpException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase())
));
}
}

}
56 changes: 31 additions & 25 deletions src/main/java/org/folio/service/InvoiceStorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.folio.rest.impl.InvoiceStorageImpl.DOCUMENT_TABLE;
import static org.folio.rest.impl.InvoiceStorageImpl.INVOICE_ID_FIELD_NAME;
import static org.folio.rest.impl.InvoiceStorageImpl.INVOICE_PREFIX;
import static org.folio.rest.impl.InvoiceStorageImpl.INVOICE_TABLE;
import static org.folio.rest.utils.HelperUtils.combineCqlExpressions;
import static org.folio.rest.utils.ResponseUtils.buildOkResponse;
import static org.folio.rest.utils.ResponseUtils.buildErrorResponse;
Expand All @@ -17,48 +16,44 @@
import javax.ws.rs.core.Response;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.dao.invoice.InvoiceDAO;
import org.folio.rest.jaxrs.model.Document;
import org.folio.rest.jaxrs.model.DocumentCollection;
import org.folio.rest.jaxrs.model.Invoice;
import org.folio.rest.jaxrs.model.InvoiceDocument;
import org.folio.rest.jaxrs.resource.InvoiceStorage.GetInvoiceStorageInvoicesDocumentsByIdResponse;
import org.folio.rest.jaxrs.resource.InvoiceStorage.PutInvoiceStorageInvoicesByIdResponse;
import org.folio.rest.persist.DBClient;
import org.folio.rest.persist.PgUtil;

import io.vertx.core.AsyncResult;
import io.vertx.core.Context;
import io.vertx.core.Handler;
import io.vertx.ext.web.handler.HttpException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RequiredArgsConstructor
public class InvoiceStorageService {

private static final Logger log = LogManager.getLogger(InvoiceStorageService.class);
private static final String INVOICE_ID_MISMATCH_ERROR_MESSAGE = "Invoice id mismatch";

private final InvoiceDAO invoiceDAO;

public InvoiceStorageService(InvoiceDAO invoiceDAO) {
this.invoiceDAO = invoiceDAO;
}

public void postInvoiceStorageInvoices(Invoice invoice, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext, Map<String, String> headers) {
try {
vertxContext.runOnContext(v -> {
log.info("postInvoiceStorageInvoices:: Creating a new invoice by id: {}", invoice.getId());
new DBClient(vertxContext, headers).getPgClient()
.withTrans(conn -> invoiceDAO.createInvoice(invoice, conn))
.onComplete(reply -> {
if (reply.failed()) {
asyncResultHandler.handle(buildErrorResponse(reply.cause()));
return;
}
log.info("postInvoiceStorageInvoices:: Preparing response to client");
.onSuccess(s -> {
log.info("postInvoiceStorageInvoices:: Successfully created a new invoice by id: {}", invoice.getId());
asyncResultHandler.handle(buildResponseWithLocation(headers.get(OKAPI_URL), INVOICE_PREFIX + invoice.getId(), invoice));
})
.onFailure(f -> {
log.error("Error occurred while creating a new invoice with id: {}", invoice.getId(), f);
asyncResultHandler.handle(buildErrorResponse(f));
});
});
} catch (Exception e) {
Expand Down Expand Up @@ -102,17 +97,28 @@ public void deleteInvoiceStorageInvoicesById(String id, Handler<AsyncResult<Resp
}

public void putInvoiceStorageInvoicesById(String id, Invoice invoice, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
log.debug("putInvoiceStorageInvoicesById:: Updating invoice with id: {}", id);
PgUtil.put(INVOICE_TABLE, invoice, id, okapiHeaders, vertxContext,
PutInvoiceStorageInvoicesByIdResponse.class, reply -> {
if (reply.succeeded()) {
log.info("putInvoiceStorageInvoicesById:: Invoice with id: {} was successfully updated", id);
} else {
log.error("Error occurred while updating invoice with id: {}", id, reply.cause());
}
asyncResultHandler.handle(reply);
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
try {
vertxContext.runOnContext(v -> {
log.info("putInvoiceStorageInvoicesById:: Updating invoice with id: {}", id);
new DBClient(vertxContext, okapiHeaders).getPgClient()
.withTrans(conn -> invoiceDAO.updateInvoice(invoice, conn))
.onSuccess(s -> {
log.info("putInvoiceStorageInvoicesById:: Successfully updated invoice with id: {}", id);
asyncResultHandler.handle(buildNoContentResponse());
})
.onFailure(f -> {
log.error("Error occurred while updating invoice with id: {}", id, f);
asyncResultHandler.handle(buildErrorResponse(f));
});
});
} catch (Exception e) {
log.error("Error occurred while updating invoice with id: {}", invoice.getId(), e);
asyncResultHandler.handle(buildErrorResponse(
new HttpException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase())
));
}
}

public void getInvoiceStorageInvoicesDocumentsById(String id, int offset, int limit, String query,
Expand Down

0 comments on commit bda0c1c

Please sign in to comment.