Skip to content

Commit

Permalink
✨ [Feat] (#62) 회원 검색 api 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
seonyo committed Jun 8, 2024
1 parent e01b0ec commit 0042b12
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/MtoM/MtoM/domain/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.MtoM.MtoM.domain.user.dto.req.RegisterRequestDto;
import com.MtoM.MtoM.domain.user.dto.res.FindAllUserResponseDto;
import com.MtoM.MtoM.domain.user.dto.res.FindByUserResponseDto;
import com.MtoM.MtoM.domain.user.dto.res.SearchUserResponseDto;
import com.MtoM.MtoM.domain.user.service.UserService;
import com.MtoM.MtoM.global.S3Service.S3Service;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -80,4 +81,10 @@ public ResponseEntity<List<FindAllUserResponseDto>> findAllUser(){
List<FindAllUserResponseDto> users = userService.findAllUser();
return ResponseEntity.ok().body(users);
}

@GetMapping("/searches/{searchResult}")
public ResponseEntity<List<SearchUserResponseDto>> searchUser(@PathVariable("searchResult") String searchResult){
List<SearchUserResponseDto> users = userService.searchUser(searchResult);
return ResponseEntity.ok().body(users);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.MtoM.MtoM.domain.user.dto.res;

import com.MtoM.MtoM.domain.user.domain.Gender;
import com.MtoM.MtoM.domain.user.domain.Major;
import com.MtoM.MtoM.domain.user.domain.UserDomain;
import com.MtoM.MtoM.domain.user.dto.Skill;
import lombok.Getter;

import java.util.List;
import java.util.stream.Collectors;

@Getter
public class SearchUserResponseDto {
private String userId;
private String name;
private Long studentId;
private String birthday;
private Gender gender;
private String phonenumber;
private Major major;
private String email;
private String profile;
private String mbti;
private String personal;
private String imogi;
private String mentoring_topics;
private List<Skill> skills;

public SearchUserResponseDto(UserDomain user){
this.userId = user.getId();
this.name = user.getName();
this.studentId = user.getStudent_id();
this.birthday = user.getBirthday();
this.gender = user.getGender();
this.phonenumber = user.getPhonenumber();
this.major = user.getMajor();
this.email = user.getEmail();
this.profile = user.getProfile();
this.mbti = user.getMbti();
this.personal = user.getPersonal();
this.imogi = user.getImogi();
this.mentoring_topics = user.getMentoring_topics();
this.skills = user.getSkillDomainList().stream()
.map(skillDomain -> new Skill(skillDomain.getSkill_name(), skillDomain.getSkill_score()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import com.MtoM.MtoM.domain.user.domain.UserDomain;
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;

public interface UserRepository extends JpaRepository<UserDomain, String> {
boolean existsByEmail(String email);
@Query(value = "SELECT * FROM users u WHERE u.name LIKE %:searchResult% OR CAST(u.student_id AS CHAR) LIKE %:searchResult%", nativeQuery = true)
List<UserDomain> searchUsers(@Param("searchResult") String searchResult);

}
10 changes: 10 additions & 0 deletions src/main/java/com/MtoM/MtoM/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.MtoM.MtoM.domain.user.dto.req.RegisterRequestDto;
import com.MtoM.MtoM.domain.user.dto.res.FindAllUserResponseDto;
import com.MtoM.MtoM.domain.user.dto.res.FindByUserResponseDto;
import com.MtoM.MtoM.domain.user.dto.res.SearchUserResponseDto;
import com.MtoM.MtoM.domain.user.repository.SkillRepository;
import com.MtoM.MtoM.domain.user.repository.UserRepository;
import com.MtoM.MtoM.global.exception.*;
Expand Down Expand Up @@ -97,6 +98,15 @@ public List<FindAllUserResponseDto> findAllUser(){
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<SearchUserResponseDto> searchUser(String searchResult){
List<UserDomain> users= userRepository.searchUsers(searchResult);

return users.stream()
.map(SearchUserResponseDto::new)
.collect(Collectors.toList());
}

public void duplicateId(String id){
if(userRepository.existsById(id)){
throw new IdDuplicateException("id duplicated", ErrorCode.ID_DUPLICATION);
Expand Down

0 comments on commit 0042b12

Please sign in to comment.