Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,48 +31,80 @@ public class WalletController extends BaseController {
public List<Wallet> getAllWallets() throws ControllerException {
try {
Copy link
Collaborator

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.

List<Wallet> walletList = walletService.getWallets();

return walletList;
} catch (Exception e) {
log.error("Error encountered in getting the users");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message should be
log.error("Error encountered in getting the users", e);

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Expand Down
95 changes: 83 additions & 12 deletions src/main/java/com/educare/unitylend/dao/WalletRepository.java
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;


Expand All @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the name of method to getWalletForUserSQLQuery
The method names should be descriptive.

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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can SELECT w.walletid, w.userid, w.balance FROM wallet w be replaced with select w.* from wallet w ?

if (userId != null) {
sqlQueryBuilder.append(" AND w.userid = '").append(userId).append("'");
}

return sqlQueryBuilder.toString();
}


public static String generateWalletInTableQuery(String userId) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if UserId is null, Whose wallet it is ?
User and wallet are closely associated and wallet table always should have UserId populated.

Ideally you should have constraint in table for userId to not be null.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the proper naming convention.
what ex is denoting here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Query here might return no values.
Please add the proper validation.
Or this can be handled at service layer as well.

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");
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The below code is not making sense,
Why are we comparing with null;

Instead throw exception when walletId is null

if (walletid == null) {
        throw new IllegalArgumentException("walletid cannot be null");
    }

Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}

29 changes: 3 additions & 26 deletions src/main/java/com/educare/unitylend/model/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,13 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Wallet {

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can another method be added to deductFromWallet()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we setting user here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
}
}


Expand Down