Skip to content
Snippets Groups Projects
Commit e028824f authored by Raditss's avatar Raditss
Browse files

fixing voucher return

parent 3546ef0e
Branches
Tags
No related merge requests found
...@@ -43,7 +43,7 @@ INSERT INTO `api` (`api_key`) VALUES ...@@ -43,7 +43,7 @@ INSERT INTO `api` (`api_key`) VALUES
('toco_rest'), ('toco_rest'),
('toco_php'); ('toco_php');
CREATE TABLE `voucher`( CREATE TABLE `voucher_record`(
`vid` int(11) NOT NULL AUTO_INCREMENT, `vid` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(255) NOT NULL, `code` varchar(255) NOT NULL,
`user_id` int NOT NULL, `user_id` int NOT NULL,
......
...@@ -6,10 +6,12 @@ public class voucher_entity { ...@@ -6,10 +6,12 @@ public class voucher_entity {
private String code; private String code;
private Integer user_id; private Integer user_id;
private Integer amount; 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.code = code;
this.user_id = user_id; this.user_id = user_id;
this.amount = amount; this.amount = amount;
this.created_at = created_at;
} }
} }
...@@ -7,7 +7,7 @@ import org.toco.entity.voucher_entity; ...@@ -7,7 +7,7 @@ import org.toco.entity.voucher_entity;
public class voucher_model { public class voucher_model {
public void insert (voucher_entity voucher_entity) { 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() ; try (Connection connection = connector.connect() ;
PreparedStatement command = connection.prepareStatement(sql)) { PreparedStatement command = connection.prepareStatement(sql)) {
command.setString(1, voucher_entity.getCode()); command.setString(1, voucher_entity.getCode());
...@@ -20,24 +20,24 @@ public class voucher_model { ...@@ -20,24 +20,24 @@ public class voucher_model {
} }
public voucher_entity [] getAllVouchers () { public voucher_entity [] getAllVouchers () {
String sql = "SELECT * FROM voucher"; String sql = "SELECT * FROM voucher_record";
try (Connection connection = connector.connect() ; try (Connection connection = connector.connect() ;
PreparedStatement command = connection.prepareStatement(sql)) { PreparedStatement command = connection.prepareStatement(sql)) {
ResultSet result = command.executeQuery(); ResultSet result = command.executeQuery();
voucher_entity [] voucher_entity = new voucher_entity[getAllCount()]; voucher_entity [] voucher_entity = new voucher_entity[getAllCount()];
int i = 0; int i = 0;
while (result.next()) { 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++; i++;
} }
return voucher_entity; return voucher_entity;
} catch (SQLException exception) { } catch (SQLException exception) {
throw new RuntimeException("Error when inserting", exception); throw new RuntimeException("Error when getting", exception);
} }
} }
public voucher_entity[] getSpecifiedVoucher(String code){ 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() ; try (Connection connection = connector.connect() ;
PreparedStatement command = connection.prepareStatement(sql)) { PreparedStatement command = connection.prepareStatement(sql)) {
command.setString(1, code); command.setString(1, code);
...@@ -45,17 +45,17 @@ public class voucher_model { ...@@ -45,17 +45,17 @@ public class voucher_model {
voucher_entity [] voucher_entity = new voucher_entity[getSpecifiedCount(code)]; voucher_entity [] voucher_entity = new voucher_entity[getSpecifiedCount(code)];
int i = 0; int i = 0;
while (result.next()) { 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++; i++;
} }
return voucher_entity; return voucher_entity;
} catch (SQLException exception) { } catch (SQLException exception) {
throw new RuntimeException("Error when inserting", exception); throw new RuntimeException("Error when getting", exception);
} }
} }
public Integer getSpecifiedCount(String code){ 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() ; try (Connection connection = connector.connect() ;
PreparedStatement command = connection.prepareStatement(sql)) { PreparedStatement command = connection.prepareStatement(sql)) {
command.setString(1, code); command.setString(1, code);
...@@ -67,12 +67,12 @@ public class voucher_model { ...@@ -67,12 +67,12 @@ public class voucher_model {
return 0; return 0;
} }
} catch (SQLException exception) { } catch (SQLException exception) {
throw new RuntimeException("Error when inserting", exception); throw new RuntimeException("Error when counting", exception);
} }
} }
public Integer getAllCount(){ public Integer getAllCount(){
String sql = "SELECT COUNT(*) FROM voucher"; String sql = "SELECT COUNT(*) FROM voucher_record";
try (Connection connection = connector.connect() ; try (Connection connection = connector.connect() ;
PreparedStatement command = connection.prepareStatement(sql)) { PreparedStatement command = connection.prepareStatement(sql)) {
ResultSet result = command.executeQuery(); ResultSet result = command.executeQuery();
...@@ -83,7 +83,7 @@ public class voucher_model { ...@@ -83,7 +83,7 @@ public class voucher_model {
return 0; return 0;
} }
} catch (SQLException exception) { } catch (SQLException exception) {
throw new RuntimeException("Error when inserting", exception); throw new RuntimeException("Error when counting", exception);
} }
} }
......
...@@ -111,7 +111,7 @@ public class toco_service_impl implements toco_service { ...@@ -111,7 +111,7 @@ public class toco_service_impl implements toco_service {
voucher_model voucherModel = new voucher_model(); voucher_model voucherModel = new voucher_model();
userGems_model userGemsModel = new userGems_model(); userGems_model userGemsModel = new userGems_model();
transaction_model transactionModel = new transaction_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); voucherModel.insert(voucherEntity);
transactionModel.insert(new transaction_entity(user_id, amount, "voucher", "accepted", "0")); transactionModel.insert(new transaction_entity(user_id, amount, "voucher", "accepted", "0"));
if (userGemsModel.checkUser(user_id)) { if (userGemsModel.checkUser(user_id)) {
...@@ -137,7 +137,7 @@ public class toco_service_impl implements toco_service { ...@@ -137,7 +137,7 @@ public class toco_service_impl implements toco_service {
Integer len = voucherModel.getSpecifiedCount(code); Integer len = voucherModel.getSpecifiedCount(code);
String[] ret = new String[len]; String[] ret = new String[len];
for (int i = 0; i < len; i++) { 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; return ret;
} }
...@@ -155,7 +155,7 @@ public class toco_service_impl implements toco_service { ...@@ -155,7 +155,7 @@ public class toco_service_impl implements toco_service {
Integer len = voucherModel.getAllCount(); Integer len = voucherModel.getAllCount();
String[] ret = new String[len]; String[] ret = new String[len];
for (int i = 0; i < len; i++) { 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; return ret;
} }
......
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