diff --git a/src/main/java/com/linkode/api_server/controller/MemberController.java b/src/main/java/com/linkode/api_server/controller/MemberController.java index a0ae35a..984136c 100644 --- a/src/main/java/com/linkode/api_server/controller/MemberController.java +++ b/src/main/java/com/linkode/api_server/controller/MemberController.java @@ -3,6 +3,7 @@ import com.linkode.api_server.common.response.BaseResponse; import com.linkode.api_server.common.response.status.BaseExceptionResponseStatus; import com.linkode.api_server.dto.member.CreateAvatarRequest; +import com.linkode.api_server.dto.member.UpdateAvatarRequest; import com.linkode.api_server.service.MemberService; import com.linkode.api_server.util.JwtProvider; import lombok.RequiredArgsConstructor; @@ -32,4 +33,15 @@ public BaseResponse createAvatar(@RequestBody CreateAvatarRequest createAv return new BaseResponse<>(null); } + /** + * 캐릭터 수정(프로필 수정) + */ + @PatchMapping("/avatar") + public BaseResponse modifyAvatar(@RequestHeader("Authorization") String authorization, @RequestBody UpdateAvatarRequest request) { + log.info("[MemberController.modifyAvatar]"); + Long memberId = jwtProvider.extractIdFromHeader(authorization); + memberService.updateAvatar(memberId, request); + return new BaseResponse<>(null); + } + } diff --git a/src/main/java/com/linkode/api_server/domain/Member.java b/src/main/java/com/linkode/api_server/domain/Member.java index 6493855..62a1634 100644 --- a/src/main/java/com/linkode/api_server/domain/Member.java +++ b/src/main/java/com/linkode/api_server/domain/Member.java @@ -68,4 +68,11 @@ public Member(String githubId, String nickname, Avatar avatar, String color, Bas this.avatar = avatar; this.status = status; } + + public void updateMemberInfo(String nickname, Avatar avatar, String color){ + this.nickname=nickname; + this.avatar=avatar; + this.color=color; + } + } diff --git a/src/main/java/com/linkode/api_server/dto/member/UpdateAvatarRequest.java b/src/main/java/com/linkode/api_server/dto/member/UpdateAvatarRequest.java new file mode 100644 index 0000000..65602ac --- /dev/null +++ b/src/main/java/com/linkode/api_server/dto/member/UpdateAvatarRequest.java @@ -0,0 +1,15 @@ +package com.linkode.api_server.dto.member; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter @Setter +@AllArgsConstructor +@NoArgsConstructor +public class UpdateAvatarRequest { + private String nickname; + private Long avatarId; + private String color; +} diff --git a/src/main/java/com/linkode/api_server/repository/AvatarRepository.java b/src/main/java/com/linkode/api_server/repository/AvatarRepository.java index b8e0ae4..9acf334 100644 --- a/src/main/java/com/linkode/api_server/repository/AvatarRepository.java +++ b/src/main/java/com/linkode/api_server/repository/AvatarRepository.java @@ -2,8 +2,9 @@ import com.linkode.api_server.domain.Avatar; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +@Repository public interface AvatarRepository extends JpaRepository { - } diff --git a/src/main/java/com/linkode/api_server/repository/MemberRepository.java b/src/main/java/com/linkode/api_server/repository/MemberRepository.java index f2b2f73..1f8fa87 100644 --- a/src/main/java/com/linkode/api_server/repository/MemberRepository.java +++ b/src/main/java/com/linkode/api_server/repository/MemberRepository.java @@ -3,11 +3,15 @@ import com.linkode.api_server.domain.Member; import com.linkode.api_server.domain.base.BaseStatus; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; import java.util.Optional; +@Repository public interface MemberRepository extends JpaRepository { boolean existsByGithubIdAndStatus(String githubId, BaseStatus ACTIVE); Optional findByGithubIdAndStatus(String githubId, BaseStatus ACTIVE); + Optional findByMemberIdAndStatus(long memberId, BaseStatus ACTIVE); + } diff --git a/src/main/java/com/linkode/api_server/service/MemberService.java b/src/main/java/com/linkode/api_server/service/MemberService.java index 3b2ba2a..c27261f 100644 --- a/src/main/java/com/linkode/api_server/service/MemberService.java +++ b/src/main/java/com/linkode/api_server/service/MemberService.java @@ -6,6 +6,7 @@ import com.linkode.api_server.domain.Member; import com.linkode.api_server.domain.base.BaseStatus; import com.linkode.api_server.dto.member.CreateAvatarRequest; +import com.linkode.api_server.dto.member.UpdateAvatarRequest; import com.linkode.api_server.repository.AvatarRepository; import com.linkode.api_server.repository.MemberRepository; import lombok.RequiredArgsConstructor; @@ -43,4 +44,25 @@ public void createAvatar(CreateAvatarRequest createAvatarRequest){ memberRepository.save(member); } } + + + @Transactional + public void updateAvatar(long memberId, UpdateAvatarRequest request){ + log.info("[MemberService.updateAvatar]"); + Member member = memberRepository.findByMemberIdAndStatus(memberId,BaseStatus.ACTIVE) + .orElseThrow(()-> new IllegalArgumentException("Invalid memberId: " + memberId)); + + String newNickname= request.getNickname()==null ? member.getNickname():request.getNickname() ; + + Avatar newAvatar= request.getAvatarId()==null ? member.getAvatar() : avatarRepository.findById(request.getAvatarId()) + .orElseThrow(()-> new IllegalArgumentException("Invalid avatarId: " + request.getAvatarId())); + + String newColor= request.getColor()==null ? member.getColor(): request.getColor(); + + member.updateMemberInfo(newNickname,newAvatar,newColor); + + memberRepository.save(member); + + } + }