Skip to content

Commit

Permalink
Merge pull request #23 from GDSC-snowflowerthon/feat/changeInfo-22
Browse files Browse the repository at this point in the history
[feat] 마이페이지
  • Loading branch information
yunji118 authored Jan 11, 2024
2 parents 3649755 + 2c32736 commit 359f4ec
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/ALGo/ALGo_server/entity/User.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 359f4ec

Please sign in to comment.