Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search student associated profile #12

Merged
merged 4 commits into from
Jul 3, 2024
Merged
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
@@ -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;
Expand Down Expand Up @@ -117,6 +118,15 @@ public ResponseEntity<ApiResponse<ProfileDto>> updateProfile(@Valid @RequestBody
return ResponseEntity.ok(apiResponse);
}

@GetMapping("/search")
public ResponseEntity<ApiResponse<List<SearchDto>>> getAllByUserName(@RequestParam String username) {
List<SearchDto> searchDtos = service.getAllStudentsByName(username);
ApiResponse<List<SearchDto>> apiResponse = new ApiResponse<List<SearchDto>>();
apiResponse.setResponseData(searchDtos);
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20004,new String[]{"search"}));
return ResponseEntity.ok(apiResponse);
}

@PostMapping("/{id}/blocked-users")
public ResponseEntity<ApiResponse<Boolean>> blockUser(@Valid @RequestBody BlockRequest request,
@PathVariable Long id) {
Expand All @@ -136,6 +146,4 @@ public ResponseEntity<ApiResponse<Boolean>> unBlockUser(@Valid @RequestBody Bloc
apiResponse.setMessage(messagingService.getResponseMessage(AppStatusCode.S20000));
return ResponseEntity.ok(apiResponse);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package edu.university_connect.model.contract.dto;

import lombok.Data;

@Data
public class SearchDto {
private UserDto userDto;
private ProfileDto profileDto;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, Long> {
Optional<User> findByUsername(String username);

@Query("SELECT u FROM User u WHERE u.username = :username ")
List<User> findAllByUsername(@Param("username") String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> savedFiles = storageService.loadFileNames(contextUser.getUser().getUsername(), StorageResourceType.RESOURCE.name(),
List<String> savedFiles = storageService.loadFileNames(contextUser.getLoginUser().getUsername(), StorageResourceType.RESOURCE.name(),
resourceOpt.get().getId());
resourceDto.setFiles(savedFiles);
}
Expand Down Expand Up @@ -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<String> savedFiles = storageService.loadFileNames(contextUser.getUser().getUsername(), StorageResourceType.RESOURCE.name(),
List<String> savedFiles = storageService.loadFileNames(contextUser.getLoginUser().getUsername(), StorageResourceType.RESOURCE.name(),
savedResource.getId());
resourceDto.setFiles(savedFiles);
}
Expand Down Expand Up @@ -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<String> savedFiles=storageService.loadFileNames(contextUser.getUser().getUsername(),
List<String> savedFiles=storageService.loadFileNames(contextUser.getLoginUser().getUsername(),
StorageResourceType.RESOURCE.name(),
resourceOpt.get().getId());
ResourceDto resourceDto=ResourceDtoMapper.MAPPER.entityToDto(resource);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,6 +30,8 @@ public interface UserService {

ProfileDto updateUserProfile(Long id, ProfileRequest updateRequest);

List<SearchDto> getAllStudentsByName(String uname);

boolean blockUser(Long id, BlockRequest request);

boolean unblockUser(Long id, BlockRequest request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +80,7 @@ public UserDto create(UserCreateRequest createRequest) {
@Override
public UserDto update(Long id, UserUpdateRequest updateRequest) {
Optional<User> userOpt= getUserById(id);
if (userOpt.isPresent()){
if(userOpt.isPresent()){
User user=userOpt.get();
user.setUsername(updateRequest.getUsername());
user.setEmail(updateRequest.getEmail());
Expand Down Expand Up @@ -165,6 +166,19 @@ public ProfileDto updateUserProfile(Long id, ProfileRequest updateRequest) {
}

@Override

public List<SearchDto> getAllStudentsByName(String uname) {
List<User> users = repository.findAllByUsername(uname);
return users.stream().map(user -> {
SearchDto searchDto = new SearchDto();
searchDto.setUserDto(UserDtoMapper.MAPPER.entityToDto(user));
Optional<Profile> 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<User> blockerOpt=repository.findByUsername(contextUser.getUser().getUsername());
Optional<User> blockedOpt=repository.findById(request.getUserId());
Expand Down Expand Up @@ -192,5 +206,6 @@ public boolean unblockUser(Long id, BlockRequest request) {
blocker.setBlockedUsers(blockedUsers);
repository.save(blocker);
return true;

}
}
2 changes: 1 addition & 1 deletion university-connect.iml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<setting name="validation-enabled" value="true" />
<setting name="provider-name" value="Hibernate" />
<datasource-mapping>
<factory-entry name="entityManagerFactory" value="8e7e5ef7-c807-46ae-ab7f-296dbd95389c" />
<factory-entry name="entityManagerFactory" />
</datasource-mapping>
<naming-strategy-map />
</configuration>
Expand Down