diff --git a/src/main/java/com/meogo/meogo_backend/domain/community/article/dto/ArticleGetResponse.java b/src/main/java/com/meogo/meogo_backend/domain/community/article/dto/ArticleGetResponse.java index 8cf0272..3da582b 100644 --- a/src/main/java/com/meogo/meogo_backend/domain/community/article/dto/ArticleGetResponse.java +++ b/src/main/java/com/meogo/meogo_backend/domain/community/article/dto/ArticleGetResponse.java @@ -6,6 +6,7 @@ public record ArticleGetResponse( Long id, + String userName, String title, String content, String schoolName, @@ -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(), diff --git a/src/main/java/com/meogo/meogo_backend/domain/community/article/entity/ArticleEntity.java b/src/main/java/com/meogo/meogo_backend/domain/community/article/entity/ArticleEntity.java index d138040..62a6047 100644 --- a/src/main/java/com/meogo/meogo_backend/domain/community/article/entity/ArticleEntity.java +++ b/src/main/java/com/meogo/meogo_backend/domain/community/article/entity/ArticleEntity.java @@ -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; @@ -34,7 +36,8 @@ public class ArticleEntity implements ArticleModel{ protected ArticleEntity(){} - protected ArticleEntity(String title, String content, String schoolName, List images, List tags){ + protected ArticleEntity(String userName, String title, String content, String schoolName, List images, List tags){ + this.userName = userName; this.title = title; this.content = content; this.schoolName = schoolName; diff --git a/src/main/java/com/meogo/meogo_backend/domain/community/article/entity/ArticleModel.java b/src/main/java/com/meogo/meogo_backend/domain/community/article/entity/ArticleModel.java index ea9c97d..bdc1822 100644 --- a/src/main/java/com/meogo/meogo_backend/domain/community/article/entity/ArticleModel.java +++ b/src/main/java/com/meogo/meogo_backend/domain/community/article/entity/ArticleModel.java @@ -14,7 +14,7 @@ public interface ArticleModel { List getTags(); void update(ArticleRequest articleRequest, List images); - static ArticleEntity createArticleEntity(String title, String content, String schoolName, List images, List tags) { - return new ArticleEntity(title, content, schoolName, images, tags); + static ArticleEntity createArticleEntity(String userName, String title, String content, String schoolName, List images, List tags) { + return new ArticleEntity(userName, title, content, schoolName, images, tags); } } diff --git a/src/main/java/com/meogo/meogo_backend/domain/community/article/service/ArticleService.java b/src/main/java/com/meogo/meogo_backend/domain/community/article/service/ArticleService.java index 4e547f9..35a30d3 100644 --- a/src/main/java/com/meogo/meogo_backend/domain/community/article/service/ArticleService.java +++ b/src/main/java/com/meogo/meogo_backend/domain/community/article/service/ArticleService.java @@ -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; @@ -50,6 +48,7 @@ public List findAll() { private ArticleEntity createEntity(ArticleRequest request, List images) { return ArticleModel.createArticleEntity( + (UserEntity) currentUser.getCurrentUser(), request.title(), request.content(), request.schoolName(), @@ -59,4 +58,5 @@ private ArticleEntity createEntity(ArticleRequest request, List i } private final ArticleRepository repository; + private final CurrentUser currentUser; } diff --git a/src/main/java/com/meogo/meogo_backend/global/current/CurrentUser.java b/src/main/java/com/meogo/meogo_backend/global/current/CurrentUser.java new file mode 100644 index 0000000..e880211 --- /dev/null +++ b/src/main/java/com/meogo/meogo_backend/global/current/CurrentUser.java @@ -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(); +} diff --git a/src/main/java/com/meogo/meogo_backend/global/current/CurrentUserImpl.java b/src/main/java/com/meogo/meogo_backend/global/current/CurrentUserImpl.java new file mode 100644 index 0000000..543cf5f --- /dev/null +++ b/src/main/java/com/meogo/meogo_backend/global/current/CurrentUserImpl.java @@ -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; +}