diff --git a/src/main/java/edu/university_connect/controller/UserController.java b/src/main/java/edu/university_connect/controller/UserController.java index 30b60916..fa55a3d0 100644 --- a/src/main/java/edu/university_connect/controller/UserController.java +++ b/src/main/java/edu/university_connect/controller/UserController.java @@ -1,6 +1,7 @@ package edu.university_connect.controller; import edu.university_connect.model.contract.dto.ProfileDto; +import edu.university_connect.model.contract.dto.SearchDto; import edu.university_connect.model.contract.request.action.ActionUpdateRequest; import edu.university_connect.model.contract.request.profile.ProfileRequest; import edu.university_connect.model.contract.request.user.BlockRequest; @@ -117,6 +118,15 @@ public ResponseEntity> updateProfile(@Valid @RequestBody return ResponseEntity.ok(apiResponse); } + @GetMapping("/search") + public ResponseEntity>> getAllByUserName(@RequestParam String username) { + List searchDtos = service.getAllStudentsByName(username); + ApiResponse> apiResponse = new ApiResponse>(); + apiResponse.setResponseData(searchDtos); + apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20004,new String[]{"search"})); + return ResponseEntity.ok(apiResponse); + } + @PostMapping("/{id}/blocked-users") public ResponseEntity> blockUser(@Valid @RequestBody BlockRequest request, @PathVariable Long id) { @@ -136,6 +146,4 @@ public ResponseEntity> unBlockUser(@Valid @RequestBody Bloc apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20000)); return ResponseEntity.ok(apiResponse); } - - } diff --git a/src/main/java/edu/university_connect/model/contract/dto/SearchDto.java b/src/main/java/edu/university_connect/model/contract/dto/SearchDto.java new file mode 100644 index 00000000..e8246f5a --- /dev/null +++ b/src/main/java/edu/university_connect/model/contract/dto/SearchDto.java @@ -0,0 +1,9 @@ +package edu.university_connect.model.contract.dto; + +import lombok.Data; + +@Data +public class SearchDto { + private UserDto userDto; + private ProfileDto profileDto; +} diff --git a/src/main/java/edu/university_connect/repository/UserRepository.java b/src/main/java/edu/university_connect/repository/UserRepository.java index 6e09a47b..ec78560e 100644 --- a/src/main/java/edu/university_connect/repository/UserRepository.java +++ b/src/main/java/edu/university_connect/repository/UserRepository.java @@ -3,10 +3,15 @@ import edu.university_connect.domain.entity.Resource; import edu.university_connect.domain.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import java.util.List; import java.util.Optional; public interface UserRepository extends JpaRepository { Optional findByUsername(String username); + @Query("SELECT u FROM User u WHERE u.username = :username ") + List findAllByUsername(@Param("username") String username); } diff --git a/src/main/java/edu/university_connect/service/resource/ResourceServiceImpl.java b/src/main/java/edu/university_connect/service/resource/ResourceServiceImpl.java index 8e4c906d..5e042073 100644 --- a/src/main/java/edu/university_connect/service/resource/ResourceServiceImpl.java +++ b/src/main/java/edu/university_connect/service/resource/ResourceServiceImpl.java @@ -49,7 +49,7 @@ public ResourceDto getById(Long id) { if (resourceOpt.isPresent()) { ResourceDto resourceDto=ResourceDtoMapper.MAPPER.entityToDto(resourceOpt.get()); if(Objects.nonNull(resourceOpt.get().getUrl())) { - List savedFiles = storageService.loadFileNames(contextUser.getUser().getUsername(), StorageResourceType.RESOURCE.name(), + List savedFiles = storageService.loadFileNames(contextUser.getLoginUser().getUsername(), StorageResourceType.RESOURCE.name(), resourceOpt.get().getId()); resourceDto.setFiles(savedFiles); } @@ -78,7 +78,7 @@ public ResourceDto update(Long id, ResourceRequest updateRequest) { Resource savedResource = repository.save(resource); ResourceDto resourceDto=ResourceDtoMapper.MAPPER.entityToDto(resource); if(Objects.nonNull(savedResource.getUrl())) { - List savedFiles = storageService.loadFileNames(contextUser.getUser().getUsername(), StorageResourceType.RESOURCE.name(), + List savedFiles = storageService.loadFileNames(contextUser.getLoginUser().getUsername(), StorageResourceType.RESOURCE.name(), savedResource.getId()); resourceDto.setFiles(savedFiles); } @@ -113,11 +113,11 @@ public ResourceDto handleFileUpload(MultipartFile[] files, Long id) { throw ServiceException.of(AppStatusCode.E40000, "resource", "id = " + id); } Resource resource=resourceOpt.get(); - String url=storageService.store(files,contextUser.getUser().getUsername(), StorageResourceType.RESOURCE.name(), + String url=storageService.store(files,contextUser.getLoginUser().getUsername(), StorageResourceType.RESOURCE.name(), resourceOpt.get().getId()); resource.setUrl(url); repository.save(resource); - List savedFiles=storageService.loadFileNames(contextUser.getUser().getUsername(), + List savedFiles=storageService.loadFileNames(contextUser.getLoginUser().getUsername(), StorageResourceType.RESOURCE.name(), resourceOpt.get().getId()); ResourceDto resourceDto=ResourceDtoMapper.MAPPER.entityToDto(resource); @@ -132,7 +132,7 @@ public org.springframework.core.io.Resource loadResource(String filename, Long i throw ServiceException.of(AppStatusCode.E40000, "resource", "id = " + id); } org.springframework.core.io.Resource fileResource=storageService.loadAsResource(filename, - storageService.getRootLocation(contextUser.getUser().getUsername(), StorageResourceType.RESOURCE.name(), + storageService.getRootLocation(contextUser.getLoginUser().getUsername(), StorageResourceType.RESOURCE.name(), resourceOpt.get().getId())); if(Objects.isNull(fileResource)){ throw ServiceException.of(AppStatusCode.E50009); diff --git a/src/main/java/edu/university_connect/service/user/UserService.java b/src/main/java/edu/university_connect/service/user/UserService.java index e5c44a58..ddeab7ed 100644 --- a/src/main/java/edu/university_connect/service/user/UserService.java +++ b/src/main/java/edu/university_connect/service/user/UserService.java @@ -1,6 +1,7 @@ package edu.university_connect.service.user; import edu.university_connect.model.contract.dto.ProfileDto; +import edu.university_connect.model.contract.dto.SearchDto; import edu.university_connect.model.contract.request.auth.SignUpRequest; import edu.university_connect.model.contract.request.profile.ProfileRequest; import edu.university_connect.model.contract.request.user.BlockRequest; @@ -29,6 +30,8 @@ public interface UserService { ProfileDto updateUserProfile(Long id, ProfileRequest updateRequest); + List getAllStudentsByName(String uname); + boolean blockUser(Long id, BlockRequest request); boolean unblockUser(Long id, BlockRequest request); diff --git a/src/main/java/edu/university_connect/service/user/UserServiceImpl.java b/src/main/java/edu/university_connect/service/user/UserServiceImpl.java index 4de86161..c5589406 100644 --- a/src/main/java/edu/university_connect/service/user/UserServiceImpl.java +++ b/src/main/java/edu/university_connect/service/user/UserServiceImpl.java @@ -8,6 +8,7 @@ import edu.university_connect.mapper.ProfileDtoMapper; import edu.university_connect.mapper.UserDtoMapper; import edu.university_connect.model.contract.dto.ProfileDto; +import edu.university_connect.model.contract.dto.SearchDto; import edu.university_connect.model.contract.request.auth.SignUpRequest; import edu.university_connect.model.contract.request.profile.ProfileRequest; import edu.university_connect.model.contract.request.user.BlockRequest; @@ -79,7 +80,7 @@ public UserDto create(UserCreateRequest createRequest) { @Override public UserDto update(Long id, UserUpdateRequest updateRequest) { Optional userOpt= getUserById(id); - if (userOpt.isPresent()){ + if(userOpt.isPresent()){ User user=userOpt.get(); user.setUsername(updateRequest.getUsername()); user.setEmail(updateRequest.getEmail()); @@ -165,6 +166,19 @@ public ProfileDto updateUserProfile(Long id, ProfileRequest updateRequest) { } @Override + + public List getAllStudentsByName(String uname) { + List users = repository.findAllByUsername(uname); + return users.stream().map(user -> { + SearchDto searchDto = new SearchDto(); + searchDto.setUserDto(UserDtoMapper.MAPPER.entityToDto(user)); + Optional profileOpt = profileService.getProfileByUserId(user.getId()); + if (profileOpt.isPresent()) { + searchDto.setProfileDto(ProfileDtoMapper.MAPPER.entityToDto(profileOpt.get())); + } + return searchDto; + }).toList(); + public boolean blockUser(Long id, BlockRequest request) { Optional blockerOpt=repository.findByUsername(contextUser.getUser().getUsername()); Optional blockedOpt=repository.findById(request.getUserId()); @@ -192,5 +206,6 @@ public boolean unblockUser(Long id, BlockRequest request) { blocker.setBlockedUsers(blockedUsers); repository.save(blocker); return true; + } } diff --git a/university-connect.iml b/university-connect.iml index f9855698..e3d99454 100644 --- a/university-connect.iml +++ b/university-connect.iml @@ -6,7 +6,7 @@ - +