Update wallet implementation & resource API
This commit is contained in:
parent
892afd8efb
commit
9f20f92c02
8
sql/sequence_session_script.sql
Normal file
8
sql/sequence_session_script.sql
Normal file
|
@ -0,0 +1,8 @@
|
|||
-- SEQUENCE: public."T_SESSION_session_id_seq"
|
||||
|
||||
-- DROP SEQUENCE public."T_SESSION_session_id_seq";
|
||||
|
||||
CREATE SEQUENCE public."t_session_id_seq";
|
||||
|
||||
ALTER SEQUENCE public."t_session_id_seq"
|
||||
OWNER TO postgres;
|
19
sql/table_session_script.sql
Normal file
19
sql/table_session_script.sql
Normal file
|
@ -0,0 +1,19 @@
|
|||
-- Table: public."T_SESSION"
|
||||
|
||||
-- DROP TABLE public."T_SESSION";
|
||||
|
||||
CREATE TABLE public."T_SESSION"
|
||||
(
|
||||
session_id integer NOT NULL DEFAULT nextval('"t_session_id_seq"'::regclass),
|
||||
user_id bigint NOT NULL,
|
||||
start_session bigint NOT NULL,
|
||||
end_session bigint NOT NULL,
|
||||
CONSTRAINT "T_SESSION_pkey" PRIMARY KEY (session_id)
|
||||
)
|
||||
WITH (
|
||||
OIDS = FALSE
|
||||
)
|
||||
TABLESPACE pg_default;
|
||||
|
||||
ALTER TABLE public."T_SESSION"
|
||||
OWNER to postgres;
|
|
@ -8,7 +8,7 @@ CREATE TABLE public."T_WALLET"
|
|||
wallet_hash character varying(255) COLLATE pg_catalog."default" NOT NULL,
|
||||
user_hash character varying(255) COLLATE pg_catalog."default" NOT NULL,
|
||||
type character varying(255) COLLATE pg_catalog."default" NOT NULL,
|
||||
sold numeric(255,5) NOT NULL,
|
||||
balance numeric(255,5) NOT NULL,
|
||||
creation_date bigint NOT NULL,
|
||||
modification_date bigint NOT NULL,
|
||||
is_active boolean NOT NULL,
|
||||
|
|
|
@ -15,7 +15,7 @@ public class Wallet {
|
|||
@DatabaseField(canBeNull = false)
|
||||
private String type;
|
||||
@DatabaseField(canBeNull = false)
|
||||
private Double sold;
|
||||
private Double balance;
|
||||
@DatabaseField(canBeNull = false)
|
||||
private long creation_date;
|
||||
@DatabaseField(canBeNull = false)
|
||||
|
@ -40,11 +40,11 @@ public class Wallet {
|
|||
this.is_active = isActive;
|
||||
}
|
||||
|
||||
public Wallet(String wallet_hash, String user_hash, String type, Double sold, boolean isActive) {
|
||||
public Wallet(String wallet_hash, String user_hash, String type, Double balance, boolean isActive) {
|
||||
this.wallet_hash = wallet_hash;
|
||||
this.user_hash = user_hash;
|
||||
this.type = type;
|
||||
this.sold = sold;
|
||||
this.balance = balance;
|
||||
this.is_active = isActive;
|
||||
}
|
||||
|
||||
|
@ -77,11 +77,11 @@ public class Wallet {
|
|||
this.type = type;
|
||||
}
|
||||
|
||||
public Double getSold() {
|
||||
return sold;
|
||||
public Double getBalance() {
|
||||
return balance;
|
||||
}
|
||||
public void setSold(Double sold) {
|
||||
this.sold = sold;
|
||||
public void setBalance(Double sold) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public boolean is_active() {
|
||||
|
@ -110,6 +110,6 @@ public class Wallet {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{wallet_hash:"+wallet_hash+",user_hash:"+user_hash+",type:"+type+",sold:"+sold+"}";
|
||||
return "{wallet_hash:"+wallet_hash+",user_hash:"+user_hash+",type:"+type+",balance:"+balance+"}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,14 @@ public class WalletDao {
|
|||
return walletDao.queryForFirst(preparedQuery);
|
||||
}
|
||||
|
||||
public void updateWalletBalance(String walletHash, double newBalance) throws Exception {
|
||||
walletDao = createWalletDaoConnection();
|
||||
UpdateBuilder<Wallet, String> updateBuilder = walletDao.updateBuilder();
|
||||
updateBuilder.updateColumnValue("balance",newBalance);
|
||||
updateBuilder.where().eq("wallet_hash",walletHash);
|
||||
updateBuilder.update();
|
||||
}
|
||||
|
||||
public List<Wallet> getUserWallet(String userHash) throws Exception {
|
||||
walletDao = createWalletDaoConnection();
|
||||
QueryBuilder<Wallet, String> queryBuilder = walletDao.queryBuilder();
|
||||
|
@ -43,7 +51,7 @@ public class WalletDao {
|
|||
return walletDao.query(preparedQuery);
|
||||
}
|
||||
|
||||
public List<Wallet> getOldUserWallets(String userHash) throws Exception{
|
||||
public List<Wallet> getUserOldWallets(String userHash) throws Exception{
|
||||
walletDao = createWalletDaoConnection();
|
||||
QueryBuilder<Wallet, String> queryBuilder = walletDao.queryBuilder();
|
||||
queryBuilder.where().eq("user_hash",userHash).and().eq("is_active",false);
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.util.List;
|
|||
|
||||
public class BlockchainQueryImplementation {
|
||||
|
||||
/*
|
||||
public Double getWalletSold(String walletHash){
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
Double sold = 0.0;
|
||||
|
@ -63,7 +64,7 @@ public class BlockchainQueryImplementation {
|
|||
return wallets;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
public String sendTransaction(String sourceWallet, String destinationWallet, Double amount){
|
||||
TransactionWrapper transactionWrapper = new TransactionWrapper();
|
||||
BlockEvent.TransactionEvent transactionEvent = transactionWrapper.sendTransaction("transaction",new String[]{sourceWallet,destinationWallet,amount.toString()});
|
||||
|
|
|
@ -8,6 +8,7 @@ import java.util.List;
|
|||
|
||||
public class DatabaseTransactionImplementation {
|
||||
|
||||
/*
|
||||
public void saveTransaction(Transaction transaction) throws Exception{
|
||||
TransactionDao transactionDao = new TransactionDao();
|
||||
long now = Instant.now().toEpochMilli();
|
||||
|
@ -44,5 +45,6 @@ public class DatabaseTransactionImplementation {
|
|||
TransactionDao transactionDao = new TransactionDao();
|
||||
return transactionDao.getUserReceivedTransaction(userHash);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
|
|
@ -36,10 +36,10 @@ public class DatabaseUserImplementation {
|
|||
return response;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
|
||||
|
||||
public User getUser(String email, String password) throws Exception{
|
||||
UserDao userDao = new UserDao();
|
||||
User user1 = userDao.getUser(email);
|
||||
|
@ -54,9 +54,9 @@ public class DatabaseUserImplementation {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
|
||||
public Map<String,String> userLogger(User user) throws Exception {
|
||||
UserDao userDao = new UserDao();
|
||||
Map<String,String> response = new HashMap<String, String>();
|
||||
|
@ -76,9 +76,9 @@ public class DatabaseUserImplementation {
|
|||
}
|
||||
return response;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
|
||||
private String hashPassword(String plainTextPassword){
|
||||
return BCrypt.hashpw(plainTextPassword,BCrypt.gensalt());
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@ import javax.json.JsonReader;
|
|||
import java.io.StringReader;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class WalletImplementation {
|
||||
|
@ -32,7 +31,7 @@ public class WalletImplementation {
|
|||
|
||||
transactionWrapper.sendTransaction("initWallet",new String[]{wallet.getWallet_hash(),wallet.getType(),wallet.getUser_hash()}); //Create Wallet in Blockchain
|
||||
|
||||
wallet.setSold(0.0);
|
||||
wallet.setBalance(0.0);
|
||||
wallet.setIs_active(true);
|
||||
long now = Instant.now().toEpochMilli();
|
||||
wallet.setCreation_date(now);
|
||||
|
@ -42,7 +41,7 @@ public class WalletImplementation {
|
|||
|
||||
response.put("walletHash",wallet.getWallet_hash());
|
||||
response.put("walletType",wallet.getType());
|
||||
response.put("walletSold",wallet.getSold().toString());
|
||||
response.put("walletSold",wallet.getBalance().toString());
|
||||
response.put("response","true");
|
||||
|
||||
return response;
|
||||
|
@ -73,18 +72,41 @@ public class WalletImplementation {
|
|||
wallet.setWallet_hash(walletJson.getString("id"));
|
||||
wallet.setUser_hash(walletJson.getString("owner"));
|
||||
wallet.setType(walletJson.getString("walletType"));
|
||||
wallet.setSold(walletJson.getJsonNumber("sold").doubleValue());
|
||||
wallet.setBalance(walletJson.getJsonNumber("sold").doubleValue());
|
||||
}
|
||||
return wallet;
|
||||
}
|
||||
|
||||
//setSold
|
||||
public void setSoldToWallet(String walletHash, double balance) throws Exception {
|
||||
TransactionWrapper transactionWrapper = new TransactionWrapper();
|
||||
transactionWrapper.sendTransaction("setSoldOnWallet",new String[]{walletHash,String.valueOf(balance)});
|
||||
|
||||
WalletDao walletDao = new WalletDao();
|
||||
walletDao.updateWalletBalance(walletHash,balance);
|
||||
}
|
||||
|
||||
//addMoney
|
||||
//TODO
|
||||
|
||||
//transfer
|
||||
public void transferWallet(String walletHash, String newUserHash) throws Exception {
|
||||
TransactionWrapper transactionWrapper = new TransactionWrapper();
|
||||
transactionWrapper.sendTransaction("transferWallet",new String[]{walletHash,newUserHash});
|
||||
|
||||
WalletDao walletDao = new WalletDao();
|
||||
walletDao.transferWallet(walletHash,newUserHash);
|
||||
}
|
||||
|
||||
|
||||
//delete
|
||||
public void deleteWallet(String walletHash) throws Exception {
|
||||
TransactionWrapper transactionWrapper = new TransactionWrapper();
|
||||
transactionWrapper.sendTransaction("deleteWallet",new String[]{walletHash});
|
||||
|
||||
WalletDao walletDao = new WalletDao();
|
||||
walletDao.deleteWallet(walletHash);
|
||||
}
|
||||
|
||||
|
||||
private String generateWalletHash(String walletBuilderString){
|
||||
|
|
|
@ -12,7 +12,7 @@ import javax.validation.Valid;
|
|||
@RequestMapping(value = "/api/rest/query")
|
||||
public class BlockchainQueryResource {
|
||||
|
||||
|
||||
/*
|
||||
@RequestMapping(value = "/balance", method = RequestMethod.GET ,params = {"walletHash"},produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity<StringResponse> getUserBalance(@RequestParam(value = "walletHash") String walletHash){
|
||||
|
@ -30,7 +30,7 @@ public class BlockchainQueryResource {
|
|||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
@RequestMapping(value = "/registerUser", method = RequestMethod.POST, produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity<StringResponse> registerUser(@Valid @RequestBody User user){
|
||||
|
|
|
@ -12,7 +12,7 @@ import java.util.List;
|
|||
@RestController
|
||||
@RequestMapping(value = "/api/rest/transaction")
|
||||
public class DatabaseTransactionResource {
|
||||
|
||||
/*
|
||||
@PostMapping("/save")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public ResponseEntity<String> saveTransaction(@Valid @RequestBody Transaction transaction){
|
||||
|
@ -105,6 +105,7 @@ public class DatabaseTransactionResource {
|
|||
return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -42,9 +42,9 @@ public class DatabaseUserResource {
|
|||
return ResponseEntity.status(HttpStatus.CONFLICT).body(responseS);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
|
||||
@RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity<StringResponse> login(@Valid @RequestBody User user){
|
||||
|
@ -78,9 +78,9 @@ public class DatabaseUserResource {
|
|||
return ResponseEntity.status(HttpStatus.CONFLICT).body(responseS);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
|
||||
@PostMapping(value = "/get", produces = "application/json")
|
||||
@ResponseBody
|
||||
public ResponseEntity<User> getUser(@RequestBody User user){
|
||||
|
@ -99,9 +99,9 @@ public class DatabaseUserResource {
|
|||
return new ResponseEntity(e.getMessage(), HttpStatus.CONFLICT);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
|
||||
@PostMapping(value = "/update")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity updateUser(@RequestBody User user){
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package restService;
|
||||
|
||||
import database.Wallet.Wallet;
|
||||
import database.transaction.Transaction;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -24,7 +23,7 @@ public class WalletResource {
|
|||
|
||||
Wallet returnWallet = new Wallet();
|
||||
returnWallet.setWallet_hash(walletResponse.get("walletHash"));
|
||||
returnWallet.setSold(Double.parseDouble(walletResponse.get("walletSold")));
|
||||
returnWallet.setBalance(Double.parseDouble(walletResponse.get("walletSold")));
|
||||
returnWallet.setType(walletResponse.get("walletType"));
|
||||
|
||||
if(Boolean.parseBoolean(walletResponse.get("response"))){
|
||||
|
@ -72,4 +71,48 @@ public class WalletResource {
|
|||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/setBalance", method = RequestMethod.POST,produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity setBalance(@RequestBody Wallet wallet){
|
||||
WalletImplementation walletImplementation = new WalletImplementation();
|
||||
try{
|
||||
walletImplementation.setSoldToWallet(wallet.getWallet_hash(),wallet.getBalance());
|
||||
return ResponseEntity.status(HttpStatus.OK).body("{\"response\":\"ok\"}");
|
||||
} catch (Exception e){
|
||||
StringResponse responseS = new StringResponse(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/transfer", method = RequestMethod.POST, produces = "application/json")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public ResponseEntity transferWallet(@RequestBody Wallet wallet){
|
||||
WalletImplementation walletImplementation = new WalletImplementation();
|
||||
try {
|
||||
walletImplementation.transferWallet(wallet.getWallet_hash(),wallet.getUser_hash());
|
||||
return ResponseEntity.status(HttpStatus.OK).body("{\"response\":\"ok\"}");
|
||||
} catch (Exception e){
|
||||
StringResponse responseS = new StringResponse(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/delete/{walletHash}", method = RequestMethod.DELETE)
|
||||
public ResponseEntity deleteWallet(@PathVariable("walletHash") String walletHash){
|
||||
WalletImplementation walletImplementation = new WalletImplementation();
|
||||
try{
|
||||
walletImplementation.deleteWallet(walletHash);
|
||||
return ResponseEntity.status(HttpStatus.OK).body("{\"response\":\"ok\"}");
|
||||
}catch (Exception e){
|
||||
StringResponse responseS = new StringResponse(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue