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,11 +2,11 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UnitylendApplication {

public static void main(String[] args) {

SpringApplication.run(UnitylendApplication.class, args);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.educare.unitylend.controller;

import com.educare.unitylend.Exception.ControllerException;
import com.educare.unitylend.model.BorrowRequest;
import com.educare.unitylend.service.BorrowRequestService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/borrow-request")
public class BorrowRequestController extends BaseController{
BorrowRequestService borrowRequestService;
@GetMapping("/{userId}")
public ResponseEntity<List<String>> getAllCommunities(@PathVariable String userId) throws ControllerException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function name should be getCommunitiesForUser()

Copy link
Collaborator

Choose a reason for hiding this comment

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

There should be a method for getting the borrowRequest for a given UserId, Community.

And one for Administration purpose to get all the Borrowrequests.

try {
if (userId == null || userId.isEmpty()) {
return ResponseEntity.badRequest().body(List.of("User ID cannot be null or empty"));
}

List<String> communityList = borrowRequestService.getRequestedCommunitiesByUserId(userId);

if (communityList == null || communityList.isEmpty()) {
return ResponseEntity.notFound().build();
}

return ResponseEntity.ok(communityList);
} catch (Exception e) {
log.error("Error encountered in getting the communities for user with ID: {}", userId, e);
throw new ControllerException("Error encountered in getting the communities", e);
}
}

@PostMapping("/request")
public ResponseEntity<String> raiseBorrowRequest(@RequestBody BorrowRequest borrowRequest) throws ControllerException {
try {
String userId = borrowRequest.getBorrowerId();

if (userId == null || userId.isEmpty()) {
return ResponseEntity.badRequest().body("User ID cannot be null or empty");
}

boolean isBorrowRequestSuccessful = borrowRequestService.raiseBorrowRequestByUserId(userId,borrowRequest);

if (isBorrowRequestSuccessful) {
borrowRequestService.createBorrowRequest(borrowRequest);
return ResponseEntity.ok("Raised Borrow Request");
} else {
return ResponseEntity.ok("Cannot make borrow request as your EMI is greater than monthly salary");
}
} catch (Exception e) {
log.error("Error encountered in raising borrow request for user with ID: {}", borrowRequest.getBorrower(), e);
throw new ControllerException("Error encountered in getting the communities", e);
}
}




}
Original file line number Diff line number Diff line change
@@ -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.model.Community;
import com.educare.unitylend.service.CommunityService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/community")
public class CommunityController extends BaseController {
CommunityService communityService;

@GetMapping("all-communities")
public ResponseEntity<?> getAllCommunities() throws ControllerException {
try {
List<Community> communityList = communityService.getCommunities();

if (communityList.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No data found");
}

return ResponseEntity.ok(communityList);
} catch (ServiceException e) {
log.error("Error encountered in getting the communities", e);
throw new ControllerException("Error encountered in getting the communities", e);
}
}

@GetMapping("get-community-by-tag")
public ResponseEntity<?> getCommunityWithTag(@RequestParam String commonTag) throws ControllerException {
try {
String communityName = communityService.getCommunityName(commonTag);

if (communityName == null || communityName.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No data found");
}

return ResponseEntity.ok(communityName);
} catch (ServiceException e) {
log.error("Error encountered in getting the community by tag", e);
throw new ControllerException("Error encountered in getting the community by tag", e);
}
}

@PostMapping("/create-community")
public ResponseEntity<String> createNewCommunity(@RequestBody Community community) throws ControllerException {
try {
communityService.createCommunity(community);
return ResponseEntity.ok("success!!!");
} catch (ServiceException e) {
log.error("Error encountered in creating the community", e);
throw new ControllerException("Error encountered in creating the community", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.educare.unitylend.controller;

import com.educare.unitylend.Exception.ControllerException;
import com.educare.unitylend.service.UserCommunityService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/usercommunity")
public class UserCommunityController {

private UserCommunityService usercommunityService;
@GetMapping("/{userId}")
public ResponseEntity<List<String>> getAllCommunities(@PathVariable String userId) throws ControllerException {
try {
List<String> communityList = usercommunityService.getCommunitiesByUserId(userId);
return ResponseEntity.ok(communityList);
} catch (Exception e) {
log.error("Error encountered in getting the communities for user with ID: {}", userId, e);
throw new ControllerException("Error encountered in getting the communities", e);
}
}



}
86 changes: 80 additions & 6 deletions src/main/java/com/educare/unitylend/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.educare.unitylend.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -21,18 +21,92 @@ public class UserController extends BaseController {
UserService userService;

/**
*
* @return List of all the available {@link User}
* @throws ControllerException : Exception to be thrown from controller in case of any exception
*/
@GetMapping("all-users")
public List<User> getAllUsers() throws ControllerException {
public ResponseEntity<?> getAllUsers() throws ControllerException {
try {
List<User> userList = userService.getUsers();
return userList;
if (userList.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No data found");
}
return ResponseEntity.ok(userList);
} catch (ServiceException e) {
log.error("Error encountered in getting the users", e);
throw new ControllerException("Error encountered in getting the users", e);
}
}


@PostMapping("/create-user")
public ResponseEntity<String> createUser(@RequestBody User user) throws ControllerException{
// Create the user
try {
userService.createUser(user);
return ResponseEntity.ok("succcesss!!!");
} catch (ServiceException e) {
log.error("Error encountered in getting the users");
throw new ControllerException("Error encountered in getting the users", e);
}

}

@PutMapping("/{userId}")
public ResponseEntity<String> updateUser(@PathVariable String userId, @RequestBody User updatedUser) throws ControllerException {
// Set the userId in the updatedUser object
updatedUser.setUserid(userId);
System.out.println(updatedUser);
// Validate and update the user
try {
userService.updateUser(updatedUser);
} catch (ServiceException e) {
log.error("Error encountered in getting the users");
throw new ControllerException("Error encountered in getting the users", e);
}

return ResponseEntity.ok("User updated successfully");
}
@GetMapping("/{userId}/get-info")
public ResponseEntity<User> getUserByUserId(@PathVariable String userId) throws ControllerException{
try {
if (userId == null || userId.isEmpty()) {
return ResponseEntity.badRequest().build();
}

User user = userService.getUserByUserId(userId);

if (user != null) {
return ResponseEntity.ok(user);
} else {
return ResponseEntity.notFound().build();
}
} catch (ServiceException e) {
// Log the exception or handle it as needed
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}

@PutMapping("/{userId}/inactive")
public ResponseEntity<String> deactivateUser(@PathVariable String userId) {
try {
if (userId == null || userId.isEmpty()) {
return ResponseEntity.badRequest().build();
}
boolean updated = userService.markUserAsInactive(userId);

if (updated) {
return ResponseEntity.ok("User marked as inactive successfully");
} else {
return ResponseEntity.notFound().build();
}
} catch (ServiceException e) {
// Log the exception or handle it as needed
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}


}


Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.educare.unitylend.dao;

import com.educare.unitylend.model.BorrowRequest;
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.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface BorrowRequestRepository {

Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. Add method to fetch borrow request based on community_id, This is required for lenders page.
  2. Modify the schema to have a borrow request multiple communities, You can have a separate table which can have the mapping of community to Borrow request.

@Select("SELECT Community.CommunityName " +
"FROM Borrow_Request " +
"JOIN Community ON Borrow_Request.CommunityID = Community.CommunityId " +
"WHERE Borrow_Request.BorrowerId = #{userId}")
List<String> getBorrowRequestByUserId(@Param("userId") 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.

Based on the functionality, The function name should be
getCommunitiesForWhichBorrowRequestRaisedByUser()


@Insert("INSERT INTO Borrow_Request (RequestID, BorrowerID, CommunityID, ReturnPeriod, Status, TargetAmount) " +
"VALUES (uuid_generate_v4(), #{borrowRequest.borrowerId}, #{borrowRequest.community.communityid}, CAST (#{borrowRequest.returnPeriod} AS INTEGER), #{borrowRequest.status}, #{borrowRequest.targetAmount})")
void createBorrowRequest(@Param("borrowRequest") BorrowRequest borrowRequest);

}
35 changes: 35 additions & 0 deletions src/main/java/com/educare/unitylend/dao/CommunityRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.educare.unitylend.dao;

import com.educare.unitylend.model.Community;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository

public interface CommunityRepository {
@Select("select * from community")
List<Community> getAllCommunities();

@Select("select communityname from community where commontag = #{commontag}")
String getCommunity(String commontag);

@Insert("INSERT INTO community (communityid, communityname, commontag) VALUES (uuid_generate_v4(), #{communityname}, #{commontag})")
void createCommunity(Community community);

@Insert("INSERT INTO community (communityid, communityname, commontag) VALUES (uuid_generate_v4(), #{name}, #{tag})")
void createCommunityUsingStrings(String name, String tag);

@Select("SELECT COUNT(*) > 0 FROM community WHERE commontag = #{commontag}")
boolean existsByCommontag(String commontag);

@Select("SELECT commontag FROM community WHERE commontag = #{commontag}")
String findByCommontag(String commontag);

@Select("SELECT communityid FROM community WHERE communityname = #{name}")
String getCommunityIdByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.educare.unitylend.dao;


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.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserCommunityRepository {
@Insert("INSERT INTO usercommunity (userid, communityid) VALUES (#{userid}, #{communityid})")
void createUserCommunityMapping(@Param("userid") String userId, @Param("communityid") String communityId);


@Select("SELECT c.CommunityName FROM Community c JOIN UserCommunity uc ON c.CommunityId = uc.CommunityId WHERE uc.UserId = #{userId}")
List<String> findCommunityNamesByUserId(@Param("userId") String userId);


}
Loading