Skip to content

Commit

Permalink
feat: 이미지와 포스트를 저장하는 임시 메서드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxklee committed Nov 8, 2024
1 parent 39eb431 commit 318f660
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/main/java/com/leets/X/domain/post/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.leets.X.domain.post.service;

import com.leets.X.domain.image.domain.Image;
import com.leets.X.domain.image.service.ImageService;
import com.leets.X.domain.like.domain.Like;
import com.leets.X.domain.like.repository.LikeRepository;
import com.leets.X.domain.post.controller.ResponseMessage;
import com.leets.X.domain.post.domain.Post;
import com.leets.X.domain.post.domain.enums.IsDeleted;
import com.leets.X.domain.post.dto.request.PostRequestDTO;
Expand All @@ -16,11 +17,12 @@
import com.leets.X.domain.user.domain.User;
import com.leets.X.domain.user.exception.UserNotFoundException;
import com.leets.X.domain.user.service.UserService;
import com.leets.X.global.common.response.ResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
Expand All @@ -33,6 +35,7 @@ public class PostService {
private final PostRepository postRepository;
private final UserService userService;
private final LikeRepository likeRepository;
private final ImageService imageService;

// 게시물 ID로 조회
public PostResponseDto getPostResponse(Long id) {
Expand Down Expand Up @@ -157,4 +160,21 @@ public PostUserResponse findUser(String email) {
User user = userService.find(email);
return PostUserResponse.from(user); // PostUserResponse로 변환하여 반환
}

@Transactional
public PostResponseDto createPostImage(PostRequestDTO postRequestDTO, List<MultipartFile> files) throws IOException {
// 이메일로 사용자 조회
User user = userService.find("[email protected]"); // JWT에서 추출한 이메일 사용

Post post = Post.create(user, postRequestDTO.content()); // 글 생성 로직 캡슐화
Post savedPost = postRepository.save(post);

if (files != null) {
List<Image> images = imageService.save(files, savedPost);
post.addImage(images);
}

// 저장된 게시글을 ResponseDto에 담아 반환
return PostResponseDto.from(savedPost);
}
}

0 comments on commit 318f660

Please sign in to comment.