From 9b5986d49d162f477c45f1e08d935ed387199962 Mon Sep 17 00:00:00 2001
From: BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
Date: Thu, 23 Sep 2021 18:35:12 +0200
Subject: [PATCH] Added handling of POST SubmitTransaction calls.

---
 .../ISubmitTransactionController.java         |  3 +-
 .../api/ApiSubmitTransactionController.java   | 46 ++++++++++++++++++-
 .../sil/erp/back/model/RegistryEntry.java     | 10 ++--
 .../sil/erp/back/model/Transaction.java       | 43 +++++++++++++++++
 4 files changed, 94 insertions(+), 8 deletions(-)
 create mode 100644 src/main/java/fr/unistra/sil/erp/back/model/Transaction.java

diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/ISubmitTransactionController.java b/src/main/java/fr/unistra/sil/erp/back/controller/ISubmitTransactionController.java
index 2f6ef53..e71ba5e 100644
--- a/src/main/java/fr/unistra/sil/erp/back/controller/ISubmitTransactionController.java
+++ b/src/main/java/fr/unistra/sil/erp/back/controller/ISubmitTransactionController.java
@@ -19,8 +19,9 @@ public interface ISubmitTransactionController {
      * @param request the HTTP Servlet Request, provided by Spring Web
      * @param response the HTTP Servlet Response, provided by Spring Web
      * @return the response served to the user.
+     * @throws java.lang.Exception when the request cannot be served.
      */
     public ResponseEntity<Object> submitTransaction(HttpServletRequest request,
-            HttpServletResponse response);
+            HttpServletResponse response) throws Exception;
     
 }
diff --git a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiSubmitTransactionController.java b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiSubmitTransactionController.java
index 0074b6c..a249869 100644
--- a/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiSubmitTransactionController.java
+++ b/src/main/java/fr/unistra/sil/erp/back/controller/api/ApiSubmitTransactionController.java
@@ -4,10 +4,18 @@
  */
 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 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.HttpServletResponse;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -22,10 +30,44 @@ public class ApiSubmitTransactionController
         implements ISubmitTransactionController {
     
     @RequestMapping(value=MAPPING_SUBTRANSAC, method = RequestMethod.POST)
+    @Override
     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);
     }
     
 }
diff --git a/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java b/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java
index 1bd908b..8cb06fa 100644
--- a/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java
+++ b/src/main/java/fr/unistra/sil/erp/back/model/RegistryEntry.java
@@ -16,19 +16,19 @@ public class RegistryEntry {
     private final int id;
     private final int transactionId;
     private final LocalDateTime dt;
-    private final String description;
+    private final int accountId;
     private final BigDecimal debit;
     private final BigDecimal credit;
     private final String remarks;
     
     public RegistryEntry(int id, int transactionId, LocalDateTime dt,
-            String description, BigDecimal debit, BigDecimal credit,
+            int accountId, BigDecimal debit, BigDecimal credit,
             String remarks)
     {
         this.id = id;
         this.transactionId = transactionId;
         this.dt = dt;
-        this.description = description;
+        this.accountId = accountId;
         this.debit = debit;
         this.credit = credit;
         this.remarks = remarks;
@@ -49,9 +49,9 @@ public class RegistryEntry {
         return this.dt;
     }
     
-    public String getDescription()
+    public int getAccountId()
     {
-        return this.description;
+        return this.accountId;
     }
     
     public BigDecimal getDebit()
diff --git a/src/main/java/fr/unistra/sil/erp/back/model/Transaction.java b/src/main/java/fr/unistra/sil/erp/back/model/Transaction.java
new file mode 100644
index 0000000..19dae4e
--- /dev/null
+++ b/src/main/java/fr/unistra/sil/erp/back/model/Transaction.java
@@ -0,0 +1,43 @@
+/*
+ * 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;
+    }
+    
+}
-- 
GitLab