From f78c97938891ce42890fbf43af59002e9f2096f9 Mon Sep 17 00:00:00 2001 From: Purushottam1 Kumar Date: Mon, 4 Mar 2024 21:00:02 +0530 Subject: [PATCH] Added support for Borrow Request --- pom.xml | 192 +++++++++--------- .../controller/BorrowReqController.java | 64 ++++++ .../unitylend/dao/BorrowReqRepository.java | 41 ++++ .../unitylend/service/BorrowReqService.java | 13 ++ .../service/impl/BorrowServiceImpl.java | 52 +++++ 5 files changed, 266 insertions(+), 96 deletions(-) create mode 100644 src/main/java/com/educare/unitylend/controller/BorrowReqController.java create mode 100644 src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java create mode 100644 src/main/java/com/educare/unitylend/service/BorrowReqService.java create mode 100644 src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java diff --git a/pom.xml b/pom.xml index b95f6e6..29e17e8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,101 +1,101 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.7.17 - - - educare - unitylend - 0.0.1-SNAPSHOT - war - unitylend - UnityLend fosters financial cooperation and empowerment within communities - - 17 - 3.0.0 - 2.17.1 - 3.2.0.RELEASE - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.17 + + + educare + unitylend + 0.0.1-SNAPSHOT + war + unitylend + UnityLend fosters financial cooperation and empowerment within communities + + 17 + 3.0.0 + 2.17.1 + 3.2.0.RELEASE + - - - org.springframework.boot - spring-boot-starter - - - org.projectlombok - lombok - - - org.springframework - spring-context - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.boot - spring-boot-starter-tomcat - - provided - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-configuration-processor - true - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework - spring-aspects - ${spring.aspects.version} - - - org.mybatis.spring.boot - mybatis-spring-boot-starter - ${mybatis.spring.boot.version} - - - org.postgresql - postgresql - - + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + + + org.springframework + spring-context + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework + spring-aspects + ${spring.aspects.version} + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis.spring.boot.version} + + + org.postgresql + postgresql + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - paketobuildpacks/builder-jammy-base:latest - - - - org.projectlombok - lombok - - - org.apache.maven.plugins - maven-clean-plugin - - - - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + paketobuildpacks/builder-jammy-base:latest + + + + org.projectlombok + lombok + + + org.apache.maven.plugins + maven-clean-plugin + + + + + + - + \ No newline at end of file diff --git a/src/main/java/com/educare/unitylend/controller/BorrowReqController.java b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java new file mode 100644 index 0000000..c802416 --- /dev/null +++ b/src/main/java/com/educare/unitylend/controller/BorrowReqController.java @@ -0,0 +1,64 @@ +package com.educare.unitylend.controller; + +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.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@Slf4j +@AllArgsConstructor +@RestController +@RequestMapping("/all-borrow-requests") +public class BorrowReqController extends BaseController{ + BorrowReqService borrowReqService; + @GetMapping("/{userId}") + public List getAllRequests(@PathVariable String userId) throws ControllerException { + try { + List borrowRequestList = borrowReqService.getBorrowRequests(userId); + return borrowRequestList; + } catch (ServiceException e) { + log.error("Error encountered in getting the borrow requests of user"); + throw new ControllerException("Error encountered in getting the borrow requests", e); + } + } + // new + @GetMapping("/{userId}/{communityId}") + public List getRequestsForUserAndCommunity( + @PathVariable String userId, + @PathVariable String communityId + ) throws ControllerException { + try { + List borrowRequestList = borrowReqService.getRequestsForUserAndCommunity(userId, communityId); + return 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); + } + } + + @GetMapping("/user/{userId}/target-amount/{amount}") + public List getRequestsForUserAndCommunity( + @PathVariable String userId, + @PathVariable double amount + ) throws ControllerException { + try { + List borrowRequestListByAmount = borrowReqService.getRequestsByCommunityAndAmount(userId, amount); + return 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); + } + } +// new ends +} \ No newline at end of file diff --git a/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java new file mode 100644 index 0000000..760e3cd --- /dev/null +++ b/src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java @@ -0,0 +1,41 @@ +package com.educare.unitylend.dao; + +import com.educare.unitylend.model.BorrowRequest; +import com.educare.unitylend.model.User; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Select; +import org.apache.ibatis.annotations.Param; +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 + ); + static final String SELECT_REQUESTS_FOR_USER = "SELECT * FROM borrow_request WHERE communityid IN " + + "(SELECT communityid FROM usercommunity WHERE userid = #{userId})"; + + static final String SELECT_REQUESTS_FOR_USER_AND_COMMUNITY = + "SELECT br.* FROM borrow_request br " + + "WHERE br.communityid IN (SELECT communityid FROM usercommunity WHERE userid = #{userId}) " + + "AND br.communityid = #{communityId}"; + + static final String SELECT_REQUESTS_BY_COMMUNITY_AND_AMOUNT = + "SELECT br.* FROM borrow_request br " + + "WHERE br.communityid IN (" + + " SELECT communityid FROM usercommunity WHERE userid = #{userId}" + + ") AND br.targetamount >= #{amount}"; +} \ No newline at end of file diff --git a/src/main/java/com/educare/unitylend/service/BorrowReqService.java b/src/main/java/com/educare/unitylend/service/BorrowReqService.java new file mode 100644 index 0000000..6a2ea2a --- /dev/null +++ b/src/main/java/com/educare/unitylend/service/BorrowReqService.java @@ -0,0 +1,13 @@ +package com.educare.unitylend.service; + +import com.educare.unitylend.Exception.ServiceException; +import com.educare.unitylend.model.BorrowRequest; +import com.educare.unitylend.model.User; + +import java.util.List; + +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; +} \ No newline at end of file diff --git a/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java new file mode 100644 index 0000000..f8d407f --- /dev/null +++ b/src/main/java/com/educare/unitylend/service/impl/BorrowServiceImpl.java @@ -0,0 +1,52 @@ +package com.educare.unitylend.service.impl; + +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.apache.ibatis.annotations.Param; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +@Slf4j +@AllArgsConstructor +@Service +public class BorrowServiceImpl implements BorrowReqService { + private BorrowReqRepository borrowReqRepository; + @Override + public List getBorrowRequests(String userId) throws ServiceException { + try { + List borrowRequestList = borrowReqRepository.getAllRequests(userId); + return borrowRequestList; + } catch (Exception e) { + log.error("Error encountered during user fetch operation"); + throw new ServiceException("Error encountered during user fetch operation", e); + } + } + + @Override + public List getRequestsForUserAndCommunity(String userId, String communityId) throws ServiceException { + try { + List borrowRequestListForComm = borrowReqRepository.getAllRequestsForUserAndCommunity(userId,communityId); + return borrowRequestListForComm; + } catch (Exception e) { + log.error("Error encountered during Borrow request for community fetch operation"); + throw new ServiceException("Error encountered during Borrow request for community fetch operation", e); + } + } + + @Override + public List getRequestsByCommunityAndAmount(String userId, double amount) throws ServiceException { + try { + List borrowRequestListByAmount = borrowReqRepository.getAllRequestsByCommunityAndAmount(userId,amount); + return borrowRequestListByAmount; + } catch (Exception e) { + log.error("Error encountered during Borrow request filtered by amount fetch operation"); + throw new ServiceException("Error encountered during Borrow request filtered by amount fetch operation", e); + } + } +} \ No newline at end of file