From 380744350603de3100c195d47fdb6ed5f5c57d38 Mon Sep 17 00:00:00 2001 From: Surabhi Shreya Date: Fri, 8 Mar 2024 22:28:35 +0530 Subject: [PATCH 1/3] Create Wallet feature --- .../controller/WalletController.java | 45 +++++++-- .../unitylend/dao/WalletRepository.java | 95 ++++++++++++++++--- .../com/educare/unitylend/model/Wallet.java | 29 +----- .../service/impl/WalletServiceImpl.java | 45 +++++---- 4 files changed, 152 insertions(+), 62 deletions(-) diff --git a/src/main/java/com/educare/unitylend/controller/WalletController.java b/src/main/java/com/educare/unitylend/controller/WalletController.java index 9256c49..92ec99c 100644 --- a/src/main/java/com/educare/unitylend/controller/WalletController.java +++ b/src/main/java/com/educare/unitylend/controller/WalletController.java @@ -31,6 +31,7 @@ public class WalletController extends BaseController { public List getAllWallets() throws ControllerException { try { List walletList = walletService.getWallets(); + return walletList; } catch (Exception e) { log.error("Error encountered in getting the users"); @@ -38,21 +39,42 @@ public List getAllWallets() throws ControllerException { } } - @GetMapping("/get-user-info/{userId}") - public Wallet getWalletInfo(@PathVariable String userId) throws ControllerException{ + @GetMapping("/get-wallet-info/{userId}") + public ResponseEntity getWalletInfo(@PathVariable String userId) throws ControllerException { try { - return walletService.getWalletInfo(userId); + if (userId == null || userId.isEmpty()) { + return ResponseEntity.badRequest().body(null); + } + + Wallet currentWallet = walletService.getWalletInfo(userId); + + if (currentWallet == null) { + return ResponseEntity.notFound().build(); + } + + return ResponseEntity.ok(currentWallet); } catch (Exception e) { - log.error("Error encountered in getting the wallet info for the given user"); + log.error("Error encountered in getting the wallet info for the given user with userId: {}", userId, e); throw new ControllerException("Error encountered in getting the wallet info for the given user", e); } } + @GetMapping("get-wallet/{walletId}") public Wallet getWalletById(@PathVariable String walletId) throws ControllerException { try { - return walletService.getWalletById(walletId); + if (walletId == null || walletId.isEmpty()) { + return null; + } + + Wallet currentWallet = walletService.getWalletById(walletId); + + if (currentWallet == null) { + return null; + } + + return currentWallet; } catch (Exception e) { log.error("Error in fetching desired wallet's details"); throw new ControllerException("Error encountered in getting the wallet info for the given walletId", e); @@ -60,19 +82,28 @@ public Wallet getWalletById(@PathVariable String walletId) throws ControllerExce } @PostMapping("/{walletId}/addAmount") - public ResponseEntity addAmountToWallet(@PathVariable String walletId, @RequestParam Float amount) throws ControllerException{ + public ResponseEntity addAmountToWallet(@PathVariable String walletId, @RequestParam Float amount) throws ControllerException { try { + if (walletId == null || walletId.isEmpty()) { + return ResponseEntity.badRequest().body(null); + } + walletService.addAmountToWallet(walletId, amount); return ResponseEntity.ok("Amount added successfully to wallet"); } catch (Exception e) { - log.error("Error adding requested amount to wallet"); + log.error("Error adding requested amount to wallet", e); throw new ControllerException("Error encountered while adding requested amount to wallet", e); } } + @PostMapping("/{walletId}/debitAmount") public ResponseEntity debitFromWallet(@PathVariable String walletId, @RequestParam Float amount) throws ControllerException{ try { + if (walletId == null || walletId.isEmpty()) { + return ResponseEntity.badRequest().body(null); + } + walletService.debitFromWallet(walletId, amount); return ResponseEntity.ok("Amount debited successfully from wallet"); } catch (Exception e) { diff --git a/src/main/java/com/educare/unitylend/dao/WalletRepository.java b/src/main/java/com/educare/unitylend/dao/WalletRepository.java index 1a68519..606f93d 100644 --- a/src/main/java/com/educare/unitylend/dao/WalletRepository.java +++ b/src/main/java/com/educare/unitylend/dao/WalletRepository.java @@ -1,14 +1,9 @@ package com.educare.unitylend.dao; - import com.educare.unitylend.model.User; import com.educare.unitylend.model.Wallet; -import org.apache.ibatis.annotations.Insert; -import org.apache.ibatis.annotations.Mapper; -import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; -import org.apache.ibatis.annotations.Update; import org.springframework.stereotype.Repository; @@ -19,26 +14,102 @@ @Repository public interface WalletRepository { - @Select("select * from wallet") + static final String SELECT_WALLETS = "select * from wallet"; + + static String getWalletInfoQuery(String userId) { + StringBuilder sqlQueryBuilder = new StringBuilder(); + + sqlQueryBuilder.append("SELECT w.walletid, w.userid, w.balance FROM wallet w JOIN tempuser u ON w.userid = u.userid WHERE 1=1"); + if (userId != null) { + sqlQueryBuilder.append(" AND w.userid = '").append(userId).append("'"); + } + + return sqlQueryBuilder.toString(); + } + + + public static String generateWalletInTableQuery(String userId) { + StringBuilder sqlQueryBuilder = new StringBuilder(); + + sqlQueryBuilder.append("INSERT INTO wallet (walletid, balance, userid) VALUES (uuid_generate_v4(), 0, "); + if (userId != null) { + sqlQueryBuilder.append("'").append(userId).append("'"); + } else { + sqlQueryBuilder.append("NULL"); + } + sqlQueryBuilder.append(")"); + + return sqlQueryBuilder.toString(); + } + + public static String getUserIdWithWalletQuery(@Param("ex") Wallet ex) { + StringBuilder sqlQueryBuilder = new StringBuilder(); + + sqlQueryBuilder.append("SELECT u.* FROM tempuser u WHERE u.userid = (SELECT userid FROM wallet WHERE walletid = "); + if (ex != null && ex.getWalletid() != null) { + sqlQueryBuilder.append("'").append(ex.getWalletid()).append("'"); + } else { + sqlQueryBuilder.append("NULL"); + } + sqlQueryBuilder.append(")"); + + return sqlQueryBuilder.toString(); + } + + public static String getWalletByIdQuery(@Param("walletid") String walletid) { + StringBuilder sqlQueryBuilder = new StringBuilder(); + + sqlQueryBuilder.append("SELECT * FROM wallet WHERE walletid = "); + if (walletid != null) { + sqlQueryBuilder.append("'").append(walletid).append("'"); + } else { + sqlQueryBuilder.append("NULL"); + } + + return sqlQueryBuilder.toString(); + } + + public static String updateBalanceQuery(@Param("walletid") String walletid, @Param("balance") Float balance) { + StringBuilder sqlQueryBuilder = new StringBuilder(); + + sqlQueryBuilder.append("UPDATE wallet SET balance = "); + if (balance != null) { + sqlQueryBuilder.append(balance); + } else { + sqlQueryBuilder.append("NULL"); + } + sqlQueryBuilder.append(" WHERE walletid = "); + if (walletid != null) { + sqlQueryBuilder.append("'").append(walletid).append("'"); + } else { + sqlQueryBuilder.append("NULL"); + } + + return sqlQueryBuilder.toString(); + } + + + @Select(SELECT_WALLETS) List getAllWallets(); - @Select("SELECT w.walletid, w.userid, w.balance FROM wallet w JOIN tempuser u ON w.userid = u.userid WHERE w.userid = #{userId}") + @SelectProvider(type = WalletRepository.class, method = "getWalletInfoQuery") Wallet getWalletInfo(@Param("userId") String userId); - @Insert("INSERT INTO wallet (walletid, balance, userid) VALUES (uuid_generate_v4(), 0, #{userId})") + @InsertProvider(type = WalletRepository.class, method = "generateWalletInTableQuery") void generateWalletInTable(String userId); - @Select("SELECT u.* FROM tempuser u WHERE u.userid = (SELECT userid FROM wallet WHERE walletid = #{ex.walletid})") + @SelectProvider(type = WalletRepository.class, method = "getUserIdWithWalletQuery") User getUserIdWithWallet(@Param("ex") Wallet ex); - @Select("SELECT * FROM wallet WHERE walletid = #{walletid}") + @SelectProvider(type = WalletRepository.class, method = "getWalletByIdQuery") Wallet getWalletById(@Param("walletid") String walletid); - @Update("UPDATE wallet SET balance = #{balance} WHERE walletid = #{walletid}") + @UpdateProvider(type = WalletRepository.class, method = "updateBalanceQuery") void updateBalance(@Param("walletid") String walletid, @Param("balance") Float balance); } + diff --git a/src/main/java/com/educare/unitylend/model/Wallet.java b/src/main/java/com/educare/unitylend/model/Wallet.java index da14684..1377600 100644 --- a/src/main/java/com/educare/unitylend/model/Wallet.java +++ b/src/main/java/com/educare/unitylend/model/Wallet.java @@ -4,36 +4,13 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.util.UUID; @Data @NoArgsConstructor @AllArgsConstructor public class Wallet { - - private String walletid; + private String Walletid; private User user; - private Float balance = 0f; - public String getWalletid() { - return walletid; - } - - public void setWalletid(String walletid) { - this.walletid = walletid; - } - - public User getUser() { - return user; - } - - public void setUser(User user) { - this.user = user; - } - - public Float getBalance() { - return balance; - } - - public void setBalance(Float balance) { - this.balance = balance; - } + private Float Balance; } diff --git a/src/main/java/com/educare/unitylend/service/impl/WalletServiceImpl.java b/src/main/java/com/educare/unitylend/service/impl/WalletServiceImpl.java index 75b42db..912bb80 100644 --- a/src/main/java/com/educare/unitylend/service/impl/WalletServiceImpl.java +++ b/src/main/java/com/educare/unitylend/service/impl/WalletServiceImpl.java @@ -10,11 +10,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - import java.util.ArrayList; import java.util.List; - @Slf4j @AllArgsConstructor @Service @@ -37,30 +35,43 @@ public List getWallets() throws ServiceException { @Override public Wallet getWalletInfo(String userId) throws ServiceException { - Wallet wallet = walletRepository.getWalletInfo(userId); - - - User user = walletRepository.getUserIdWithWallet(wallet); - wallet.setUser(user); - return wallet; + try { + Wallet wallet = walletRepository.getWalletInfo(userId); + User user = walletRepository.getUserIdWithWallet(wallet); + wallet.setUser(user); + return wallet; + } catch (Exception e) { + log.error("Error encountered during wallet fetching operation"); + throw new ServiceException("Error encountered during wallet fetch operation", e); + } } @Override public void generateWallet(String userId) throws ServiceException { - walletRepository.generateWalletInTable(userId); - Wallet wallet = walletRepository.getWalletInfo(userId); - User user = walletRepository.getUserIdWithWallet(wallet); - wallet.setUser(user); + try { + walletRepository.generateWalletInTable(userId); + Wallet wallet = walletRepository.getWalletInfo(userId); + User user = walletRepository.getUserIdWithWallet(wallet); + wallet.setUser(user); + } catch (Exception e) { + log.error("Error encountered during generate wallet operation"); + throw new ServiceException("Error encountered during generation of wallet", e); + } } @Override - public Wallet getWalletById(String walletId) { - Wallet wallet = walletRepository.getWalletById(walletId); - User user = walletRepository.getUserIdWithWallet(wallet); - wallet.setUser(user); - return wallet; + public Wallet getWalletById(String walletId) throws ServiceException{ + try { + Wallet wallet = walletRepository.getWalletById(walletId); + User user = walletRepository.getUserIdWithWallet(wallet); + wallet.setUser(user); + return wallet; + } catch (Exception e) { + log.error("Error encountered during wallet fetching operation"); + throw new ServiceException("Error encountered during wallet fetch operation", e); + } } From adad8e90df73209b6314d38fc5456aedae90d170 Mon Sep 17 00:00:00 2001 From: Surabhi Shreya Date: Mon, 11 Mar 2024 13:57:18 +0530 Subject: [PATCH 2/3] Made changes to wallet feature based on feedback --- .../unitylend/controller/WalletController.java | 11 ++++++----- .../com/educare/unitylend/service/WalletService.java | 4 ++-- .../unitylend/service/impl/WalletServiceImpl.java | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/educare/unitylend/controller/WalletController.java b/src/main/java/com/educare/unitylend/controller/WalletController.java index 92ec99c..d459a87 100644 --- a/src/main/java/com/educare/unitylend/controller/WalletController.java +++ b/src/main/java/com/educare/unitylend/controller/WalletController.java @@ -41,13 +41,13 @@ public List getAllWallets() throws ControllerException { @GetMapping("/get-wallet-info/{userId}") - public ResponseEntity getWalletInfo(@PathVariable String userId) throws ControllerException { + public ResponseEntity getWalletInfoByUserId(@PathVariable String userId) throws ControllerException { try { if (userId == null || userId.isEmpty()) { return ResponseEntity.badRequest().body(null); } - Wallet currentWallet = walletService.getWalletInfo(userId); + Wallet currentWallet = walletService.getWalletByUserId(userId); if (currentWallet == null) { return ResponseEntity.notFound().build(); @@ -62,15 +62,16 @@ public ResponseEntity getWalletInfo(@PathVariable String userId) throws @GetMapping("get-wallet/{walletId}") - public Wallet getWalletById(@PathVariable String walletId) throws ControllerException { + public Wallet getWalletInfoByWalletId(@PathVariable String walletId) throws ControllerException { try { if (walletId == null || walletId.isEmpty()) { return null; } - Wallet currentWallet = walletService.getWalletById(walletId); + Wallet currentWallet = walletService.getWalletByWalletId(walletId); if (currentWallet == null) { + log.info("Wallet not found"); return null; } @@ -81,7 +82,7 @@ public Wallet getWalletById(@PathVariable String walletId) throws ControllerExce } } - @PostMapping("/{walletId}/addAmount") + @PostMapping("/addAmount/{walletId}") public ResponseEntity addAmountToWallet(@PathVariable String walletId, @RequestParam Float amount) throws ControllerException { try { if (walletId == null || walletId.isEmpty()) { diff --git a/src/main/java/com/educare/unitylend/service/WalletService.java b/src/main/java/com/educare/unitylend/service/WalletService.java index 37e2419..92e5d64 100644 --- a/src/main/java/com/educare/unitylend/service/WalletService.java +++ b/src/main/java/com/educare/unitylend/service/WalletService.java @@ -7,13 +7,13 @@ public interface WalletService { - Wallet getWalletInfo(String userId) throws ServiceException; + Wallet getWalletByUserId(String userId) throws ServiceException; void generateWallet(String userId) throws ServiceException; List getWallets() throws ServiceException; - Wallet getWalletById(String walletId) throws ServiceException; + Wallet getWalletByWalletId(String walletId) throws ServiceException; void addAmountToWallet(String walletId, Float amount) throws ServiceException; diff --git a/src/main/java/com/educare/unitylend/service/impl/WalletServiceImpl.java b/src/main/java/com/educare/unitylend/service/impl/WalletServiceImpl.java index 912bb80..cfa5b38 100644 --- a/src/main/java/com/educare/unitylend/service/impl/WalletServiceImpl.java +++ b/src/main/java/com/educare/unitylend/service/impl/WalletServiceImpl.java @@ -34,7 +34,7 @@ public List getWallets() throws ServiceException { @Override - public Wallet getWalletInfo(String userId) throws ServiceException { + public Wallet getWalletByUserId(String userId) throws ServiceException { try { Wallet wallet = walletRepository.getWalletInfo(userId); User user = walletRepository.getUserIdWithWallet(wallet); @@ -62,7 +62,7 @@ public void generateWallet(String userId) throws ServiceException { @Override - public Wallet getWalletById(String walletId) throws ServiceException{ + public Wallet getWalletByWalletId(String walletId) throws ServiceException{ try { Wallet wallet = walletRepository.getWalletById(walletId); User user = walletRepository.getUserIdWithWallet(wallet); From 0f9408a54daa66dac4a44f946d73479d6698665b Mon Sep 17 00:00:00 2001 From: Surabhi Shreya Date: Mon, 11 Mar 2024 20:21:05 +0530 Subject: [PATCH 3/3] changed wallet class members to camel-case --- src/main/java/com/educare/unitylend/dao/WalletRepository.java | 4 ++-- src/main/java/com/educare/unitylend/model/Wallet.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/educare/unitylend/dao/WalletRepository.java b/src/main/java/com/educare/unitylend/dao/WalletRepository.java index 606f93d..1dc66ea 100644 --- a/src/main/java/com/educare/unitylend/dao/WalletRepository.java +++ b/src/main/java/com/educare/unitylend/dao/WalletRepository.java @@ -46,8 +46,8 @@ public static String getUserIdWithWalletQuery(@Param("ex") Wallet ex) { StringBuilder sqlQueryBuilder = new StringBuilder(); sqlQueryBuilder.append("SELECT u.* FROM tempuser u WHERE u.userid = (SELECT userid FROM wallet WHERE walletid = "); - if (ex != null && ex.getWalletid() != null) { - sqlQueryBuilder.append("'").append(ex.getWalletid()).append("'"); + if (ex != null && ex.getWalletId() != null) { + sqlQueryBuilder.append("'").append(ex.getWalletId()).append("'"); } else { sqlQueryBuilder.append("NULL"); } diff --git a/src/main/java/com/educare/unitylend/model/Wallet.java b/src/main/java/com/educare/unitylend/model/Wallet.java index 1377600..ebae6e0 100644 --- a/src/main/java/com/educare/unitylend/model/Wallet.java +++ b/src/main/java/com/educare/unitylend/model/Wallet.java @@ -10,7 +10,7 @@ @NoArgsConstructor @AllArgsConstructor public class Wallet { - private String Walletid; + private String walletId; private User user; - private Float Balance; + private Float balance; }