diff --git a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java index ddc7e32..39116eb 100644 --- a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java +++ b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java @@ -2,13 +2,12 @@ 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; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -23,42 +22,102 @@ public class BorrowReqController extends BaseController{ BorrowReqService borrowReqService; @GetMapping("/{userId}") - public List getAllRequests(@PathVariable String userId) throws ControllerException { + public ResponseEntity getAllRequests(@PathVariable String userId) { try { + boolean isUserexists=borrowReqService.userExists(userId); + if (!isUserexists) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User does not exist."); + } List borrowRequestList = borrowReqService.getBorrowRequests(userId); - return borrowRequestList; + 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"); } } -// new @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"); } } @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); - return borrowRequestListByAmount; + 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); } 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); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error encountered in getting the borrow requests filtered by amount"); + } + } + + @GetMapping("/community/{communityId}") + public ResponseEntity getBorrowRequestsByCommunityId(@PathVariable String communityId) { + try { + 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 ResponseEntity getRequestsForCommunityByAmount( + @PathVariable String communityId, + @PathVariable double amount + ) throws ControllerException { + try + { + List borrowRequestListOfCommunityIdByAmount = borrowReqService.getBorrowRequestsOfCommunityByAmount(communityId,amount); + 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"); } } -// new ends } diff --git a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java index ad0b8b8..0ae0c7c 100644 --- a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java +++ b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java @@ -5,26 +5,52 @@ 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, @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 + ); + + @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); + + @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})"; @@ -38,4 +64,11 @@ 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..fe92844 100644 --- a/src/main/java/com/educare/unitylend/service/BorrowReqService.java +++ b/src/main/java/com/educare/unitylend/service/BorrowReqService.java @@ -5,9 +5,19 @@ import com.educare.unitylend.model.User; import java.util.List; +import java.util.ServiceConfigurationError; 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; + 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 f33db23..afc0422 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,75 @@ 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); + } + } + + @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); + } + } + + 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); + } + } + + 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); + } + } } 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