diff --git a/src/main/java/com/wooribound/api/individual/controller/WbUserInfoController.java b/src/main/java/com/wooribound/api/individual/controller/WbUserInfoController.java index f217c1d..0435c94 100644 --- a/src/main/java/com/wooribound/api/individual/controller/WbUserInfoController.java +++ b/src/main/java/com/wooribound/api/individual/controller/WbUserInfoController.java @@ -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; @@ -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. 우바 점수 조회 diff --git a/src/main/java/com/wooribound/api/individual/dto/WbUserUpdateDTO.java b/src/main/java/com/wooribound/api/individual/dto/WbUserUpdateDTO.java index ab1e1da..3b5e9de 100644 --- a/src/main/java/com/wooribound/api/individual/dto/WbUserUpdateDTO.java +++ b/src/main/java/com/wooribound/api/individual/dto/WbUserUpdateDTO.java @@ -6,6 +6,7 @@ import lombok.Data; import java.util.Date; +import java.util.List; @Data @Builder @@ -20,4 +21,6 @@ public class WbUserUpdateDTO { private YN jobInterest; private String addrCity; private String addrProvince; + private List workHistoryJobs; + private List interestJobs; } diff --git a/src/main/java/com/wooribound/api/individual/facade/WbUserInfoFacade.java b/src/main/java/com/wooribound/api/individual/facade/WbUserInfoFacade.java index 26f36ec..b4db0da 100644 --- a/src/main/java/com/wooribound/api/individual/facade/WbUserInfoFacade.java +++ b/src/main/java/com/wooribound/api/individual/facade/WbUserInfoFacade.java @@ -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; @@ -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); } diff --git a/src/main/java/com/wooribound/domain/interestjob/InterestJobRepository.java b/src/main/java/com/wooribound/domain/interestjob/InterestJobRepository.java index b05bc40..00b98cc 100644 --- a/src/main/java/com/wooribound/domain/interestjob/InterestJobRepository.java +++ b/src/main/java/com/wooribound/domain/interestjob/InterestJobRepository.java @@ -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 findJobNamesByUserId(@Param("userId") String userId); + + @Modifying + @Query("DELETE FROM InterestJob ij WHERE ij.wbUser.userId = :userId") + void deleteByUserId(@Param("userId") String userId); } diff --git a/src/main/java/com/wooribound/domain/job/JobRepository.java b/src/main/java/com/wooribound/domain/job/JobRepository.java index 940b41b..e3ad9d0 100644 --- a/src/main/java/com/wooribound/domain/job/JobRepository.java +++ b/src/main/java/com/wooribound/domain/job/JobRepository.java @@ -7,6 +7,7 @@ import java.util.Optional; @Repository -public interface JobRepository extends JpaRepository { +public interface JobRepository extends JpaRepository { + Job findByJobName(String jobName); } diff --git a/src/main/java/com/wooribound/domain/wbuser/WbUserService.java b/src/main/java/com/wooribound/domain/wbuser/WbUserService.java index 9057e86..33874ad 100644 --- a/src/main/java/com/wooribound/domain/wbuser/WbUserService.java +++ b/src/main/java/com/wooribound/domain/wbuser/WbUserService.java @@ -10,7 +10,7 @@ public interface WbUserService { WbUserDTO getUserInfo(String userId); - String updateUserInfo(WbUserUpdateDTO wbUserUpdateDTO); + WbUserUpdateDTO updateUserInfo(WbUserUpdateDTO wbUserUpdateDTO); void saveWbUser(WbUser wbUser); diff --git a/src/main/java/com/wooribound/domain/wbuser/WbUserServiceImpl.java b/src/main/java/com/wooribound/domain/wbuser/WbUserServiceImpl.java index 5a9cf72..6bd15c8 100644 --- a/src/main/java/com/wooribound/domain/wbuser/WbUserServiceImpl.java +++ b/src/main/java/com/wooribound/domain/wbuser/WbUserServiceImpl.java @@ -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; @@ -20,6 +26,7 @@ import java.util.Optional; import java.util.stream.Collectors; +@RequiredArgsConstructor @Service public class WbUserServiceImpl implements WbUserService { @@ -27,12 +34,8 @@ public class WbUserServiceImpl implements WbUserService { 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 @@ -78,31 +81,144 @@ public WbUserDTO getUserInfo(String userId) { // 2. 사용자 정보 수정 @Override - public String updateUserInfo(WbUserUpdateDTO wbUserUpdateDTO) { - try { - Optional 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 interestJobs) { + // 기존 관심 직종 삭제 + interestJobRepository.deleteByUserId(user.getUserId()); + + // 새로운 관심 직종 추가 + if (interestJobs != null && !interestJobs.isEmpty()) { + List 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 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 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); } } @@ -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()); @@ -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(); @@ -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) { diff --git a/src/main/java/com/wooribound/domain/workhistory/WorkHistoryRepository.java b/src/main/java/com/wooribound/domain/workhistory/WorkHistoryRepository.java index 0297e3f..a77490d 100644 --- a/src/main/java/com/wooribound/domain/workhistory/WorkHistoryRepository.java +++ b/src/main/java/com/wooribound/domain/workhistory/WorkHistoryRepository.java @@ -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 findJobNamesByUserId(String loginUser); - + @Modifying + @Query("DELETE FROM WorkHistory wh WHERE wh.wbUser.userId = :userId") + void deleteByUserId(@Param("userId") String userId); }