Skip to content
Snippets Groups Projects
Commit 9b5986d4 authored by BEAUVAIS ANTOINE's avatar BEAUVAIS ANTOINE
Browse files

Added handling of POST SubmitTransaction calls.

parent 45bc912e
1 merge request!1Merge 'develop' changes into 'master'.
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
...@@ -19,8 +19,9 @@ public interface ISubmitTransactionController { ...@@ -19,8 +19,9 @@ public interface ISubmitTransactionController {
* @param request the HTTP Servlet Request, provided by Spring Web * @param request the HTTP Servlet Request, provided by Spring Web
* @param response the HTTP Servlet Response, provided by Spring Web * @param response the HTTP Servlet Response, provided by Spring Web
* @return the response served to the user. * @return the response served to the user.
* @throws java.lang.Exception when the request cannot be served.
*/ */
public ResponseEntity<Object> submitTransaction(HttpServletRequest request, public ResponseEntity<Object> submitTransaction(HttpServletRequest request,
HttpServletResponse response); HttpServletResponse response) throws Exception;
} }
...@@ -4,10 +4,18 @@ ...@@ -4,10 +4,18 @@
*/ */
package fr.unistra.sil.erp.back.controller.api; package fr.unistra.sil.erp.back.controller.api;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import static fr.unistra.sil.erp.back.Config.MAPPING_SUBTRANSAC; import static fr.unistra.sil.erp.back.Config.MAPPING_SUBTRANSAC;
import fr.unistra.sil.erp.back.controller.ISubmitTransactionController; import fr.unistra.sil.erp.back.controller.ISubmitTransactionController;
import fr.unistra.sil.erp.back.model.Transaction;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
...@@ -22,10 +30,44 @@ public class ApiSubmitTransactionController ...@@ -22,10 +30,44 @@ public class ApiSubmitTransactionController
implements ISubmitTransactionController { implements ISubmitTransactionController {
@RequestMapping(value=MAPPING_SUBTRANSAC, method = RequestMethod.POST) @RequestMapping(value=MAPPING_SUBTRANSAC, method = RequestMethod.POST)
@Override
public ResponseEntity<Object> submitTransaction(HttpServletRequest request, public ResponseEntity<Object> submitTransaction(HttpServletRequest request,
HttpServletResponse response) HttpServletResponse response) throws ApiBadRequestException
{ {
throw new UnsupportedOperationException("Not yet supported."); Gson gson = new Gson();
String body;
try {
body = request.getReader().lines()
.collect(Collectors.joining(System.lineSeparator()));
} catch (IOException ex) {
Logger.getLogger(ApiSubmitTransactionController.class.getName())
.log(Level.SEVERE, "Unparseable body.", ex);
throw new ApiBadRequestException("Unparseable body.");
}
if(body == null)
throw new ApiBadRequestException("Missing JSON body.");
Transaction t;
try
{
t = gson.fromJson(body, Transaction.class);
}
catch(JsonParseException ex)
{
throw new ApiBadRequestException("Invalid JSON: syntax error.");
}
if(t == null)
throw new ApiBadRequestException("Missing JSON body.");
if(t.getItem() == null || t.getType() == null ||
t.getAmount() == null)
throw new ApiBadRequestException("Invalid JSON schema.");
System.out.println("Transaction : " + t.getItem() +
t.getType() + " " + t.getAmount());
return new ResponseEntity<>(t, HttpStatus.CREATED);
} }
} }
...@@ -16,19 +16,19 @@ public class RegistryEntry { ...@@ -16,19 +16,19 @@ public class RegistryEntry {
private final int id; private final int id;
private final int transactionId; private final int transactionId;
private final LocalDateTime dt; private final LocalDateTime dt;
private final String description; private final int accountId;
private final BigDecimal debit; private final BigDecimal debit;
private final BigDecimal credit; private final BigDecimal credit;
private final String remarks; private final String remarks;
public RegistryEntry(int id, int transactionId, LocalDateTime dt, public RegistryEntry(int id, int transactionId, LocalDateTime dt,
String description, BigDecimal debit, BigDecimal credit, int accountId, BigDecimal debit, BigDecimal credit,
String remarks) String remarks)
{ {
this.id = id; this.id = id;
this.transactionId = transactionId; this.transactionId = transactionId;
this.dt = dt; this.dt = dt;
this.description = description; this.accountId = accountId;
this.debit = debit; this.debit = debit;
this.credit = credit; this.credit = credit;
this.remarks = remarks; this.remarks = remarks;
...@@ -49,9 +49,9 @@ public class RegistryEntry { ...@@ -49,9 +49,9 @@ public class RegistryEntry {
return this.dt; return this.dt;
} }
public String getDescription() public int getAccountId()
{ {
return this.description; return this.accountId;
} }
public BigDecimal getDebit() public BigDecimal getDebit()
......
/*
* CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B
* https://cecill.info/licences/Licence_CeCILL-B_V1-fr.html
*/
package fr.unistra.sil.erp.back.model;
import java.math.BigDecimal;
/**
* Representation of a transaction.
* @author BEAUVAIS ANTOINE
*/
public class Transaction {
private final Integer item;
private final Integer type;
private final BigDecimal amount;
public Transaction(Integer item, Integer type, BigDecimal amount)
{
this.item = item;
this.type = type;
this.amount = amount;
}
public Integer getItem()
{
return this.item;
}
public Integer getType()
{
return this.type;
}
public BigDecimal getAmount()
{
return this.amount;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment