Skip to content

Commit

Permalink
[COZY-102] 사용자 상세정보 수정 / 조회 API
Browse files Browse the repository at this point in the history
* [COZY-102] feat: 사용자 상세정보 수정 / 조회

[COZY-102] feat: Controller 코드 추가
[COZY-102] feat: 수정, 조회 Service 코드 추가
[COZY-102] feat: toEntity를 업데이트 로직과 함께 사용 가능하게 변경하기
[COZY-102] feat: Request DTO 클래스 생성(Query)
[COZY-102] feat: Response DTO 세부 클래스 생성(Command)
[COZY-102] feat: TimeUtil 오탈자 수정
[COZY-102] feat: 불필요한 Import 제거

* [COZY-102] fix: 중괄호 수정

* [COZY-102] refactor: fixed update logic, common code
  • Loading branch information
jpark0506 authored Jul 31, 2024
1 parent fce1517 commit aef368f
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.cozymate.cozymate_server.domain.memberstat;

import com.cozymate.cozymate_server.domain.member.Member;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO.MemberStatCommandRequestDTO;
import com.cozymate.cozymate_server.domain.university.University;
import com.cozymate.cozymate_server.global.utils.BaseTimeEntity;
import com.cozymate.cozymate_server.global.utils.TimeUtil;
import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -90,4 +92,32 @@ public class MemberStat extends BaseTimeEntity{
@Type(JsonType.class)
@Column(columnDefinition = "json")
private Map<String, List<String>> options;

public void update(Member member, University university, MemberStatCommandRequestDTO memberStatCommandRequestDTO) {
this.member = member;
this.university = university;
this.acceptance = memberStatCommandRequestDTO.getAcceptance();
this.admissionYear = Integer.parseInt(memberStatCommandRequestDTO.getAdmissionYear());
this.major = memberStatCommandRequestDTO.getMajor();
this.numOfRoommate = memberStatCommandRequestDTO.getNumOfRoommate();
this.wakeUpTime = TimeUtil.convertTime(memberStatCommandRequestDTO.getWakeUpMeridian(),memberStatCommandRequestDTO.getWakeUpTime());
this.sleepingTime = TimeUtil.convertTime(memberStatCommandRequestDTO.getSleepingMeridian(),memberStatCommandRequestDTO.getSleepingTime());
this.turnOffTime = TimeUtil.convertTime(memberStatCommandRequestDTO.getTurnOffMeridian(),memberStatCommandRequestDTO.getTurnOffTime());
this.smoking = memberStatCommandRequestDTO.getSmokingState();
this.sleepingHabit = memberStatCommandRequestDTO.getSleepingHabit();
this.airConditioningIntensity = memberStatCommandRequestDTO.getAirConditioningIntensity();
this.heatingIntensity = memberStatCommandRequestDTO.getHeatingIntensity();
this.lifePattern = memberStatCommandRequestDTO.getLifePattern();
this.intimacy = memberStatCommandRequestDTO.getIntimacy();
this.canShare = memberStatCommandRequestDTO.getCanShare();
this.isPlayGame = memberStatCommandRequestDTO.getIsPlayGame();
this.isPhoneCall = memberStatCommandRequestDTO.getIsPhoneCall();
this.studying = memberStatCommandRequestDTO.getStudying();
this.cleanSensitivity = memberStatCommandRequestDTO.getCleanSensitivity();
this.noiseSensitivity = memberStatCommandRequestDTO.getNoiseSensitivity();
this.cleaningFrequency = memberStatCommandRequestDTO.getCleaningFrequency();
this.personality = memberStatCommandRequestDTO.getPersonality();
this.mbti = memberStatCommandRequestDTO.getMbti();
this.options = memberStatCommandRequestDTO.getOptions();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.cozymate.cozymate_server.domain.memberstat.controller;

import com.cozymate.cozymate_server.domain.memberstat.converter.MemberStatConverter;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatResponseDTO.MemberStatQueryResponseDTO;
import com.cozymate.cozymate_server.domain.memberstat.service.MemberStatCommandService;
import com.cozymate.cozymate_server.domain.memberstat.service.MemberStatQueryService;
import com.cozymate.cozymate_server.global.response.ApiResponse;
import com.cozymate.cozymate_server.global.response.code.status.SuccessStatus;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -22,6 +26,7 @@
public class MemberStatController {

private final MemberStatCommandService memberStatCommandService;
private final MemberStatQueryService memberStatQueryService;

/**
* TODO: member는 추후 시큐리티 인증 객체에서 받아오는 것으로 변경 예정, path 변경 예정 사항("/memberId" -> "/")
Expand All @@ -35,9 +40,44 @@ public class MemberStatController {
)
@PostMapping("/{memberId}")
public ResponseEntity<ApiResponse<Long>> createMemberStat(
@PathVariable("memberId") Long memberId,@Valid @RequestBody MemberStatRequestDTO.MemberStatCreateRequestDTO memberStatCreateRequestDTO) {
@PathVariable("memberId") Long memberId,@Valid @RequestBody MemberStatRequestDTO.MemberStatCommandRequestDTO memberStatCommandRequestDTO) {
return ResponseEntity.ok(
ApiResponse.onSuccess(memberStatCommandService.createMemberStat(memberId,memberStatCreateRequestDTO)));
ApiResponse.onSuccess(memberStatCommandService.createMemberStat(memberId,memberStatCommandRequestDTO)));
}

/**
* TODO: member는 추후 시큐리티 인증 객체에서 받아오는 것으로 변경 예정, path 변경 예정 사항("/memberId" -> "/")
*/
@Operation(
summary = "[포비] 사용자 상세정보 수정",
description = "Path Variable로 memberId, body로 사용자 상세정보를 넣어 사용합니다.\n\n"
+ "시간은 형식에 맞춰 meridian은 오전, 오후, time은 값을 주시면 됩니다.\n\n"
+ "에어컨, 히터, 예민도들은 모두 정수로 주시면 됩니다.\n\n"
+ "학번의 경우 09학번-> \"09\"로 주시면 됩니다."
)
@PutMapping("/{memberId}")
public ResponseEntity<ApiResponse<Long>> modifyMemberStat(
@PathVariable("memberId") Long memberId,@Valid @RequestBody MemberStatRequestDTO.MemberStatCommandRequestDTO memberStatCommandRequestDTO) {
return ResponseEntity.ok(
ApiResponse.onSuccess(memberStatCommandService.modifyMemberStat(memberId,memberStatCommandRequestDTO)));
}

/**
* TODO: member는 추후 시큐리티 인증 객체에서 받아오는 것으로 변경 예정, path 변경 예정 사항("/memberId" -> "/")
*/
@Operation(
summary = "[포비] 사용자 상세정보 조회",
description = "Path Variable로 memberId를 넣어 사용합니다.\n\n"
+ "시간 관련 처리를 유의해주세요."
)
@GetMapping("/{memberId}")
public ResponseEntity<ApiResponse<MemberStatQueryResponseDTO>> getMemberStat(
@PathVariable("memberId") Long memberId) {
MemberStatQueryResponseDTO memberStatQueryResponseDTO = MemberStatConverter.toDto(memberStatQueryService.getMemberStat(memberId));
return ResponseEntity.ok(
ApiResponse.onSuccess(
memberStatQueryResponseDTO
));
}

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,77 @@
package com.cozymate.cozymate_server.domain.memberstat.converter;

import com.cozymate.cozymate_server.domain.member.Member;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO;
import com.cozymate.cozymate_server.domain.memberstat.MemberStat;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO.MemberStatCreateRequestDTO;
import com.cozymate.cozymate_server.domain.memberstat.MemberStat.MemberStatBuilder;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO.MemberStatCommandRequestDTO;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatResponseDTO.MemberStatQueryResponseDTO;
import com.cozymate.cozymate_server.domain.university.University;
import com.cozymate.cozymate_server.global.utils.TimeUtil;

public class MemberStatConverter {

public static MemberStat toEntity(
Member member, University university, MemberStatCreateRequestDTO memberStatCreateRequestDTO) {
return MemberStat.builder()
Long memberStatId, Member member, University university, MemberStatCommandRequestDTO memberStatCommandRequestDTO) {
MemberStatBuilder builder = MemberStat.builder()
.member(member)
.university(university)
.admissionYear(Integer.parseInt(memberStatCreateRequestDTO.getAdmissionYear()))
.major(memberStatCreateRequestDTO.getMajor())
.numOfRoommate(memberStatCreateRequestDTO.getNumOfRoommate())
.acceptance(memberStatCreateRequestDTO.getAcceptance())
.wakeUpTime(TimeUtil.convertTime(memberStatCreateRequestDTO.getWakeUpMeridian(), memberStatCreateRequestDTO.getWakeUpTime()))
.sleepingTime(TimeUtil.convertTime(memberStatCreateRequestDTO.getSleepingMeridian(), memberStatCreateRequestDTO.getSleepingTime()))
.turnOffTime(TimeUtil.convertTime(memberStatCreateRequestDTO.getTurnOffMeridian(), memberStatCreateRequestDTO.getTurnOffTime()))
.smoking(memberStatCreateRequestDTO.getSmokingState())
.sleepingHabit(memberStatCreateRequestDTO.getSleepingHabit())
.airConditioningIntensity(memberStatCreateRequestDTO.getAirConditioningIntensity())
.heatingIntensity(memberStatCreateRequestDTO.getHeatingIntensity())
.lifePattern(memberStatCreateRequestDTO.getLifePattern())
.intimacy(memberStatCreateRequestDTO.getIntimacy())
.canShare(memberStatCreateRequestDTO.getCanShare())
.isPlayGame(memberStatCreateRequestDTO.getIsPlayGame())
.isPhoneCall(memberStatCreateRequestDTO.getIsPhoneCall())
.studying(memberStatCreateRequestDTO.getStudying())
.cleanSensitivity(memberStatCreateRequestDTO.getCleanSensitivity())
.noiseSensitivity(memberStatCreateRequestDTO.getNoiseSensitivity())
.cleaningFrequency(memberStatCreateRequestDTO.getCleaningFrequency())
.personality(memberStatCreateRequestDTO.getPersonality())
.mbti(memberStatCreateRequestDTO.getMbti())
.options(memberStatCreateRequestDTO.getOptions())
.admissionYear(Integer.parseInt(memberStatCommandRequestDTO.getAdmissionYear()))
.major(memberStatCommandRequestDTO.getMajor())
.numOfRoommate(memberStatCommandRequestDTO.getNumOfRoommate())
.acceptance(memberStatCommandRequestDTO.getAcceptance())
.wakeUpTime(TimeUtil.convertTime(memberStatCommandRequestDTO.getWakeUpMeridian(), memberStatCommandRequestDTO.getWakeUpTime()))
.sleepingTime(TimeUtil.convertTime(memberStatCommandRequestDTO.getSleepingMeridian(), memberStatCommandRequestDTO.getSleepingTime()))
.turnOffTime(TimeUtil.convertTime(memberStatCommandRequestDTO.getTurnOffMeridian(), memberStatCommandRequestDTO.getTurnOffTime()))
.smoking(memberStatCommandRequestDTO.getSmokingState())
.sleepingHabit(memberStatCommandRequestDTO.getSleepingHabit())
.airConditioningIntensity(memberStatCommandRequestDTO.getAirConditioningIntensity())
.heatingIntensity(memberStatCommandRequestDTO.getHeatingIntensity())
.lifePattern(memberStatCommandRequestDTO.getLifePattern())
.intimacy(memberStatCommandRequestDTO.getIntimacy())
.canShare(memberStatCommandRequestDTO.getCanShare())
.isPlayGame(memberStatCommandRequestDTO.getIsPlayGame())
.isPhoneCall(memberStatCommandRequestDTO.getIsPhoneCall())
.studying(memberStatCommandRequestDTO.getStudying())
.cleanSensitivity(memberStatCommandRequestDTO.getCleanSensitivity())
.noiseSensitivity(memberStatCommandRequestDTO.getNoiseSensitivity())
.cleaningFrequency(memberStatCommandRequestDTO.getCleaningFrequency())
.personality(memberStatCommandRequestDTO.getPersonality())
.mbti(memberStatCommandRequestDTO.getMbti())
.options(memberStatCommandRequestDTO.getOptions());

if (memberStatId != null) {
builder.id(memberStatId);
}

return builder.build();
}

public static MemberStatQueryResponseDTO toDto(MemberStat memberStat){
return MemberStatQueryResponseDTO.builder()
.universityId(memberStat.getUniversity().getId())
.admissionYear(memberStat.getAdmissionYear())
.major(memberStat.getMajor())
.numOfRoommate(memberStat.getNumOfRoommate())
.acceptance(memberStat.getAcceptance())
.wakeUpTime(memberStat.getWakeUpTime())
.sleepingTime(memberStat.getSleepingTime())
.turnOffTime(memberStat.getTurnOffTime())
.smokingState(memberStat.getSmoking())
.sleepingHabit(memberStat.getSleepingHabit())
.airConditioningIntensity(memberStat.getAirConditioningIntensity())
.heatingIntensity(memberStat.getHeatingIntensity())
.lifePattern(memberStat.getLifePattern())
.intimacy(memberStat.getIntimacy())
.canShare(memberStat.getCanShare())
.isPlayGame(memberStat.getIsPlayGame())
.isPhoneCall(memberStat.getIsPhoneCall())
.studying(memberStat.getStudying())
.cleanSensitivity(memberStat.getCleanSensitivity())
.noiseSensitivity(memberStat.getNoiseSensitivity())
.cleaningFrequency(memberStat.getCleaningFrequency())
.personality(memberStat.getPersonality())
.mbti(memberStat.getMbti())
.options(memberStat.getOptions())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@


public class MemberStatRequestDTO {

// Create, Update를 Command DTO로 관리하기
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class MemberStatCreateRequestDTO {
public static class MemberStatCommandRequestDTO {
@NotNull
private Long universityId;
//학번의 경우 처리하기 애매한 부분이 있어, String 2자리로 통일함. EX) 09 학번 -> "09"
Expand Down Expand Up @@ -90,6 +92,4 @@ public static class MemberStatCreateRequestDTO {
private String mbti;
private Map<String, List<String>> options;
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
package com.cozymate.cozymate_server.domain.memberstat.dto;

import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

public class MemberStatResponseDTO {
// Read를 Query DTO로 관리하기
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class MemberStatQueryResponseDTO {

private Long universityId;
private Integer admissionYear;
private String major;
private Integer numOfRoommate;
private String acceptance;
private Integer wakeUpTime;
private Integer sleepingTime;
private Integer turnOffTime;
private String smokingState;
private String sleepingHabit;
private Integer airConditioningIntensity;
private Integer heatingIntensity;
private String lifePattern;
private String intimacy;
private Boolean canShare;
private Boolean isPlayGame;
private Boolean isPhoneCall;
private String studying;
private Integer cleanSensitivity;
private Integer noiseSensitivity;
private String cleaningFrequency;
private String personality;
private String mbti;
private Map<String, List<String>> options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import com.cozymate.cozymate_server.domain.member.MemberRepository;
import com.cozymate.cozymate_server.domain.memberstat.converter.MemberStatConverter;
import com.cozymate.cozymate_server.domain.memberstat.MemberStat;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO.MemberStatCreateRequestDTO;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO.MemberStatCommandRequestDTO;
import com.cozymate.cozymate_server.domain.memberstat.repository.MemberStatRepository;
import com.cozymate.cozymate_server.domain.memberstat.dto.MemberStatRequestDTO;
import com.cozymate.cozymate_server.domain.university.University;
import com.cozymate.cozymate_server.domain.university.UniversityRepository;
import com.cozymate.cozymate_server.global.response.code.status.ErrorStatus;
import com.cozymate.cozymate_server.global.response.exception.GeneralException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -26,24 +26,46 @@ public class MemberStatCommandService {
private final MemberRepository memberRepository;
private final UniversityRepository universityRepository;

public Long createMemberStat(Long memberId, MemberStatCreateRequestDTO memberStatCreateRequestDTO) {
public Long createMemberStat(
Long memberId, MemberStatCommandRequestDTO memberStatCommandRequestDTO) {

// 멤버 상세정보가 이미 존재하는 경우
if (memberStatRepository.findByMemberId(memberId).isPresent()) {
throw new GeneralException(ErrorStatus._MEMBERSTAT_EXISTS);
}

Member member = memberRepository.findById(memberId)
.orElseThrow(()->new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));
University university = universityRepository.findById(memberStatCreateRequestDTO.getUniversityId())
.orElseThrow(()->new GeneralException(ErrorStatus._UNIVERSITY_NOT_FOUND));
.orElseThrow(() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));
University university = universityRepository.findById(
memberStatCommandRequestDTO.getUniversityId())
.orElseThrow(() -> new GeneralException(ErrorStatus._UNIVERSITY_NOT_FOUND));

MemberStat saveMemberStat = memberStatRepository.save(
MemberStatConverter.toEntity(
member, university, memberStatCreateRequestDTO
null, member, university, memberStatCommandRequestDTO
)
);

return saveMemberStat.getId();
}

public Long modifyMemberStat(

Long memberId, MemberStatCommandRequestDTO memberStatCommandRequestDTO) {

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));

University university = universityRepository.findById(
memberStatCommandRequestDTO.getUniversityId())
.orElseThrow(() -> new GeneralException(ErrorStatus._UNIVERSITY_NOT_FOUND));

MemberStat updatedMemberStat = memberStatRepository.findByMemberId(memberId).orElseThrow(
() -> new GeneralException(ErrorStatus._MEMBERSTAT_NOT_EXISTS)
);

updatedMemberStat.update(member,university,memberStatCommandRequestDTO);
return updatedMemberStat.getId();

}
}
Loading

0 comments on commit aef368f

Please sign in to comment.