Skip to content

Commit

Permalink
[FEAT] 마이페이지 닉네임, 비밀번호, 프로필 사진 변경 코드
Browse files Browse the repository at this point in the history
  • Loading branch information
Anjihee committed Aug 19, 2023
1 parent 4940e43 commit 2dbcba6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/java/com/example/wonderwoman/edit/EditController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.example.wonderwoman.edit;
import com.example.wonderwoman.login.CurrentUser;
import com.example.wonderwoman.member.entity.Member;
import com.example.wonderwoman.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/edit")
public class EditController {

private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;

@Autowired
public EditController(MemberRepository memberRepository, PasswordEncoder passwordEncoder) {
this.memberRepository = memberRepository;
this.passwordEncoder = passwordEncoder;
}

@PostMapping("/change-nickname")
public String changeNickname(@PathVariable Long userId, @RequestParam String newNickname) {
Optional<Member> optionalMember = memberRepository.findById(userId);

if (optionalMember.isPresent()) {
Member member = optionalMember.get();
member.updateNickname(newNickname);
memberRepository.save(member);
return "Nickname changed successfully.";
} else {
return "Member not found.";
}
}

@PostMapping("/change-password")
public String changePassword(@PathVariable Long userId, @RequestParam String currentPassword, @RequestParam String newPassword) {
Optional<Member> optionalMember = memberRepository.findById(userId);

if (optionalMember.isPresent()) {
Member member = optionalMember.get();
if (passwordEncoder.matches(currentPassword, member.getPassword())) {
String newPasswordEncoded = passwordEncoder.encode(newPassword);
member.updatePassword(newPasswordEncoded);
memberRepository.save(member);
return "Password changed successfully.";
} else {
return "Current password is incorrect.";
}
} else {
return "Member not found.";
}
}

@PostMapping("/change-image-url")
public String changeImageUrl(@PathVariable Long userId, @RequestParam String newImageUrl) {
Optional<Member> optionalMember = memberRepository.findById(userId);

if (optionalMember.isPresent()) {
Member member = optionalMember.get();
member.updateImage(newImageUrl);
memberRepository.save(member);
return "Image URL changed successfully.";
} else {
return "Member not found.";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public void updatePassword(String password) {
this.password = password;
}

public void updateNickname(String nickname) {
this.password = nickname;
}

public void updateImage(String imgUrl) {
this.imgUrl = imgUrl;
}
Expand Down

0 comments on commit 2dbcba6

Please sign in to comment.