-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPostCommandService.java
More file actions
151 lines (129 loc) · 6.28 KB
/
PostCommandService.java
File metadata and controls
151 lines (129 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.example.solidconnection.community.post.service;
import static com.example.solidconnection.common.exception.ErrorCode.CAN_NOT_DELETE_OR_UPDATE_QUESTION;
import static com.example.solidconnection.common.exception.ErrorCode.CAN_NOT_UPLOAD_MORE_THAN_FIVE_IMAGES;
import static com.example.solidconnection.common.exception.ErrorCode.DUPLICATE_POST_CREATE_REQUEST;
import static com.example.solidconnection.common.exception.ErrorCode.INVALID_POST_ACCESS;
import static com.example.solidconnection.common.exception.ErrorCode.INVALID_POST_CATEGORY;
import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND;
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.community.board.domain.Board;
import com.example.solidconnection.community.board.repository.BoardRepository;
import com.example.solidconnection.community.post.domain.Post;
import com.example.solidconnection.community.post.domain.PostCategory;
import com.example.solidconnection.community.post.domain.PostImage;
import com.example.solidconnection.community.post.dto.PostCreateRequest;
import com.example.solidconnection.community.post.dto.PostCreateResponse;
import com.example.solidconnection.community.post.dto.PostDeleteResponse;
import com.example.solidconnection.community.post.dto.PostUpdateRequest;
import com.example.solidconnection.community.post.dto.PostUpdateResponse;
import com.example.solidconnection.community.post.repository.PostRepository;
import com.example.solidconnection.s3.domain.UploadPath;
import com.example.solidconnection.s3.dto.UploadedFileUrlResponse;
import com.example.solidconnection.s3.service.S3Service;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@Service
@RequiredArgsConstructor
public class PostCommandService {
private final PostRepository postRepository;
private final BoardRepository boardRepository;
private final SiteUserRepository siteUserRepository;
private final S3Service s3Service;
private final PostRedisManager postRedisManager;
@Transactional
public PostCreateResponse createPost(long siteUserId, PostCreateRequest postCreateRequest,
List<MultipartFile> imageFile) {
SiteUser siteUser = siteUserRepository.findById(siteUserId)
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
// 유효성 검증
validatePostCategory(postCreateRequest.postCategory());
validateFileSize(imageFile);
// 중복 생성 방지
if (!postRedisManager.isPostCreationAllowed(siteUserId)) {
throw new CustomException(DUPLICATE_POST_CREATE_REQUEST);
}
// 객체 생성
Board board = boardRepository.getByCode(postCreateRequest.boardCode());
Post post = postCreateRequest.toEntity(siteUser, board);
// 이미지 처리
savePostImages(imageFile, post);
Post createdPost = postRepository.save(post);
return PostCreateResponse.from(createdPost);
}
@Transactional
public PostUpdateResponse updatePost(long siteUserId, Long postId, PostUpdateRequest postUpdateRequest,
List<MultipartFile> imageFile) {
SiteUser siteUser = siteUserRepository.findById(siteUserId)
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
// 유효성 검증
Post post = postRepository.getById(postId);
validatePostCategory(postUpdateRequest.postCategory());
validateOwnership(post, siteUser);
validateQuestion(post);
validateFileSize(imageFile);
// 기존 사진 모두 삭제
removePostImages(post);
// 새로운 이미지 등록
savePostImages(imageFile, post);
// 게시글 내용 수정
post.update(postUpdateRequest);
return PostUpdateResponse.from(post);
}
private void savePostImages(List<MultipartFile> imageFile, Post post) {
if (imageFile.isEmpty()) {
return;
}
List<UploadedFileUrlResponse> uploadedFileUrlResponseList = s3Service.uploadFiles(imageFile, UploadPath.COMMUNITY);
for (UploadedFileUrlResponse uploadedFileUrlResponse : uploadedFileUrlResponseList) {
PostImage postImage = new PostImage(uploadedFileUrlResponse.fileUrl());
postImage.setPost(post);
}
}
@Transactional
public PostDeleteResponse deletePostById(long siteUserId, Long postId) {
SiteUser siteUser = siteUserRepository.findById(siteUserId)
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
Post post = postRepository.getById(postId);
validateOwnership(post, siteUser);
validateQuestion(post);
removePostImages(post);
postRedisManager.deleteViewCountCache(postId);
postRepository.deleteById(post.getId());
return new PostDeleteResponse(postId);
}
private void validateOwnership(Post post, SiteUser siteUser) {
if (!Objects.equals(post.getSiteUserId(), siteUser.getId())) {
throw new CustomException(INVALID_POST_ACCESS);
}
}
private void validateFileSize(List<MultipartFile> imageFile) {
if (imageFile.isEmpty()) {
return;
}
if (imageFile.size() > 5) {
throw new CustomException(CAN_NOT_UPLOAD_MORE_THAN_FIVE_IMAGES);
}
}
private void validateQuestion(Post post) {
if (post.getIsQuestion()) {
throw new CustomException(CAN_NOT_DELETE_OR_UPDATE_QUESTION);
}
}
private void validatePostCategory(String category) {
if (!PostCategory.isValid(category) || category.equals(PostCategory.전체.toString())) {
throw new CustomException(INVALID_POST_CATEGORY);
}
}
private void removePostImages(Post post) {
for (PostImage postImage : post.getPostImageList()) {
s3Service.deletePostImage(postImage.getUrl());
}
post.getPostImageList().clear();
}
}