Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : 아바타 수정 API 구현 #17

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,4 +33,15 @@ public BaseResponse<Void> createAvatar(@RequestBody CreateAvatarRequest createAv
return new BaseResponse<>(null);
}

/**
* 캐릭터 수정(프로필 수정)
*/
@PatchMapping("/avatar")
public BaseResponse<Void> 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);
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/linkode/api_server/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Avatar, Long> {


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Member, Long> {

boolean existsByGithubIdAndStatus(String githubId, BaseStatus ACTIVE);
Optional<Member> findByGithubIdAndStatus(String githubId, BaseStatus ACTIVE);
Optional<Member> findByMemberIdAndStatus(long memberId, BaseStatus ACTIVE);

}
22 changes: 22 additions & 0 deletions src/main/java/com/linkode/api_server/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 저는 if 문으로 쓸 생각만 했는데 이렇게 하면 코드 길이도 줄고 작성도 편할거같아요!


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);

}

}
Loading