-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] 마이페이지 닉네임, 비밀번호, 프로필 사진 변경 코드
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/com/example/wonderwoman/edit/EditController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters