diff --git a/src/main/java/com/ALGo/ALGo_server/entity/User.java b/src/main/java/com/ALGo/ALGo_server/entity/User.java index 3dfbda5..285f50d 100644 --- a/src/main/java/com/ALGo/ALGo_server/entity/User.java +++ b/src/main/java/com/ALGo/ALGo_server/entity/User.java @@ -1,5 +1,6 @@ package com.ALGo.ALGo_server.entity; +import com.ALGo.ALGo_server.mypage.Dto.ChangeInfoRequestDto; import jakarta.persistence.*; import lombok.Builder; import lombok.Getter; @@ -32,6 +33,13 @@ public class User implements UserDetails { @Enumerated(EnumType.STRING) private Role role; + public void updateInfo(ChangeInfoRequestDto requestDto){ + this.language = requestDto.getLanguage(); + this.city = requestDto.getCity(); + this.gu = requestDto.getGu(); + } + + public void encodePassword(PasswordEncoder passwordEncoder){ this.password = passwordEncoder.encode(password); } diff --git a/src/main/java/com/ALGo/ALGo_server/mypage/Controller/MyPageController.java b/src/main/java/com/ALGo/ALGo_server/mypage/Controller/MyPageController.java new file mode 100644 index 0000000..a6ada73 --- /dev/null +++ b/src/main/java/com/ALGo/ALGo_server/mypage/Controller/MyPageController.java @@ -0,0 +1,28 @@ +package com.ALGo.ALGo_server.mypage.Controller; + +import com.ALGo.ALGo_server.entity.User; +import com.ALGo.ALGo_server.mypage.Dto.ChangeInfoRequestDto; +import com.ALGo.ALGo_server.mypage.Dto.MyInfoResponseDto; +import com.ALGo.ALGo_server.mypage.Service.MyPageService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/mypage") +public class MyPageController { + private final MyPageService myPageService; + + @GetMapping() + public MyInfoResponseDto getMyInfo(@AuthenticationPrincipal User user){ + return myPageService.getMyInfo(user); + } + + @PutMapping("/change") + public ResponseEntity changeInfo(@AuthenticationPrincipal User user, @RequestBody ChangeInfoRequestDto requestDto){ + return myPageService.changeInfo(user, requestDto); + } + +} diff --git a/src/main/java/com/ALGo/ALGo_server/mypage/Dto/ChangeInfoRequestDto.java b/src/main/java/com/ALGo/ALGo_server/mypage/Dto/ChangeInfoRequestDto.java new file mode 100644 index 0000000..4bde38e --- /dev/null +++ b/src/main/java/com/ALGo/ALGo_server/mypage/Dto/ChangeInfoRequestDto.java @@ -0,0 +1,10 @@ +package com.ALGo.ALGo_server.mypage.Dto; + +import lombok.Getter; + +@Getter +public class ChangeInfoRequestDto { + private String language; + private String city; + private String gu; +} diff --git a/src/main/java/com/ALGo/ALGo_server/mypage/Dto/MyInfoResponseDto.java b/src/main/java/com/ALGo/ALGo_server/mypage/Dto/MyInfoResponseDto.java new file mode 100644 index 0000000..797ab69 --- /dev/null +++ b/src/main/java/com/ALGo/ALGo_server/mypage/Dto/MyInfoResponseDto.java @@ -0,0 +1,15 @@ +package com.ALGo.ALGo_server.mypage.Dto; + +import com.ALGo.ALGo_server.entity.User; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class MyInfoResponseDto { + private String email; + private String language; + private String city; + private String gu; + +} diff --git a/src/main/java/com/ALGo/ALGo_server/mypage/Service/MyPageService.java b/src/main/java/com/ALGo/ALGo_server/mypage/Service/MyPageService.java new file mode 100644 index 0000000..2314f6a --- /dev/null +++ b/src/main/java/com/ALGo/ALGo_server/mypage/Service/MyPageService.java @@ -0,0 +1,36 @@ +package com.ALGo.ALGo_server.mypage.Service; + +import com.ALGo.ALGo_server.entity.User; +import com.ALGo.ALGo_server.mypage.Dto.ChangeInfoRequestDto; +import com.ALGo.ALGo_server.mypage.Dto.MyInfoResponseDto; +import com.ALGo.ALGo_server.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.server.ResponseStatusException; + +@Service +@RequiredArgsConstructor +public class MyPageService { + private final UserRepository userRepository; + + @Transactional(readOnly = true) + public MyInfoResponseDto getMyInfo(User user){ + MyInfoResponseDto responseDto = MyInfoResponseDto.builder() + .email(user.getEmail()) + .language(user.getLanguage()) + .city(user.getCity()) + .gu(user.getGu()) + .build(); + return responseDto; + } + + @Transactional + public ResponseEntity changeInfo(User user, ChangeInfoRequestDto requestDto){ + user.updateInfo(requestDto); + userRepository.save(user); + return new ResponseEntity(HttpStatus.OK); + } +}