From e028824faf4fcb1d9a0e1097e94e0ec2ee513411 Mon Sep 17 00:00:00 2001
From: Raditss <raditya.naufal.a@gmail.com>
Date: Fri, 17 Nov 2023 01:10:05 +0700
Subject: [PATCH] fixing voucher return

---
 db/tocosoap.sql                               |  2 +-
 .../java/org/toco/entity/voucher_entity.java  |  4 +++-
 .../java/org/toco/model/voucher_model.java    | 22 +++++++++----------
 .../org/toco/service/toco_service_impl.java   |  6 ++---
 4 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/db/tocosoap.sql b/db/tocosoap.sql
index 4faaedb..1bc5e5c 100644
--- a/db/tocosoap.sql
+++ b/db/tocosoap.sql
@@ -43,7 +43,7 @@ INSERT INTO `api` (`api_key`) VALUES
     ('toco_rest'),
     ('toco_php');
 
-CREATE TABLE `voucher`(
+CREATE TABLE `voucher_record`(
     `vid` int(11) NOT NULL AUTO_INCREMENT,
     `code` varchar(255) NOT NULL,
     `user_id` int NOT NULL,
diff --git a/src/main/java/org/toco/entity/voucher_entity.java b/src/main/java/org/toco/entity/voucher_entity.java
index f786a85..ddedd43 100644
--- a/src/main/java/org/toco/entity/voucher_entity.java
+++ b/src/main/java/org/toco/entity/voucher_entity.java
@@ -6,10 +6,12 @@ public class voucher_entity {
     private String code;
     private Integer user_id;
     private Integer amount;
+    private String created_at;
 
-    public voucher_entity(String code, Integer user_id, Integer amount) {
+    public voucher_entity(String code, Integer user_id, Integer amount, String created_at) {
         this.code = code;
         this.user_id = user_id;
         this.amount = amount;
+        this.created_at = created_at;
     }
 }
diff --git a/src/main/java/org/toco/model/voucher_model.java b/src/main/java/org/toco/model/voucher_model.java
index 4f5937a..d6935ab 100644
--- a/src/main/java/org/toco/model/voucher_model.java
+++ b/src/main/java/org/toco/model/voucher_model.java
@@ -7,7 +7,7 @@ import org.toco.entity.voucher_entity;
 
 public class voucher_model {
     public void insert (voucher_entity voucher_entity) {
-        String sql = "INSERT INTO voucher (code, user_id, amount) VALUES (?, ?, ?)";
+        String sql = "INSERT INTO voucher_record (code, user_id, amount) VALUES (?, ?, ?)";
         try (Connection connection = connector.connect() ;
              PreparedStatement command = connection.prepareStatement(sql)) {
             command.setString(1, voucher_entity.getCode());
@@ -20,24 +20,24 @@ public class voucher_model {
     }
 
     public voucher_entity [] getAllVouchers () {
-        String sql = "SELECT * FROM voucher";
+        String sql = "SELECT * FROM voucher_record";
         try (Connection connection = connector.connect() ;
              PreparedStatement command = connection.prepareStatement(sql)) {
             ResultSet result = command.executeQuery();
             voucher_entity [] voucher_entity = new voucher_entity[getAllCount()];
             int i = 0;
             while (result.next()) {
-                voucher_entity[i] = new voucher_entity(result.getString("code"), result.getInt("user_id"), result.getInt("amount"));
+                voucher_entity[i] = new voucher_entity(result.getString("code"), result.getInt("user_id"), result.getInt("amount"), result.getString("timestamp"));
                 i++;
             }
             return voucher_entity;
         } catch (SQLException exception) {
-            throw new RuntimeException("Error when inserting", exception);
+            throw new RuntimeException("Error when getting", exception);
         }
     }
 
     public voucher_entity[] getSpecifiedVoucher(String code){
-        String sql = "SELECT * FROM voucher WHERE code = ?";
+        String sql = "SELECT * FROM voucher_record WHERE code = ?";
         try (Connection connection = connector.connect() ;
              PreparedStatement command = connection.prepareStatement(sql)) {
             command.setString(1, code);
@@ -45,17 +45,17 @@ public class voucher_model {
             voucher_entity [] voucher_entity = new voucher_entity[getSpecifiedCount(code)];
             int i = 0;
             while (result.next()) {
-                voucher_entity[i] = new voucher_entity(result.getString("code"), result.getInt("user_id"), result.getInt("amount"));
+                voucher_entity[i] = new voucher_entity(result.getString("code"), result.getInt("user_id"), result.getInt("amount"),result.getString("timestamp"));
                 i++;
             }
             return voucher_entity;
         } catch (SQLException exception) {
-            throw new RuntimeException("Error when inserting", exception);
+            throw new RuntimeException("Error when getting", exception);
         }
     }
 
     public Integer getSpecifiedCount(String code){
-        String sql = "SELECT COUNT(*) FROM voucher WHERE code = ?";
+        String sql = "SELECT COUNT(*) FROM voucher_record WHERE code = ?";
         try (Connection connection = connector.connect() ;
              PreparedStatement command = connection.prepareStatement(sql)) {
             command.setString(1, code);
@@ -67,12 +67,12 @@ public class voucher_model {
                 return 0;
             }
         } catch (SQLException exception) {
-            throw new RuntimeException("Error when inserting", exception);
+            throw new RuntimeException("Error when counting", exception);
         }
     }
 
     public Integer getAllCount(){
-        String sql = "SELECT COUNT(*) FROM voucher";
+        String sql = "SELECT COUNT(*) FROM voucher_record";
         try (Connection connection = connector.connect() ;
              PreparedStatement command = connection.prepareStatement(sql)) {
             ResultSet result = command.executeQuery();
@@ -83,7 +83,7 @@ public class voucher_model {
                 return 0;
             }
         } catch (SQLException exception) {
-            throw new RuntimeException("Error when inserting", exception);
+            throw new RuntimeException("Error when counting", exception);
         }
     }
 
diff --git a/src/main/java/org/toco/service/toco_service_impl.java b/src/main/java/org/toco/service/toco_service_impl.java
index c4277e6..665696c 100644
--- a/src/main/java/org/toco/service/toco_service_impl.java
+++ b/src/main/java/org/toco/service/toco_service_impl.java
@@ -111,7 +111,7 @@ public class toco_service_impl implements toco_service {
             voucher_model voucherModel = new voucher_model();
             userGems_model userGemsModel = new userGems_model();
             transaction_model transactionModel = new transaction_model();
-            voucher_entity voucherEntity = new voucher_entity(voucher,user_id,amount);
+            voucher_entity voucherEntity = new voucher_entity(voucher,user_id,amount,"0");
             voucherModel.insert(voucherEntity);
             transactionModel.insert(new transaction_entity(user_id, amount, "voucher", "accepted", "0"));
             if (userGemsModel.checkUser(user_id)) {
@@ -137,7 +137,7 @@ public class toco_service_impl implements toco_service {
             Integer len = voucherModel.getSpecifiedCount(code);
             String[] ret = new String[len];
             for (int i = 0; i < len; i++) {
-                ret[i] = voucherEntity[i].getCode()+", "+voucherEntity[i].getAmount().toString()+", "+voucherEntity[i].getUser_id().toString();
+                ret[i] = voucherEntity[i].getCode()+", "+voucherEntity[i].getAmount().toString()+", "+voucherEntity[i].getUser_id().toString()+", "+voucherEntity[i].getCreated_at();
             }
             return ret;
         }
@@ -155,7 +155,7 @@ public class toco_service_impl implements toco_service {
             Integer len = voucherModel.getAllCount();
             String[] ret = new String[len];
             for (int i = 0; i < len; i++) {
-                ret[i] = voucherEntity[i].getCode()+", "+voucherEntity[i].getAmount().toString()+", "+voucherEntity[i].getUser_id().toString();
+                ret[i] = voucherEntity[i].getCode()+", "+voucherEntity[i].getAmount().toString()+", "+voucherEntity[i].getUser_id().toString()+", "+voucherEntity[i].getCreated_at();
             }
             return ret;
         }
-- 
GitLab