Skip to content

Commit

Permalink
feat: 멤버 정보 변경 API 작성 (#101)
Browse files Browse the repository at this point in the history
* feat: 멤버 정보 변경 API 작성

* feat: 멤버 정보 변경 API 작성

* chore: http method patch로 변경
  • Loading branch information
clean2001 authored Aug 3, 2024
1 parent 4adc483 commit 46c8561
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.layer.domain.member.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.layer.common.annotation.MemberId;
import org.layer.domain.form.controller.dto.response.FormGetResponse;
import org.layer.domain.member.controller.dto.UpdateMemberInfoRequest;
import org.layer.domain.member.controller.dto.UpdateMemberInfoResponse;
import org.springframework.http.ResponseEntity;

@Tag(name = "회원 서비스", description = "회원 관련 api")
public interface MemberApi {
@Operation(summary = "회원 정보(이름, 프로필 사진) 수정", method = "POST", description = "회원의 이름과 프로필 사진(url)을 수정합니다.")
ResponseEntity<UpdateMemberInfoResponse> updateMemberInfo(@MemberId Long memberId, @Valid UpdateMemberInfoRequest updateMemberInfoRequest);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.layer.domain.member.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.layer.common.annotation.MemberId;
import org.layer.domain.member.controller.dto.UpdateMemberInfoRequest;
import org.layer.domain.member.controller.dto.UpdateMemberInfoResponse;
import org.layer.domain.member.service.MemberService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/api/member")
@RequiredArgsConstructor
@RestController
public class MemberController implements MemberApi {
private final MemberService memberService;

@Override
@PatchMapping("/update-profile")
public ResponseEntity<UpdateMemberInfoResponse> updateMemberInfo(@MemberId Long memberId, @Valid @RequestBody UpdateMemberInfoRequest updateMemberInfoRequest) {
UpdateMemberInfoResponse response = memberService.updateMemberInfo(memberId, updateMemberInfoRequest);

return new ResponseEntity<>(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.layer.domain.member.controller.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;

@Builder
@Schema(description = "변경할 회원 정보")
public record UpdateMemberInfoRequest(@NotNull
@Schema(description = "변경할 이름")
String name,
@Schema(description = "변경할 이미지 url")
String profileImageUrl) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.layer.domain.member.controller.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;

@Builder
@Schema(description = "정보가 변경된 회원")
public record UpdateMemberInfoResponse(@NotNull
@Schema(description = "정보가 변경된 회원 ID")
Long memberId,
@NotNull
@Schema(description = "변경된 이름")
String name,
@Schema(description = "변경된 이미지 url")
String profileImageUrl) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import org.layer.common.exception.MemberExceptionType;
import org.layer.domain.auth.controller.dto.SignUpRequest;
import org.layer.domain.jwt.SecurityUtil;
import org.layer.domain.member.controller.dto.UpdateMemberInfoRequest;
import org.layer.domain.member.controller.dto.UpdateMemberInfoResponse;
import org.layer.domain.member.entity.Member;
import org.layer.domain.member.entity.SocialType;
import org.layer.domain.member.repository.MemberRepository;
import org.layer.oauth.dto.service.MemberInfoServiceResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

Expand Down Expand Up @@ -48,6 +51,7 @@ public void checkIsNewMember(String socialId, SocialType socialType) {
}
}

@Transactional
public Member saveMember(SignUpRequest signUpRequest, MemberInfoServiceResponse memberInfo) {
Member member = Member.builder()
.name(signUpRequest.name())
Expand All @@ -74,9 +78,24 @@ public Member getMemberByMemberId(Long memberId) {
.orElseThrow(() -> new BaseCustomException(NOT_FOUND_USER));
}

@Transactional
public void withdrawMember(Long memberId) {
Member currentMember = getCurrentMember();
memberRepository.delete(currentMember);
}


@Transactional
public UpdateMemberInfoResponse updateMemberInfo(Long memberId, UpdateMemberInfoRequest updateMemberInfoRequest) {
Member member = memberRepository.findByIdOrThrow(memberId);
member.updateName(updateMemberInfoRequest.name());
member.updateProfileImageUrl(updateMemberInfoRequest.profileImageUrl());


return UpdateMemberInfoResponse.builder()
.memberId(member.getId())
.name(member.getName())
.profileImageUrl(member.getProfileImageUrl())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public enum FormPublishedBy {

ADMIN("관리자","0"),
PERSONAL("개인","1");
;

private String description;
private String code;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package org.layer.domain.member.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.Valid;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -33,15 +25,15 @@ public class Member {
@Enumerated(value = EnumType.STRING)
private MemberRole memberRole;

@Valid
@NotNull
@Enumerated(value = EnumType.STRING)
private SocialType socialType;

@Valid
@NotNull
private String socialId;

private String profileImageUrl;


@Builder(access = AccessLevel.PUBLIC)
private Member(String name,String email, MemberRole memberRole,
Expand All @@ -52,4 +44,12 @@ private Member(String name,String email, MemberRole memberRole,
this.socialType = socialType;
this.socialId = socialId;
}

public void updateName(String name) {
this.name = name;
}

public void updateProfileImageUrl(String profileImageUrl) {
this.profileImageUrl = profileImageUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import org.layer.domain.member.entity.Member;
import org.layer.domain.member.entity.SocialType;
import org.layer.domain.member.exception.MemberException;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

import static org.layer.common.exception.MemberExceptionType.NOT_FOUND_USER;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findBySocialIdAndSocialType(String socialId, SocialType socialType);

default Member findByIdOrThrow(Long memberId) {
return findById(memberId).orElseThrow(() -> new MemberException(NOT_FOUND_USER));
}
}

0 comments on commit 46c8561

Please sign in to comment.