Skip to content

Commit

Permalink
Merge pull request #106 from WooRibound/feature/#33-Wbuser-info
Browse files Browse the repository at this point in the history
[feat] #33 - 개인 회원 직종 수정 추가
  • Loading branch information
2oo1s authored Nov 18, 2024
2 parents cdfea0b + 1f88347 commit a5a4353
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wooribound.api.individual.controller;

import com.wooribound.api.individual.dto.UserApplyDTO;
import com.wooribound.api.individual.dto.WbUserDTO;
import com.wooribound.api.individual.dto.WbUserUpdateDTO;
import com.wooribound.api.individual.facade.WbUserInfoFacade;
Expand Down Expand Up @@ -40,8 +39,8 @@ public WbUserDTO getUserInfo(Authentication authentication) {

// 4. 사용자 정보 수정
@PostMapping("/update")
public String updateUserInfo(@RequestBody WbUserUpdateDTO wbUserUpdateDTO) {
return wbUserInfoFacade.updateUserInfo(wbUserUpdateDTO);
public WbUserUpdateDTO updateUserInfo(Authentication authentication, @RequestBody WbUserUpdateDTO wbUserUpdateDTO) {
return wbUserInfoFacade.updateUserInfo(authentication,wbUserUpdateDTO);
}

// 5. 우바 점수 조회
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
@Builder
Expand All @@ -20,4 +21,6 @@ public class WbUserUpdateDTO {
private YN jobInterest;
private String addrCity;
private String addrProvince;
private List<String> workHistoryJobs;
private List<String> interestJobs;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wooribound.api.individual.facade;

import com.wooribound.api.individual.dto.UserApplyDTO;
import com.wooribound.api.individual.dto.WbUserDTO;
import com.wooribound.api.individual.dto.WbUserUpdateDTO;
import com.wooribound.domain.userapply.dto.WbUserApplyDTO;
Expand Down Expand Up @@ -45,7 +44,9 @@ public WbUserDTO getUserInfo(Authentication authentication) {

// 4. 사용자 정보 변경
@Transactional
public String updateUserInfo(WbUserUpdateDTO wbUserUpdateDTO) {
public WbUserUpdateDTO updateUserInfo(Authentication authentication, WbUserUpdateDTO wbUserUpdateDTO) {
String userId = authenticateUtil.CheckWbUserAuthAndGetUserId(authentication);
wbUserUpdateDTO.setUserId(userId);
return wbUserService.updateUserInfo(wbUserUpdateDTO);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ INSERT INTO interest_job (interest_id, job_id, user_id)

@Query("SELECT ij.job.jobName FROM InterestJob ij WHERE ij.wbUser.userId = :userId")
List<String> findJobNamesByUserId(@Param("userId") String userId);

@Modifying
@Query("DELETE FROM InterestJob ij WHERE ij.wbUser.userId = :userId")
void deleteByUserId(@Param("userId") String userId);
}
3 changes: 2 additions & 1 deletion src/main/java/com/wooribound/domain/job/JobRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Optional;

@Repository
public interface JobRepository extends JpaRepository<Job,Long> {
public interface JobRepository extends JpaRepository<Job, Long> {

Job findByJobName(String jobName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface WbUserService {

WbUserDTO getUserInfo(String userId);

String updateUserInfo(WbUserUpdateDTO wbUserUpdateDTO);
WbUserUpdateDTO updateUserInfo(WbUserUpdateDTO wbUserUpdateDTO);

void saveWbUser(WbUser wbUser);

Expand Down
199 changes: 158 additions & 41 deletions src/main/java/com/wooribound/domain/wbuser/WbUserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import com.wooribound.api.individual.dto.WbUserDTO;
import com.wooribound.api.individual.dto.WbUserJoinDTO;
import com.wooribound.api.individual.dto.WbUserUpdateDTO;
import com.wooribound.domain.interestjob.InterestJob;
import com.wooribound.domain.interestjob.InterestJobRepository;
import com.wooribound.domain.job.Job;
import com.wooribound.domain.job.JobRepository;
import com.wooribound.domain.workhistory.WorkHistory;
import com.wooribound.domain.workhistory.WorkHistoryRepository;
import com.wooribound.global.constant.Gender;
import com.wooribound.global.constant.YN;
import com.wooribound.global.exception.JoinWbUserException;
import com.wooribound.global.exception.NoWbUserException;
import lombok.RequiredArgsConstructor;
import org.hibernate.Hibernate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,19 +26,16 @@
import java.util.Optional;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class WbUserServiceImpl implements WbUserService {

private static final Logger logger = LoggerFactory.getLogger(WbUserServiceImpl.class);

private final WbUserRepository wbUserRepository;
private final WorkHistoryRepository workHistoryRepository;

@Autowired
public WbUserServiceImpl(WbUserRepository wbUserRepository, WorkHistoryRepository workHistoryRepository) {
this.wbUserRepository = wbUserRepository;
this.workHistoryRepository = workHistoryRepository;
}
private final InterestJobRepository interestJobRepository;
private final JobRepository jobRepository;

// 1. 사용자 정보 조회
@Override
Expand Down Expand Up @@ -78,31 +81,144 @@ public WbUserDTO getUserInfo(String userId) {

// 2. 사용자 정보 수정
@Override
public String updateUserInfo(WbUserUpdateDTO wbUserUpdateDTO) {
try {
Optional<WbUser> optionalUser = wbUserRepository.findByUserId(wbUserUpdateDTO.getUserId());
if (optionalUser.isEmpty()) {
return "사용자 정보를 찾을 수 없습니다.";
}
public WbUserUpdateDTO updateUserInfo(WbUserUpdateDTO wbUserUpdateDTO) {

WbUser user = optionalUser.get();
WbUserDTO user = wbUserRepository.findByUserId(wbUserUpdateDTO.getUserId())
.map(wbUser -> WbUserDTO.builder()
.userId(wbUser.getUserId())
.name(wbUser.getName())
.birth(wbUser.getBirth())
.email(wbUser.getEmail())
.phone(wbUser.getPhone())
.gender(wbUser.getGender())
.exjobChk(wbUser.getExjobChk())
.interestChk(wbUser.getInterestChk())
.addrCity(wbUser.getAddrCity())
.addrProvince(wbUser.getAddrProvince())
.jobPoint(wbUser.getJobPoint())
.jobInterest(wbUser.getJobInterest())
.createdAt(wbUser.getCreatedAt())
.updatedAt(wbUser.getUpdatedAt())
.isDeleted(wbUser.getIsDeleted())
.workHistoryJobs(wbUser.getWorkHistories().stream()
.map(workHistory -> workHistory.getJob().getJobName()) // WorkHistory -> Job -> JobName
.toList())
.interestJobs(wbUser.getInterestJobs().stream()
.map(interestJob -> interestJob.getJob().getJobName()) // InterestJob -> Job -> JobName
.toList())
.build())
.orElseThrow(() -> new NoWbUserException("사용자를 찾을 수 없습니다. ID: " + wbUserUpdateDTO.getUserId()));

// WbUserUpdateDTO의 필드만 업데이트
// 사용자 정보 업데이트
if (wbUserUpdateDTO.getName() != null) {
user.setName(wbUserUpdateDTO.getName());
user.setBirth(wbUserUpdateDTO.getBirth());
}
if (wbUserUpdateDTO.getPhone() != null) {
user.setPhone(wbUserUpdateDTO.getPhone());
}
if (wbUserUpdateDTO.getGender() != null) {
user.setGender(wbUserUpdateDTO.getGender());
user.setExjobChk(wbUserUpdateDTO.getExjobChk());
user.setJobInterest(wbUserUpdateDTO.getJobInterest());
}
if (wbUserUpdateDTO.getAddrCity() != null) {
user.setAddrCity(wbUserUpdateDTO.getAddrCity());
}
if (wbUserUpdateDTO.getAddrProvince() != null) {
user.setAddrProvince(wbUserUpdateDTO.getAddrProvince());
user.setUpdatedAt(new Date());
}
if (wbUserUpdateDTO.getExjobChk() != null) {
user.setExjobChk(wbUserUpdateDTO.getExjobChk());
}

wbUserRepository.save(user);
return "사용자 정보가 성공적으로 수정되었습니다.";
} catch (Exception e) {
logger.error("사용자 정보 수정 중 오류 발생: {}", e.getMessage());
return "사용자 정보 수정 중 오류가 발생했습니다.";
// 관심 직종 업데이트
if (wbUserUpdateDTO.getInterestJobs() != null) {
updateInterestJobs(user, wbUserUpdateDTO.getInterestJobs());
}

if (wbUserUpdateDTO.getExjobChk() != null) {
user.setExjobChk(wbUserUpdateDTO.getExjobChk());
// 경력 직종 업데이트
updateWorkHistories(user, wbUserUpdateDTO.getWorkHistoryJobs());
}

// 최신 데이터 조회
WbUser updatedUser = wbUserRepository.findByUserId(user.getUserId())
.orElseThrow(() -> new NoWbUserException("업데이트 후 사용자 조회 실패: ID: " + user.getUserId()));

// 최종 저장
wbUserRepository.save(updatedUser);

return WbUserUpdateDTO.builder()
.userId(updatedUser.getUserId())
.name(updatedUser.getName())
.birth(updatedUser.getBirth())
.phone(updatedUser.getPhone())
.gender(updatedUser.getGender())
.exjobChk(updatedUser.getExjobChk())
.addrCity(updatedUser.getAddrCity())
.addrProvince(updatedUser.getAddrProvince())
.jobInterest(updatedUser.getJobInterest())
.workHistoryJobs(updatedUser.getWorkHistories().stream()
.map(workHistory -> workHistory.getJob().getJobName())
.collect(Collectors.toList()))
.interestJobs(updatedUser.getInterestJobs().stream()
.map(interestJob -> interestJob.getJob().getJobName())
.collect(Collectors.toList()))
.build();
}

private void updateInterestJobs(WbUserDTO user, List<String> interestJobs) {
// 기존 관심 직종 삭제
interestJobRepository.deleteByUserId(user.getUserId());

// 새로운 관심 직종 추가
if (interestJobs != null && !interestJobs.isEmpty()) {
List<InterestJob> newInterestJobs = interestJobs.stream()
.map(jobName -> {
Job job = jobRepository.findByJobName(jobName);
if (job == null) {
logger.warn("존재하지 않는 관심 직종: {}", jobName);
return null;
}
return InterestJob.builder()
.job(job)
.wbUser(wbUserRepository.findByUserId(user.getUserId()).orElseThrow())
.build();
})
.filter(interestJob -> interestJob != null)
.collect(Collectors.toList());
interestJobRepository.saveAll(newInterestJobs);
}
}

private void updateWorkHistories(WbUserDTO user, List<String> workHistoryJobs) {
// 경력 여부 확인
logger.info("ExjobChk 상태: {}", user.getExjobChk());
if (user.getExjobChk() == YN.N) {
// 경력이 없음으로 설정된 경우 WorkHistory 테이블에서 모든 관련 데이터 삭제
workHistoryRepository.deleteByUserId(user.getUserId()); // 명시적으로 삭제
return; // 더 이상 작업 없음
}

// 기존 경력 직종 삭제
workHistoryRepository.deleteByUserId(user.getUserId());

// 새로운 경력 직종 추가
if (workHistoryJobs != null && !workHistoryJobs.isEmpty()) {
List<WorkHistory> newWorkHistories = workHistoryJobs.stream()
.map(jobName -> {
Job job = jobRepository.findByJobName(jobName);
if (job == null) {
logger.warn("존재하지 않는 경력 직종: {}", jobName);
return null;
}
return WorkHistory.builder()
.job(job)
.wbUser(wbUserRepository.findByUserId(user.getUserId()).orElseThrow())
.build();
})
.filter(workHistory -> workHistory != null)
.collect(Collectors.toList());
workHistoryRepository.saveAll(newWorkHistories);
}
}

Expand All @@ -115,7 +231,7 @@ public void saveWbUser(WbUser wbUser) {
public String craeteWbUser(WbUserJoinDTO wbUserJoinDTO) {
try {
WbUser user = wbUserRepository.findByUserId(wbUserJoinDTO.getUserId())
.orElseThrow(NoWbUserException::new);
.orElseThrow(NoWbUserException::new);

// WbUserUpdateDTO의 필드만 업데이트
user.setName(wbUserJoinDTO.getName());
Expand All @@ -131,7 +247,7 @@ public String craeteWbUser(WbUserJoinDTO wbUserJoinDTO) {
user.setInterestChk(wbUserJoinDTO.getInterestChk());
wbUserRepository.save(user);
return "사용자 정보가 성공적으로 수정되었습니다.";
}catch (NoWbUserException e){
} catch (NoWbUserException e) {
throw e;
} catch (Exception e) {
throw new JoinWbUserException();
Expand All @@ -143,23 +259,24 @@ public WbUserDTO getOneUserInfo(String userId) {
WbUser user = wbUserRepository.findByUserId(userId).orElseThrow();

return WbUserDTO.builder()
.userId(user.getUserId())
.name(user.getName())
.birth(user.getBirth())
.email(user.getEmail())
.phone(user.getPhone())
.gender(user.getGender())
.exjobChk(user.getExjobChk())
.interestChk(user.getInterestChk())
.addrCity(user.getAddrCity())
.addrProvince(user.getAddrProvince())
.jobPoint(user.getJobPoint())
.jobInterest(user.getJobInterest())
.createdAt(user.getCreatedAt())
.updatedAt(user.getUpdatedAt())
.isDeleted(user.getIsDeleted())
.build();
.userId(user.getUserId())
.name(user.getName())
.birth(user.getBirth())
.email(user.getEmail())
.phone(user.getPhone())
.gender(user.getGender())
.exjobChk(user.getExjobChk())
.interestChk(user.getInterestChk())
.addrCity(user.getAddrCity())
.addrProvince(user.getAddrProvince())
.jobPoint(user.getJobPoint())
.jobInterest(user.getJobInterest())
.createdAt(user.getCreatedAt())
.updatedAt(user.getUpdatedAt())
.isDeleted(user.getIsDeleted())
.build();
}

// 3. 우바 점수 조회
@Override
public int getJobPoint(String userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ INSERT INTO work_history (exjob_id, job_id, user_id)
@Query("SELECT wh.job.jobName FROM WorkHistory wh WHERE wh.wbUser.userId = :loginUser")
List<String> findJobNamesByUserId(String loginUser);


@Modifying
@Query("DELETE FROM WorkHistory wh WHERE wh.wbUser.userId = :userId")
void deleteByUserId(@Param("userId") String userId);
}

0 comments on commit a5a4353

Please sign in to comment.