From e6e68dd1d583e16c71adc98120e67523cd8d80f5 Mon Sep 17 00:00:00 2001 From: Shelly Date: Sun, 10 Mar 2024 01:42:06 +0530 Subject: [PATCH 1/5] Added borrow requests features for community lending page --- .../controller/BorrowReqController.java | 32 +++++++++++++++++-- .../unitylend/dao/BorrowReqRepository.java | 17 ++++++++++ .../unitylend/service/BorrowReqService.java | 2 ++ .../service/impl/BorrowServiceImpl.java | 22 +++++++++++++ src/main/resources/application.properties | 4 +-- 5 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java index ddc7e32..524578f 100644 --- a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java +++ b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java @@ -9,6 +9,8 @@ import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -32,7 +34,6 @@ public List getAllRequests(@PathVariable String userId) throws Co throw new ControllerException("Error encountered in getting the borrow requests", e); } } -// new @GetMapping("/{userId}/{communityId}") public List getRequestsForUserAndCommunity( @PathVariable String userId, @@ -60,5 +61,32 @@ public List getRequestsForUserAndCommunity( throw new ControllerException("Error encountered in getting the borrow requests filtered by amount", e); } } -// new ends + + @GetMapping("/community/{communityId}") + public List getBorrowRequestByCommunityId( + @PathVariable String communityId + ) throws ControllerException { + try { + List borrowRequestListByCommunityId = borrowReqService.getBorrowRequestsByCommunityId(communityId); + return borrowRequestListByCommunityId; + } catch (Exception e) { + log.error("Error encountered in getting the borrow requests by community Ids!", e); + throw new ControllerException("Error encountered in getting the borrow requests by commmunity Ids!", e); + } + } + + @GetMapping("/community/{communityId}/target-amount/{amount}") + public List getRequestsForCommunityByAmount( + @PathVariable String communityId, + @PathVariable double amount + ) throws ControllerException{ + try{ + List borrowRequestListOfCommunityIdByAmount = borrowReqService.getBorrowRequestsOfCommunityByAmount(communityId,amount); + return borrowRequestListOfCommunityIdByAmount; + }catch (Exception e){ + log.error("Error encountered in getting the borrow requests of community Ids filtered by amount!", e); + throw new ControllerException("Error encountered in getting the borrow requests of commmunity Ids filtered by amount!", e); + } + } + } diff --git a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java index ad0b8b8..e02bea2 100644 --- a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java +++ b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java @@ -25,6 +25,17 @@ List getAllRequestsByCommunityAndAmount( @Param("userId") String userId, @Param("amount") double amount ); + + @Select(SELECT_REQUESTS_BY_COMMUNITY_ID) + List getAllRequestsByCommunityId( + @Param("communityId") String communityId + ); + + @Select(SELECT_REQUESTS_OF_COMMID_BY_AMOUNT) + List getAllRequestsOfCommunityByAmount( + @Param("communityId") String communityId, + @Param("amount") double amount + ); static final String SELECT_REQUESTS_FOR_USER = "SELECT * FROM borrow_request WHERE communityid IN " + "(SELECT communityid FROM usercommunity WHERE userid = #{userId})"; @@ -38,4 +49,10 @@ List getAllRequestsByCommunityAndAmount( "WHERE br.communityid IN (" + " SELECT communityid FROM usercommunity WHERE userid = #{userId}" + ") AND br.targetamount >= #{amount}"; + + static final String SELECT_REQUESTS_BY_COMMUNITY_ID = + "SELECT * FROM borrow_request WHERE communityid = #{communityId}"; + + static final String SELECT_REQUESTS_OF_COMMID_BY_AMOUNT= + "SELECT * FROM borrow_request WHERE communityid = #{communityId} AND targetamount >= #{amount}"; } diff --git a/src/main/java/com/educare/unitylend/service/BorrowReqService.java b/src/main/java/com/educare/unitylend/service/BorrowReqService.java index 6290036..70b1470 100644 --- a/src/main/java/com/educare/unitylend/service/BorrowReqService.java +++ b/src/main/java/com/educare/unitylend/service/BorrowReqService.java @@ -10,4 +10,6 @@ public interface BorrowReqService { List getBorrowRequests(String userId) throws ServiceException; List getRequestsForUserAndCommunity(String userId, String communityId) throws ServiceException; List getRequestsByCommunityAndAmount(String userId, double amount) throws ServiceException; + List getBorrowRequestsByCommunityId(String communityId) throws ServiceException; + List getBorrowRequestsOfCommunityByAmount(String communityId, double amount) throws ServiceException; } diff --git a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java index f33db23..860c940 100644 --- a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java +++ b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java @@ -49,4 +49,26 @@ public List getRequestsByCommunityAndAmount(String userId, double throw new ServiceException("Error encountered during Borrow request filtered by amount fetch operation", e); } } + + @Override + public List getBorrowRequestsByCommunityId(String communityId) throws ServiceException{ + try{ + List borrowRequestListByCommunityId = borrowReqRepository.getAllRequestsByCommunityId(communityId); + return borrowRequestListByCommunityId; + } catch(Exception e){ + log.error("Error encountered during Borrow request according to community ids operation"); + throw new ServiceException("Error encountered during Borrow request according to community id operation", e); + } + } + + @Override + public List getBorrowRequestsOfCommunityByAmount(String communityId, double amount) throws ServiceException{ + try{ + List borrowRequestOfCommunityNyAmount = borrowReqRepository.getAllRequestsOfCommunityByAmount(communityId,amount); + return borrowRequestOfCommunityNyAmount; + } catch(Exception e){ + log.error("Error encountered during filtering Borrow request of community."); + throw new ServiceException("Error encountered during filtering Borrow request of community.", e); + } + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index af09fad..9f73944 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,6 +2,6 @@ server.port=8085 spring.datasource.url=jdbc:postgresql://localhost:5432/unity-lend -spring.datasource.username=user -spring.datasource.password=password +spring.datasource.username=postgres +spring.datasource.password=Shelly@279 spring.datasource.driver-class-name=org.postgresql.Driver From 7e3a1b16f2ed2050120bdc72cedb3e2342e4e1d1 Mon Sep 17 00:00:00 2001 From: Shelly Date: Sun, 10 Mar 2024 03:12:49 +0530 Subject: [PATCH 2/5] Added validation checks for user and community ids and displaying why no borrow-requests are there to the user --- .../controller/BorrowReqController.java | 42 ++++++++++++++----- .../unitylend/dao/BorrowReqRepository.java | 15 ++++++- .../unitylend/service/BorrowReqService.java | 6 +++ .../service/impl/BorrowServiceImpl.java | 29 +++++++++++++ 4 files changed, 79 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java index 524578f..ff83c65 100644 --- a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java +++ b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java @@ -2,13 +2,10 @@ import com.educare.unitylend.Exception.ControllerException; import com.educare.unitylend.Exception.ServiceException; -import com.educare.unitylend.dao.BorrowReqRepository; import com.educare.unitylend.model.BorrowRequest; -import com.educare.unitylend.model.User; import com.educare.unitylend.service.BorrowReqService; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -25,26 +22,49 @@ public class BorrowReqController extends BaseController{ BorrowReqService borrowReqService; @GetMapping("/{userId}") - public List getAllRequests(@PathVariable String userId) throws ControllerException { + public ResponseEntity getAllRequests(@PathVariable String userId) { try { List borrowRequestList = borrowReqService.getBorrowRequests(userId); - return borrowRequestList; + if (borrowRequestList.isEmpty()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No borrow requests found for user: " + userId); + } else { + return ResponseEntity.ok(borrowRequestList); + } } catch (ServiceException e) { - log.error("Error encountered in getting the borrow requests"); - throw new ControllerException("Error encountered in getting the borrow requests", e); + log.error("Error encountered in validating the user", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error encountered in validating the user"); } } @GetMapping("/{userId}/{communityId}") - public List getRequestsForUserAndCommunity( + public ResponseEntity getRequestsForUserAndCommunity( @PathVariable String userId, @PathVariable String communityId - ) throws ControllerException { + ) { try { List borrowRequestList = borrowReqService.getRequestsForUserAndCommunity(userId, communityId); - return borrowRequestList; + if (borrowRequestList.isEmpty()) { + // Check if the user is not a part of that community + boolean isUserPartOfCommunity = borrowReqService.isUserPartOfCommunity(userId, communityId); + if (!isUserPartOfCommunity) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User is not a part of the community."); + } + + // Check if no pending borrow requests are there + boolean hasPendingRequests = borrowReqService.hasPendingRequests(userId); + if (!hasPendingRequests) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No pending borrow requests found for the user."); + } + + // Check if the user is not a part of any community + boolean isUserPartOfAnyCommunity = borrowReqService.isUserPartOfAnyCommunity(userId); + if (!isUserPartOfAnyCommunity) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User is not a part of any community."); + } + } + return ResponseEntity.ok(borrowRequestList); } catch (Exception e) { log.error("Error encountered in getting the borrow requests for particular community", e); - throw new ControllerException("Error encountered in getting the borrow requests for particular community", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error encountered in getting the borrow requests for particular community"); } } diff --git a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java index e02bea2..0bc40c4 100644 --- a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java +++ b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java @@ -5,21 +5,21 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.SelectProvider; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface BorrowReqRepository { + @Select(SELECT_REQUESTS_FOR_USER) List getAllRequests(@Param("userId") String userId); - @Select(SELECT_REQUESTS_FOR_USER_AND_COMMUNITY) List getAllRequestsForUserAndCommunity( @Param("userId") String userId, @Param("communityId") String communityId ); - @Select(SELECT_REQUESTS_BY_COMMUNITY_AND_AMOUNT) List getAllRequestsByCommunityAndAmount( @Param("userId") String userId, @@ -36,6 +36,16 @@ List getAllRequestsOfCommunityByAmount( @Param("communityId") String communityId, @Param("amount") double amount ); + + @Select("SELECT COUNT(*) > 0 FROM usercommunity WHERE userid = #{userId} AND communityid = #{communityId}") + boolean isUserPartOfCommunityR(@Param("userId") String userId, @Param("communityId") String communityId); + + @Select("SELECT COUNT(*) > 0 FROM borrow_request WHERE userid = #{userId} AND status = 'pending'") + boolean hasPendingRequestsR(@Param("userId") String userId); + + @Select("SELECT COUNT(*) > 0 FROM usercommunity WHERE userid = #{userId}") + boolean isUserPartOfAnyCommunityR(@Param("userId") String userId); + static final String SELECT_REQUESTS_FOR_USER = "SELECT * FROM borrow_request WHERE communityid IN " + "(SELECT communityid FROM usercommunity WHERE userid = #{userId})"; @@ -55,4 +65,5 @@ List getAllRequestsOfCommunityByAmount( static final String SELECT_REQUESTS_OF_COMMID_BY_AMOUNT= "SELECT * FROM borrow_request WHERE communityid = #{communityId} AND targetamount >= #{amount}"; + } diff --git a/src/main/java/com/educare/unitylend/service/BorrowReqService.java b/src/main/java/com/educare/unitylend/service/BorrowReqService.java index 70b1470..eeee30c 100644 --- a/src/main/java/com/educare/unitylend/service/BorrowReqService.java +++ b/src/main/java/com/educare/unitylend/service/BorrowReqService.java @@ -5,6 +5,7 @@ import com.educare.unitylend.model.User; import java.util.List; +import java.util.ServiceConfigurationError; public interface BorrowReqService { List getBorrowRequests(String userId) throws ServiceException; @@ -12,4 +13,9 @@ public interface BorrowReqService { List getRequestsByCommunityAndAmount(String userId, double amount) throws ServiceException; List getBorrowRequestsByCommunityId(String communityId) throws ServiceException; List getBorrowRequestsOfCommunityByAmount(String communityId, double amount) throws ServiceException; + boolean hasPendingRequests(String userId) throws ServiceException; + boolean isUserPartOfCommunity(String userId, String communityId) throws ServiceException; + boolean isUserPartOfAnyCommunity(String userId) throws ServiceException; + + } diff --git a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java index 860c940..8b14ab4 100644 --- a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java +++ b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java @@ -71,4 +71,33 @@ public List getBorrowRequestsOfCommunityByAmount(String community throw new ServiceException("Error encountered during filtering Borrow request of community.", e); } } + + @Override + public boolean hasPendingRequests(String userId) throws ServiceException{ + try{ + boolean flag = borrowReqRepository.hasPendingRequestsR(userId); + return flag; + }catch(Exception e){ + log.error("Error encountered during boolean check."); + throw new ServiceException("Error encountered during boolean check", e); + } + } + public boolean isUserPartOfCommunity(String userId, String communityId) throws ServiceException{ + try{ + boolean flag = borrowReqRepository.isUserPartOfCommunityR(userId,communityId); + return flag; + }catch(Exception e){ + log.error("Error encountered during boolean check."); + throw new ServiceException("Error encountered during boolean check", e); + } + } + public boolean isUserPartOfAnyCommunity(String userId) throws ServiceException{ + try{ + boolean flag = borrowReqRepository.isUserPartOfAnyCommunityR(userId); + return flag; + }catch(Exception e){ + log.error("Error encountered during boolean check."); + throw new ServiceException("Error encountered during boolean check", e); + } + } } From dc7e953b889092e9b08a970c68bf790f65fce056 Mon Sep 17 00:00:00 2001 From: Shelly Date: Sun, 10 Mar 2024 03:47:57 +0530 Subject: [PATCH 3/5] Debugged responses in postman and terminal --- .../controller/BorrowReqController.java | 19 ++++++++++++------- .../unitylend/dao/BorrowReqRepository.java | 2 ++ .../unitylend/service/BorrowReqService.java | 1 + .../service/impl/BorrowServiceImpl.java | 10 ++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java index ff83c65..3286ac2 100644 --- a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java +++ b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java @@ -24,12 +24,12 @@ public class BorrowReqController extends BaseController{ @GetMapping("/{userId}") public ResponseEntity getAllRequests(@PathVariable String userId) { try { - List borrowRequestList = borrowReqService.getBorrowRequests(userId); - if (borrowRequestList.isEmpty()) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No borrow requests found for user: " + userId); - } else { - return ResponseEntity.ok(borrowRequestList); + boolean isUserexists=borrowReqService.userExists(userId); + if (!isUserexists) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User does not exist."); } + List borrowRequestList = borrowReqService.getBorrowRequests(userId); + return ResponseEntity.ok(borrowRequestList); } catch (ServiceException e) { log.error("Error encountered in validating the user", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error encountered in validating the user"); @@ -75,10 +75,15 @@ public List getRequestsForUserAndCommunity( ) throws ControllerException { try { List borrowRequestListByAmount = borrowReqService.getRequestsByCommunityAndAmount(userId, amount); - return borrowRequestListByAmount; + if (borrowRequestListByAmount.isEmpty()) { + String message = "No borrow requests found with amount greater than " + amount; + return (List) ResponseEntity.status(HttpStatus.NO_CONTENT).body(message); + } + return ResponseEntity.ok(borrowRequestListByAmount).getBody(); } catch (Exception e) { log.error("Error encountered in getting the borrow requests filtered by amount", e); - throw new ControllerException("Error encountered in getting the borrow requests filtered by amount", e); + String errorMessage = "Error encountered in getting the borrow requests filtered by amount: " + e.getMessage(); + return (List) ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage); } } diff --git a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java index 0bc40c4..a9e6bea 100644 --- a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java +++ b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java @@ -46,6 +46,8 @@ List getAllRequestsOfCommunityByAmount( @Select("SELECT COUNT(*) > 0 FROM usercommunity WHERE userid = #{userId}") boolean isUserPartOfAnyCommunityR(@Param("userId") String userId); + @Select("SELECT COUNT(*) > 0 FROM tempuser WHERE userid = #{userId}") + boolean userExistsR(@Param("userId") String userId); static final String SELECT_REQUESTS_FOR_USER = "SELECT * FROM borrow_request WHERE communityid IN " + "(SELECT communityid FROM usercommunity WHERE userid = #{userId})"; diff --git a/src/main/java/com/educare/unitylend/service/BorrowReqService.java b/src/main/java/com/educare/unitylend/service/BorrowReqService.java index eeee30c..2bf8fc5 100644 --- a/src/main/java/com/educare/unitylend/service/BorrowReqService.java +++ b/src/main/java/com/educare/unitylend/service/BorrowReqService.java @@ -13,6 +13,7 @@ public interface BorrowReqService { List getRequestsByCommunityAndAmount(String userId, double amount) throws ServiceException; List getBorrowRequestsByCommunityId(String communityId) throws ServiceException; List getBorrowRequestsOfCommunityByAmount(String communityId, double amount) throws ServiceException; + boolean userExists(String userId) throws ServiceException; boolean hasPendingRequests(String userId) throws ServiceException; boolean isUserPartOfCommunity(String userId, String communityId) throws ServiceException; boolean isUserPartOfAnyCommunity(String userId) throws ServiceException; diff --git a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java index 8b14ab4..499851a 100644 --- a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java +++ b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java @@ -100,4 +100,14 @@ public boolean isUserPartOfAnyCommunity(String userId) throws ServiceException{ throw new ServiceException("Error encountered during boolean check", e); } } + + public boolean userExists(String userId) throws ServiceException{ + try{ + boolean flag = borrowReqRepository.userExistsR(userId); + return flag; + }catch(Exception e){ + log.error("Error encountered during boolean check."); + throw new ServiceException("Error encountered during boolean check", e); + } + } } From 70d63f7c47b997cfac5195bdfc9a32007e8e97ef Mon Sep 17 00:00:00 2001 From: Shelly Date: Sun, 10 Mar 2024 15:39:33 +0530 Subject: [PATCH 4/5] Added case when borrow request empty for given target amount --- .../controller/BorrowReqController.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java index 3286ac2..47d77c7 100644 --- a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java +++ b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java @@ -69,21 +69,21 @@ public ResponseEntity getRequestsForUserAndCommunity( } @GetMapping("/user/{userId}/target-amount/{amount}") - public List getRequestsForUserAndCommunity( + public ResponseEntity getRequestsForUserAndCommunity( @PathVariable String userId, @PathVariable double amount ) throws ControllerException { - try { + try + { List borrowRequestListByAmount = borrowReqService.getRequestsByCommunityAndAmount(userId, amount); - if (borrowRequestListByAmount.isEmpty()) { - String message = "No borrow requests found with amount greater than " + amount; - return (List) ResponseEntity.status(HttpStatus.NO_CONTENT).body(message); + if(borrowRequestListByAmount == null | borrowRequestListByAmount.isEmpty()){ + System.out.println("The requests with target amount greater than given amount does not exist!"); + return ResponseEntity.notFound().build(); } - return ResponseEntity.ok(borrowRequestListByAmount).getBody(); + return ResponseEntity.ok(borrowRequestListByAmount); } catch (Exception e) { log.error("Error encountered in getting the borrow requests filtered by amount", e); - String errorMessage = "Error encountered in getting the borrow requests filtered by amount: " + e.getMessage(); - return (List) ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMessage); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error encountered in getting the borrow requests filtered by amount"); } } From 3a3a28210ad7903268e7fe200b4524e017eae4bf Mon Sep 17 00:00:00 2001 From: Shelly Date: Sun, 10 Mar 2024 15:55:41 +0530 Subject: [PATCH 5/5] Added checks for empty lists in features for community lending page --- .../controller/BorrowReqController.java | 38 +++++++++++-------- .../unitylend/dao/BorrowReqRepository.java | 3 ++ .../unitylend/service/BorrowReqService.java | 1 + .../service/impl/BorrowServiceImpl.java | 10 +++++ 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java index 47d77c7..39116eb 100644 --- a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java +++ b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java @@ -88,30 +88,36 @@ public ResponseEntity getRequestsForUserAndCommunity( } @GetMapping("/community/{communityId}") - public List getBorrowRequestByCommunityId( - @PathVariable String communityId - ) throws ControllerException { + public ResponseEntity getBorrowRequestsByCommunityId(@PathVariable String communityId) { try { - List borrowRequestListByCommunityId = borrowReqService.getBorrowRequestsByCommunityId(communityId); - return borrowRequestListByCommunityId; - } catch (Exception e) { - log.error("Error encountered in getting the borrow requests by community Ids!", e); - throw new ControllerException("Error encountered in getting the borrow requests by commmunity Ids!", e); + boolean isCommunityexists=borrowReqService.communityExists(communityId); + if (!isCommunityexists) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Community does not exist."); + } + List borrowRequestList = borrowReqService.getBorrowRequestsByCommunityId(communityId); + return ResponseEntity.ok(borrowRequestList); + } catch (ServiceException e) { + log.error("Error encountered in validating the user", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error encountered in validating the community"); } } @GetMapping("/community/{communityId}/target-amount/{amount}") - public List getRequestsForCommunityByAmount( + public ResponseEntity getRequestsForCommunityByAmount( @PathVariable String communityId, @PathVariable double amount - ) throws ControllerException{ - try{ + ) throws ControllerException { + try + { List borrowRequestListOfCommunityIdByAmount = borrowReqService.getBorrowRequestsOfCommunityByAmount(communityId,amount); - return borrowRequestListOfCommunityIdByAmount; - }catch (Exception e){ - log.error("Error encountered in getting the borrow requests of community Ids filtered by amount!", e); - throw new ControllerException("Error encountered in getting the borrow requests of commmunity Ids filtered by amount!", e); + if(borrowRequestListOfCommunityIdByAmount == null | borrowRequestListOfCommunityIdByAmount.isEmpty()){ + System.out.println("The requests of community with target amount greater than given amount does not exist!"); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Community borrow requests with target amount greater than given amount does not exist."); + } + return ResponseEntity.ok(borrowRequestListOfCommunityIdByAmount); + } catch (Exception e) { + log.error("Error encountered in getting the borrow requests of community filtered by amount", e); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error encountered in getting the borrow requests of community filtered by amount"); } } - } diff --git a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java index a9e6bea..0ae0c7c 100644 --- a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java +++ b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java @@ -48,6 +48,9 @@ List getAllRequestsOfCommunityByAmount( @Select("SELECT COUNT(*) > 0 FROM tempuser WHERE userid = #{userId}") boolean userExistsR(@Param("userId") String userId); + + @Select("SELECT COUNT(*) > 0 FROM community WHERE communityid = #{communityId}") + boolean communityExistsR(@Param("communityId") String communityId); static final String SELECT_REQUESTS_FOR_USER = "SELECT * FROM borrow_request WHERE communityid IN " + "(SELECT communityid FROM usercommunity WHERE userid = #{userId})"; diff --git a/src/main/java/com/educare/unitylend/service/BorrowReqService.java b/src/main/java/com/educare/unitylend/service/BorrowReqService.java index 2bf8fc5..fe92844 100644 --- a/src/main/java/com/educare/unitylend/service/BorrowReqService.java +++ b/src/main/java/com/educare/unitylend/service/BorrowReqService.java @@ -14,6 +14,7 @@ public interface BorrowReqService { List getBorrowRequestsByCommunityId(String communityId) throws ServiceException; List getBorrowRequestsOfCommunityByAmount(String communityId, double amount) throws ServiceException; boolean userExists(String userId) throws ServiceException; + boolean communityExists(String communityId) throws ServiceException; boolean hasPendingRequests(String userId) throws ServiceException; boolean isUserPartOfCommunity(String userId, String communityId) throws ServiceException; boolean isUserPartOfAnyCommunity(String userId) throws ServiceException; diff --git a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java index 499851a..afc0422 100644 --- a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java +++ b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java @@ -110,4 +110,14 @@ public boolean userExists(String userId) throws ServiceException{ throw new ServiceException("Error encountered during boolean check", e); } } + + public boolean communityExists(String communityId) throws ServiceException{ + try{ + boolean flag = borrowReqRepository.communityExistsR(communityId); + return flag; + }catch(Exception e){ + log.error("Error encountered during boolean check."); + throw new ServiceException("Error encountered during boolean check", e); + } + } }