Skip to content

Commit

Permalink
feat : 마이페이지 기능 구현
Browse files Browse the repository at this point in the history
.
  • Loading branch information
lljh1992 committed Oct 16, 2023
1 parent b709ac9 commit 981b314
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 4 deletions.
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);

}
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;
}



}

0 comments on commit 981b314

Please sign in to comment.