From 5fa3ccf43e01d9f04d776d0aef08eec77863b977 Mon Sep 17 00:00:00 2001
From: BEAUVAIS ANTOINE <antoine.beauvais@etu.unistra.fr>
Date: Tue, 21 Sep 2021 14:06:38 +0200
Subject: [PATCH] Added categories in DB and API.

---
 scripts/create-dev.sql                        | 21 +++++++++++++++----
 scripts/generate-dev.bat                      | 21 +++++++++++++++++++
 .../unistra/sil/erp/back/api/model/Item.java  | 18 +++++++++++++++-
 .../sil/erp/back/db/DatabaseSQLiteImpl.java   |  5 +++--
 4 files changed, 58 insertions(+), 7 deletions(-)
 create mode 100644 scripts/generate-dev.bat

diff --git a/scripts/create-dev.sql b/scripts/create-dev.sql
index f549a12..b0f01f6 100644
--- a/scripts/create-dev.sql
+++ b/scripts/create-dev.sql
@@ -1,12 +1,25 @@
 DROP TABLE IF EXISTS items;
+DROP TABLE IF EXISTS categories;
+
+CREATE TABLE categories (
+    id INTEGER PRIMARY KEY,
+	name TEXT NOT NULL
+);
 
 CREATE TABLE items (
     ref INTEGER PRIMARY KEY,
     name TEXT NOT NULL,
     price REAL NOT NULL,
-    subscriberPrice REAL NOT NULL
+    subscriberPrice REAL NOT NULL,
+	category INTEGER NOT NULL,
+	FOREIGN KEY (category) REFERENCES categories (id)
 );
 
-INSERT INTO items (ref, name, price, subscriberPrice)
-    VALUES (1, "Jus d'orange", 1.00, 0.50),
-           (2, "Coca-Cola",    1.50, 1.00);
+INSERT INTO categories (id, name)
+    VALUES (1, "Boissons"),
+	       (2, "Snacks");
+
+INSERT INTO items (ref, name, price, subscriberPrice, category)
+    VALUES (1, "Jus d'orange", 1.00, 0.50, 1),
+           (2, "Coca-Cola",    1.50, 1.00, 1),
+		   (3, "Twix",         1.10, 0.80, 2);
diff --git a/scripts/generate-dev.bat b/scripts/generate-dev.bat
new file mode 100644
index 0000000..5734b14
--- /dev/null
+++ b/scripts/generate-dev.bat
@@ -0,0 +1,21 @@
+@ECHO OFF
+WHERE sqlite3 > NUL
+
+IF %ERRORLEVEL% NEQ 0 GOTO :installsqlite
+
+sqlite3 dev.db ".read scripts/create-dev.sql"
+
+IF %ERRORLEVEL% NEQ 0 GOTO :errorProcess
+
+GOTO :end
+
+:installsqlite
+ECHO SQLite3 not found.
+ECHO.
+ECHO Make sure sqlite3.exe is in your PATH.
+GOTO :end
+
+:errorProcess
+ECHO SQLite3 failed to process the database file.
+
+:end
diff --git a/src/main/java/fr/unistra/sil/erp/back/api/model/Item.java b/src/main/java/fr/unistra/sil/erp/back/api/model/Item.java
index 6e7fb42..0ff8512 100644
--- a/src/main/java/fr/unistra/sil/erp/back/api/model/Item.java
+++ b/src/main/java/fr/unistra/sil/erp/back/api/model/Item.java
@@ -32,20 +32,27 @@ public class Item {
      */
     private final BigDecimal subscriberPrice;
     
+    /**
+     * This product's category identifier.
+     */
+    private final int category;
+    
     /**
      * Class constructor.
      * @param ref The item's reference number.
      * @param name The item's name.
      * @param price The item's price.
      * @param subscriberPrice The item's price for subscribers.
+     * @param category The item's category.
      */
     public Item(int ref, String name, BigDecimal price,
-            BigDecimal subscriberPrice)
+            BigDecimal subscriberPrice, int category)
     {
         this.ref = ref;
         this.name = name;
         this.price = price;
         this.subscriberPrice = subscriberPrice;
+        this.category = category;
     }
     
     /**
@@ -83,4 +90,13 @@ public class Item {
     {
         return this.subscriberPrice;
     }
+    
+    /**
+     * Returns the current item's category identifier.
+     * @return the category identifier.
+     */
+    public int getCategory()
+    {
+        return this.category;
+    }
 }
diff --git a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java
index 9a00463..f0d4f92 100644
--- a/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java
+++ b/src/main/java/fr/unistra/sil/erp/back/db/DatabaseSQLiteImpl.java
@@ -23,7 +23,7 @@ public class DatabaseSQLiteImpl implements DatabaseInterface {
     
     private static final String CONNECTION_URL = "jdbc:sqlite:dev.db";
     private static final String SQL_GETALLITEMS = 
-            "SELECT ref, name, price, subscriberPrice FROM items";
+            "SELECT ref, name, price, subscriberPrice, category FROM items";
     
     private Connection conn;
     
@@ -65,7 +65,8 @@ public class DatabaseSQLiteImpl implements DatabaseInterface {
             {
                 Item item = new Item(rs.getInt("ref"), rs.getString("name"),
                                      rs.getBigDecimal("price"),
-                                     rs.getBigDecimal("subscriberPrice"));
+                                     rs.getBigDecimal("subscriberPrice"),
+                                     rs.getInt("category"));
                 res.add(item);
             }
         } catch (SQLException ex) {
-- 
GitLab