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 @@ -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;
Expand All @@ -23,42 +22,102 @@
public class BorrowReqController extends BaseController{
BorrowReqService borrowReqService;
@GetMapping("/{userId}")
public List<BorrowRequest> 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<BorrowRequest> 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<BorrowRequest> getRequestsForUserAndCommunity(
public ResponseEntity<?> getRequestsForUserAndCommunity(
@PathVariable String userId,
@PathVariable String communityId
) throws ControllerException {
) {
try {
List<BorrowRequest> 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<BorrowRequest> getRequestsForUserAndCommunity(
public ResponseEntity<?> getRequestsForUserAndCommunity(
@PathVariable String userId,
@PathVariable double amount
) throws ControllerException {
try {
try
{
List<BorrowRequest> 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<BorrowRequest> 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<BorrowRequest> 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
}
37 changes: 35 additions & 2 deletions src/main/java/com/educare/unitylend/dao/BorrowReqRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BorrowRequest> getAllRequests(@Param("userId") String userId);

@Select(SELECT_REQUESTS_FOR_USER_AND_COMMUNITY)
List<BorrowRequest> getAllRequestsForUserAndCommunity(
@Param("userId") String userId,
@Param("communityId") String communityId
);

@Select(SELECT_REQUESTS_BY_COMMUNITY_AND_AMOUNT)
List<BorrowRequest> getAllRequestsByCommunityAndAmount(
@Param("userId") String userId,
@Param("amount") double amount
);

@Select(SELECT_REQUESTS_BY_COMMUNITY_ID)
List<BorrowRequest> getAllRequestsByCommunityId(
@Param("communityId") String communityId
);

@Select(SELECT_REQUESTS_OF_COMMID_BY_AMOUNT)
List<BorrowRequest> 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})";

Expand All @@ -38,4 +64,11 @@ List<BorrowRequest> 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}";

}
10 changes: 10 additions & 0 deletions src/main/java/com/educare/unitylend/service/BorrowReqService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
import com.educare.unitylend.model.User;

import java.util.List;
import java.util.ServiceConfigurationError;

public interface BorrowReqService {
List<BorrowRequest> getBorrowRequests(String userId) throws ServiceException;
List<BorrowRequest> getRequestsForUserAndCommunity(String userId, String communityId) throws ServiceException;
List<BorrowRequest> getRequestsByCommunityAndAmount(String userId, double amount) throws ServiceException;
List<BorrowRequest> getBorrowRequestsByCommunityId(String communityId) throws ServiceException;
List<BorrowRequest> 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;


}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,75 @@ public List<BorrowRequest> getRequestsByCommunityAndAmount(String userId, double
throw new ServiceException("Error encountered during Borrow request filtered by amount fetch operation", e);
}
}

@Override
public List<BorrowRequest> getBorrowRequestsByCommunityId(String communityId) throws ServiceException{
try{
List<BorrowRequest> 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<BorrowRequest> getBorrowRequestsOfCommunityByAmount(String communityId, double amount) throws ServiceException{
try{
List<BorrowRequest> 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);
}
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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