-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MODINVOSTO-187] Refactor invoice update and invoice line create/update
- Loading branch information
1 parent
c14110d
commit bda0c1c
Showing
5 changed files
with
145 additions
and
35 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
79 changes: 79 additions & 0 deletions
79
src/main/java/org/folio/service/InvoiceLineStorageService.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,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()) | ||
)); | ||
} | ||
} | ||
|
||
} |
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