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 : 마이페이지 기능 구현 #42

Merged
merged 8 commits into from
Oct 17, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import HookKiller.server.board.entity.Article;
import HookKiller.server.board.entity.ArticleContent;
import HookKiller.server.common.type.LanguageType;
import HookKiller.server.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -14,4 +15,6 @@ public interface ArticleContentRepository extends JpaRepository<ArticleContent,

List<ArticleContent> findAllByArticle(Article article);

List<ArticleContent> findAllByAuthor(User user);
Copy link
Member

Choose a reason for hiding this comment

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

ArticleContent에 User가 있나...?
혹시 Article이랑 매핑 되어있어서 되는건가요? (진짜모름)


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package HookKiller.server.user.controller;

import HookKiller.server.board.dto.ArticleRequestDto;
import HookKiller.server.board.entity.Article;
import HookKiller.server.board.entity.ArticleContent;
import HookKiller.server.board.service.ArticleContentService;
import HookKiller.server.user.dto.MyPageRequestDto;
import HookKiller.server.user.service.MyPageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/mypage")
@RequiredArgsConstructor
public class MypageController {

private final MyPageService myPageService;
private final ArticleContentService articleContentService;

/**
* 마이페이지 조회
* @param userId
* @return
*/
@GetMapping("/{userId}")
public MyPageRequestDto getMyPage(Long userId) {
return myPageService.getMyPage(userId);
}

/**
* 정보 수정
* @param requestDto
*/
@PutMapping
public void updatePasswordAndNickname(@RequestBody MyPageRequestDto requestDto) {
myPageService.updatePasswordAndNickname(requestDto);
}

/**
* 마이페이지 내가쓴 글
*
*/
@GetMapping("/{myList}")
public List<ArticleRequestDto> getMyList(Long userId) {
return myPageService.getMyList(userId);
}

}
28 changes: 28 additions & 0 deletions src/main/java/HookKiller/server/user/dto/MyPageRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package HookKiller.server.user.dto;

import HookKiller.server.user.entity.User;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class MyPageRequestDto {

private Long userId;
private String email;
private String password;
private String thumbnail;
private String nickName;

public static MyPageRequestDto from(User user) {
return MyPageRequestDto.builder()
.userId(user.getId())
.email(user.getEmail())
.password(user.getPassword())
.thumbnail(user.getThumbnail())
.nickName(user.getNickName())
.build();
}
}
11 changes: 7 additions & 4 deletions src/main/java/HookKiller/server/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import HookKiller.server.user.type.UserRole;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;


@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "tbl_user")
public class User extends AbstractTimeStamp {
Expand All @@ -35,6 +33,11 @@ public class User extends AbstractTimeStamp {

@Enumerated(EnumType.STRING)
private UserRole role;

public void updatePasswordAndNickname(String nickName, String password) {
this.nickName = nickName;
this.password = password;
}

@Builder
public User(String email, String password, String nickName, String role) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmailAndPassword(String email, String password);

boolean existsByEmail(String email);


}
63 changes: 63 additions & 0 deletions src/main/java/HookKiller/server/user/service/MyPageService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package HookKiller.server.user.service;

import HookKiller.server.auth.exception.UserNotFoundException;
import HookKiller.server.board.dto.ArticleRequestDto;
import HookKiller.server.common.util.UserUtils;
import HookKiller.server.user.dto.MyPageRequestDto;
import HookKiller.server.user.entity.User;
import HookKiller.server.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class MyPageService {

private final UserUtils userUtils;
private final UserRepository userRepository;

@Transactional(readOnly = true)
public MyPageRequestDto getMyPage(Long userId) {
// 로그인한 사용자 정보를 가져옴
User user = userUtils.getUser();

User requestUser = userRepository.findById(userId).orElseThrow(() -> UserNotFoundException.EXCEPTION);

if (user.getId().equals(requestUser.getId())) {
return MyPageRequestDto.from(requestUser);
}

return null;
}

@Transactional
public void updatePasswordAndNickname(MyPageRequestDto myPageRequestDto) {
User user = userUtils.getUser();

User requestUser = userRepository.findById(myPageRequestDto.getUserId())
.orElseThrow(()-> UserNotFoundException.EXCEPTION);

if (myPageRequestDto.getPassword() != null) {
requestUser.setPassword(myPageRequestDto.getPassword());
}
if (myPageRequestDto.getNickName() != null) {
requestUser.setNickName(myPageRequestDto.getNickName());
}

userRepository.save(requestUser);

}

@Transactional(readOnly = true)
public List<ArticleRequestDto> getMyList(Long userId) {
return null;
}



}
Loading