-
Notifications
You must be signed in to change notification settings - Fork 4
Create Wallet feature #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: wallet-feature
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,48 +31,80 @@ public class WalletController extends BaseController { | |
| public List<Wallet> getAllWallets() throws ControllerException { | ||
| try { | ||
| List<Wallet> walletList = walletService.getWallets(); | ||
|
|
||
| return walletList; | ||
| } catch (Exception e) { | ||
| log.error("Error encountered in getting the users"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message should be |
||
| throw new ControllerException("Error encountered in getting the users", e); | ||
| } | ||
| } | ||
|
|
||
| @GetMapping("/get-user-info/{userId}") | ||
| public Wallet getWalletInfo(@PathVariable String userId) throws ControllerException{ | ||
|
|
||
| @GetMapping("/get-wallet-info/{userId}") | ||
| public ResponseEntity<Wallet> getWalletInfoByUserId(@PathVariable String userId) throws ControllerException { | ||
| try { | ||
| return walletService.getWalletInfo(userId); | ||
| if (userId == null || userId.isEmpty()) { | ||
| return ResponseEntity.badRequest().body(null); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throw exception in case of userId is empty, With exception message. |
||
| } | ||
|
|
||
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throw exception here with message |
||
| } | ||
|
|
||
| Wallet currentWallet = walletService.getWalletByWalletId(walletId); | ||
|
|
||
| if (currentWallet == null) { | ||
| log.info("Wallet not found"); | ||
| return null; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will be better if u can log a statement here , "Wallet not found" , would help in debugging. |
||
| } | ||
|
|
||
| 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<String> addAmountToWallet(@PathVariable String walletId, @RequestParam Float amount) throws ControllerException{ | ||
| @PostMapping("/addAmount/{walletId}") | ||
| public ResponseEntity<String> addAmountToWallet(@PathVariable String walletId, @RequestParam Float amount) throws ControllerException { | ||
| try { | ||
| if (walletId == null || walletId.isEmpty()) { | ||
| return ResponseEntity.badRequest().body(null); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its not a badRequest, Throw exception with error message |
||
| } | ||
|
|
||
| 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<String> 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) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change the name of method to |
||
| 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"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can |
||
| if (userId != null) { | ||
| sqlQueryBuilder.append(" AND w.userid = '").append(userId).append("'"); | ||
| } | ||
|
|
||
| return sqlQueryBuilder.toString(); | ||
| } | ||
|
|
||
|
|
||
| public static String generateWalletInTableQuery(String userId) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method name should be generateWalletForUser |
||
| 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"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if UserId is null, Whose wallet it is ? Ideally you should have constraint in table for userId to not be null.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handled the case where the userId is null by throwing exception when userId is null |
||
| } | ||
| sqlQueryBuilder.append(")"); | ||
|
|
||
| return sqlQueryBuilder.toString(); | ||
| } | ||
|
|
||
| public static String getUserIdWithWalletQuery(@Param("ex") Wallet ex) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Follow the proper naming convention.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Query here might return no values. |
||
| 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"); | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The below code is not making sense, Instead throw exception when walletId is null
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add similar exception handling in all the other methods where params are necessary |
||
| 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<Wallet> 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); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,36 +4,13 @@ | |
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Data | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class Wallet { | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the wallet have more fields? |
||
| 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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Wallet> getWallets() throws ServiceException; | ||
|
|
||
| Wallet getWalletById(String walletId) throws ServiceException; | ||
| Wallet getWalletByWalletId(String walletId) throws ServiceException; | ||
|
|
||
| void addAmountToWallet(String walletId, Float amount) throws ServiceException; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can another method be added to |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Wallet> 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 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method name should be generateWalletForUser |
||
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we setting user here?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to set user explicitly because when I'm trying to get the wallet info through walletId, I'm getting null instead of user object. |
||
| 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); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function doc is missing,
This holds true for all the methods.