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

[FIX] 게시글 이미지 여러개 저장되도록 수정 #134

Merged
merged 1 commit into from
Aug 18, 2024
Merged
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
@@ -0,0 +1,21 @@
package com.example.stepperbackend.converter;

import com.example.stepperbackend.domain.*;
import com.example.stepperbackend.web.dto.ImageDto;

public class ImageConverter {

public static Image toEntity(String url, Post post) {
return Image.builder()
.url(url)
.post(post)
.build();
}

public static ImageDto.ImageResponseDto toDto(Image image) {
return ImageDto.ImageResponseDto.builder()
.id(image.getId())
.imageUrl(image.getUrl())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.example.stepperbackend.converter;

import com.example.stepperbackend.domain.Image;
import com.example.stepperbackend.domain.Member;
import com.example.stepperbackend.domain.Post;
import com.example.stepperbackend.domain.WeeklyMission;
import com.example.stepperbackend.domain.enums.BodyPart;
import com.example.stepperbackend.web.dto.BadgeDto;
import com.example.stepperbackend.web.dto.ImageDto;
import com.example.stepperbackend.web.dto.PostDto;

import java.util.List;
import java.util.stream.Collectors;

public class PostConverter {

public static Post toEntity(PostDto.PostRequestDto dto, Member member, WeeklyMission weeklyMission) {
Expand All @@ -20,10 +26,13 @@ public static Post toEntity(PostDto.PostRequestDto dto, Member member, WeeklyMis
.build();
}

public static PostDto.PostResponseDto toDto(Post post) {
public static PostDto.PostResponseDto toDto(Post post, List<Image> imageList) {
List<ImageDto.ImageResponseDto> imageDtoList = imageList.stream()
.map(ImageConverter::toDto).collect(Collectors.toList());

return PostDto.PostResponseDto.builder()
.id(post.getId())
.imageUrl(post.getImageUrl())
//.imageUrl(post.getImageUrl())
.title(post.getTitle())
.body(post.getBody())
.bodyPart(post.getBodyPart().toString())
Expand All @@ -33,13 +42,17 @@ public static PostDto.PostResponseDto toDto(Post post) {
.weeklyMissionTitle(post.getWeeklyMission() != null ? post.getWeeklyMission().getMissionTitle() : null)
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.imageList(imageDtoList)
.build();
}

public static PostDto.PostViewDto toViewDto(Post post, int likes, int scraps, int commentsCount) {
public static PostDto.PostViewDto toViewDto(Post post, int likes, int scraps, int commentsCount, List<Image> imageList) {
List<ImageDto.ImageResponseDto> imageDtoList = imageList.stream()
.map(ImageConverter::toDto).collect(Collectors.toList());

return PostDto.PostViewDto.builder()
.id(post.getId())
.imageUrl(post.getImageUrl())
//.imageUrl(post.getImageUrl())
.title(post.getTitle())
.body(post.getBody())
.bodyPart(post.getBodyPart().toString())
Expand All @@ -53,6 +66,7 @@ public static PostDto.PostViewDto toViewDto(Post post, int likes, int scraps, in
.weeklyMissionTitle(post.getWeeklyMission() != null ? post.getWeeklyMission().getMissionTitle() : null)
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.imageList(imageDtoList)
.build();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/example/stepperbackend/domain/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.stepperbackend.domain;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Setter
@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Image {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "image_id")
private Long id;

private String url;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
}
3 changes: 3 additions & 0 deletions src/main/java/com/example/stepperbackend/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ public class Post extends BaseEntity {

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<Comment> CommentList = new ArrayList<>();

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<Image> imageList = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.stepperbackend.repository;

import com.example.stepperbackend.domain.Image;
import com.example.stepperbackend.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ImageRepository extends JpaRepository<Image, Long> {

List<Image> findAllByPost(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

public interface PostService {
PostDto.PostResponseDto createPost(MultipartFile image, PostDto.PostRequestDto postRequestDto, String email);
PostDto.PostResponseDto createPost(List<MultipartFile> image, PostDto.PostRequestDto postRequestDto, String email);

List<PostDto.PostViewDto> getPostsList(String email);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.example.stepperbackend.apiPayload.code.status.ErrorStatus;
import com.example.stepperbackend.apiPayload.exception.handler.*;
import com.example.stepperbackend.domain.Comment;
import com.example.stepperbackend.domain.Member;
import com.example.stepperbackend.domain.Post;
import com.example.stepperbackend.domain.WeeklyMission;
import com.example.stepperbackend.converter.ImageConverter;
import com.example.stepperbackend.domain.*;
import com.example.stepperbackend.domain.enums.BodyPart;
import com.example.stepperbackend.domain.mapping.Scrap;
import com.example.stepperbackend.repository.*;
Expand All @@ -32,12 +30,14 @@ public class PostServiceImpl implements PostService {
private final ScrapRepository scrapRepository;
private final CommentRepository commentRepository;
private final S3Service s3Service;
private final ImageRepository imageRepository;

@Override
public PostDto.PostResponseDto createPost(MultipartFile image, PostDto.PostRequestDto postRequestDto, String email) {
if (image != null && !image.isEmpty()) {
String imageUrl = s3Service.saveFile(image);
postRequestDto.setImageUrl(imageUrl);
public PostDto.PostResponseDto createPost(List<MultipartFile> images, PostDto.PostRequestDto postRequestDto, String email) {
//이미지 url 가져오기
List<String> imageUrlList = null;
if (images != null && !images.isEmpty()) {
imageUrlList = s3Service.saveFiles(images);
}

Member member = memberRepository.findByEmail(email)
Expand All @@ -48,16 +48,24 @@ public PostDto.PostResponseDto createPost(MultipartFile image, PostDto.PostReque
weeklyMission = weeklyMissionRepository.findById(postRequestDto.getWeeklyMissionId()).orElse(null);
}


Post post = PostConverter.toEntity(postRequestDto, member, weeklyMission);
post = postRepository.save(post);
postRepository.save(post);

//이미지 저장
List<Image> imageList = imageUrlList.stream()
.map(imageUrl -> {
Image image = ImageConverter.toEntity(imageUrl, post);
return imageRepository.save(image);
})
.collect(Collectors.toList());
post.setImageList(imageList);

// 첫 커뮤니티 게시글 작성 완료
if (postRepository.getCountByMember(member) == 1) {
badgeService.putFirstBadge("첫 게시글 작성 완료", member);
}

return PostConverter.toDto(post);
return PostConverter.toDto(post, imageList);
}

@Override
Expand All @@ -70,7 +78,8 @@ public List<PostDto.PostViewDto> getPostsList(String email) {
int likes = likeRepository.getCountByPost(post);
int scraps = scrapRepository.getCountByPost(post);
int comments = commentRepository.getCountByPost(post);
return PostConverter.toViewDto(post, scraps, likes, comments);
List<Image> imageList = imageRepository.findAllByPost(post);
return PostConverter.toViewDto(post, scraps, likes, comments, imageList);
})
.collect(Collectors.toList());

Expand All @@ -95,7 +104,9 @@ public PostDto.PostViewDto getPost(Long postId, String email) {

int commentsCount = commentRepository.getCountByPost(post);

return PostConverter.toViewDto(post, likes, scraps, commentsCount);
List<Image> imageList = imageRepository.findAllByPost(post);

return PostConverter.toViewDto(post, likes, scraps, commentsCount, imageList);
}

@Override
Expand All @@ -108,7 +119,8 @@ public List<PostDto.PostViewDto> getAllPost(String categoryName, String email) {
int likes = likeRepository.getCountByPost(post);
int scraps = scrapRepository.getCountByPost(post);
int comments = commentRepository.getCountByPost(post);
return PostConverter.toViewDto(post, scraps, likes, comments);
List<Image> imageList = imageRepository.findAllByPost(post);
return PostConverter.toViewDto(post, scraps, likes, comments, imageList);
})
.collect(Collectors.toList());

Expand All @@ -129,7 +141,8 @@ public List<PostDto.PostViewDto> getWeeklyPost(Long weeklyMissionId, String emai
int likes = likeRepository.getCountByPost(post);
int scraps = scrapRepository.getCountByPost(post);
int comments = commentRepository.getCountByPost(post);
return PostConverter.toViewDto(post, scraps, likes, comments);
List<Image> imageList = imageRepository.findAllByPost(post);
return PostConverter.toViewDto(post, scraps, likes, comments, imageList);
})
.collect(Collectors.toList());

Expand Down Expand Up @@ -157,7 +170,8 @@ public List<PostDto.PostViewDto> getCommentsList(String email) {
int likes = likeRepository.getCountByPost(post);
int scraps = scrapRepository.getCountByPost(post);
int commentsCount = commentRepository.getCountByPost(post);
return PostConverter.toViewDto(post, likes, scraps, commentsCount);
List<Image> imageList = imageRepository.findAllByPost(post);
return PostConverter.toViewDto(post, likes, scraps, commentsCount, imageList);
})
.distinct()
.toList();
Expand All @@ -180,7 +194,8 @@ public List<PostDto.PostViewDto> getScrapList(String email) {
int likes = likeRepository.getCountByPost(post);
int scrapsCount = scrapRepository.getCountByPost(post);
int commentsCount = commentRepository.getCountByPost(post);
return PostConverter.toViewDto(post, likes, scrapsCount, commentsCount);
List<Image> imageList = imageRepository.findAllByPost(post);
return PostConverter.toViewDto(post, likes, scrapsCount, commentsCount, imageList);
})
.distinct()
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class PostController {
@Operation(summary = "게시글 작성 API", description = "사용자 게시글 작성")
@PostMapping("/write")
public ApiResponse<PostDto.PostResponseDto> createPost(@RequestPart("data") PostDto.PostRequestDto postRequestDto,
@RequestPart(value = "image", required = false) MultipartFile image,
@RequestPart(value = "image", required = false) List<MultipartFile> images,
HttpServletRequest request) {
String token = request.getHeader("Authorization").substring(7);
String email = jwtUtil.getUsername(token);
PostDto.PostResponseDto response = postService.createPost(image, postRequestDto, email);
PostDto.PostResponseDto response = postService.createPost(images, postRequestDto, email);
return ApiResponse.onSuccess(response);
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/example/stepperbackend/web/dto/ImageDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.stepperbackend.web.dto;

import lombok.Builder;
import lombok.Data;


public class ImageDto {

@Data
@Builder
public static class ImageResponseDto {
private Long id;
private String imageUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

public class PostDto {

Expand All @@ -31,13 +32,15 @@ public static class PostResponseDto {
private String weeklyMissionTitle;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private List<ImageDto.ImageResponseDto> imageList;

}

@Data
@Builder
public static class PostViewDto {
private Long id;
private String imageUrl;
//private String imageUrl;
private String title;
private String body;
private String authorEmail;
Expand All @@ -49,5 +52,6 @@ public static class PostViewDto {
private String weeklyMissionTitle;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private List<ImageDto.ImageResponseDto> imageList;
}
}
Loading