diff --git a/src/main/java/com/educare/unitylend/controller/WalletController.java b/src/main/java/com/educare/unitylend/controller/WalletController.java index 9256c49..d459a87 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,41 +39,72 @@ 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 getWalletInfoByUserId(@PathVariable String userId) throws ControllerException { try { - return walletService.getWalletInfo(userId); + if (userId == null || userId.isEmpty()) { + return ResponseEntity.badRequest().body(null); + } + + Wallet currentWallet = walletService.getWalletByUserId(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 { + public Wallet getWalletInfoByWalletId(@PathVariable String walletId) throws ControllerException { try { - return walletService.getWalletById(walletId); + if (walletId == null || walletId.isEmpty()) { + return null; + } + + Wallet currentWallet = walletService.getWalletByWalletId(walletId); + + if (currentWallet == null) { + log.info("Wallet not found"); + 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); } } - @PostMapping("/{walletId}/addAmount") - public ResponseEntity addAmountToWallet(@PathVariable String walletId, @RequestParam Float amount) throws ControllerException{ + @PostMapping("/addAmount/{walletId}") + 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..1dc66ea 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..ebae6e0 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/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 75b42db..cfa5b38 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 @@ -36,31 +34,44 @@ 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; + public Wallet getWalletByUserId(String userId) throws ServiceException { + 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 getWalletByWalletId(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); + } }