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

Article Entity에 userId 추가 #16

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public record ArticleGetResponse(
Long id,
String userName,
String title,
String content,
String schoolName,
Expand All @@ -15,6 +16,7 @@ public record ArticleGetResponse(
public static ArticleGetResponse of(ArticleEntity articleEntity){
return new ArticleGetResponse(
articleEntity.getId(),
articleEntity.getUserName(),
articleEntity.getTitle(),
articleEntity.getContent(),
articleEntity.getSchoolName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class ArticleEntity implements ArticleModel{
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String userName;

@Column(nullable = false)
@Length(min = 1, max = 20)
private String title;
Expand All @@ -34,7 +36,8 @@ public class ArticleEntity implements ArticleModel{

protected ArticleEntity(){}

protected ArticleEntity(String title, String content, String schoolName, List<String> images, List<String> tags){
protected ArticleEntity(String userName, String title, String content, String schoolName, List<String> images, List<String> tags){
this.userName = userName;
this.title = title;
this.content = content;
this.schoolName = schoolName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface ArticleModel {
List<String> getTags();
void update(ArticleRequest articleRequest, List<MultipartFile> images);

static ArticleEntity createArticleEntity(String title, String content, String schoolName, List<String> images, List<String> tags) {
return new ArticleEntity(title, content, schoolName, images, tags);
static ArticleEntity createArticleEntity(String userName, String title, String content, String schoolName, List<String> images, List<String> tags) {
return new ArticleEntity(userName, title, content, schoolName, images, tags);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

service 로직에서 직접 SecurityContextHolder에 접근하는 건 살짝 안 좋은 설계 같아요.
SecurityContextHolder에 접근하는 클래스를 따로 나누면 좋을 것 같네요

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import com.meogo.meogo_backend.domain.community.article.entity.ArticleModel;
import com.meogo.meogo_backend.domain.community.article.repository.ArticleRepository;
import com.meogo.meogo_backend.domain.community.article.usecase.ArticleUseCase;
import com.meogo.meogo_backend.domain.user.model.UserEntity;
import com.meogo.meogo_backend.global.current.CurrentUser;
import com.meogo.meogo_backend.global.exception.custom.NotFoundArticleException;
import com.meogo.meogo_backend.global.security.jwt.JwtProperties;
import com.meogo.meogo_backend.global.security.jwt.Tokenizer;
import io.jsonwebtoken.Jwts;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -50,6 +48,7 @@ public List<ArticleGetResponse> findAll() {

private ArticleEntity createEntity(ArticleRequest request, List<MultipartFile> images) {
return ArticleModel.createArticleEntity(
(UserEntity) currentUser.getCurrentUser(),
request.title(),
request.content(),
request.schoolName(),
Expand All @@ -59,4 +58,5 @@ private ArticleEntity createEntity(ArticleRequest request, List<MultipartFile> i
}

private final ArticleRepository repository;
private final CurrentUser currentUser;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.meogo.meogo_backend.global.current;

import com.meogo.meogo_backend.domain.user.model.UserModel;
import org.springframework.security.core.Authentication;

public interface CurrentUser {
UserModel getCurrentUser();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.meogo.meogo_backend.global.current;

import com.meogo.meogo_backend.domain.user.model.UserModel;
import com.meogo.meogo_backend.domain.user.repository.UserRepository;
import com.meogo.meogo_backend.global.exception.custom.UserNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CurrentUserImpl implements CurrentUser {
@Override
public UserModel getCurrentUser() {
String userId = SecurityContextHolder.getContext().getAuthentication().getName();

return userRepository.findByUserId(userId)
.orElseThrow(() -> UserNotFoundException.EXCEPTION );
}

private final UserRepository userRepository;
}