2020import com .example .FixLog .repository .post .PostRepository ;
2121import com .example .FixLog .repository .tag .TagRepository ;
2222import jakarta .transaction .Transactional ;
23- import org .springframework .security .core .Authentication ;
2423import org .springframework .security .core .context .SecurityContextHolder ;
2524import org .springframework .stereotype .Service ;
25+ import org .springframework .util .StringUtils ;
2626import org .springframework .web .multipart .MultipartFile ;
2727
2828import java .time .LocalDate ;
@@ -55,13 +55,20 @@ public PostService(PostRepository postRepository, PostLikeRepository postLikeRep
5555 this .s3Service = s3Service ;
5656 }
5757
58- // 이미지 null일 때 default 사진으로 변경 ( 프로필 사진,
59- public String getDefaultImage (String image ){
58+ // 이미지 null일 때 default 사진으로 변경 - 프로필 사진
59+ public String getDefaultProfile (String image ){
6060 String imageUrl = (image == null || image .isBlank ())
6161 ? "https://fixlog-bucket.s3.ap-northeast-2.amazonaws.com/default/profile.png" : image ;
6262 System .out .println (imageUrl );
6363 return imageUrl ;
6464 }
65+ // 이미지 null일 때 default 사진으로 변경 - 썸네일
66+ public String getDefaultCover (String image ){
67+ String imageUrl = (image == null || image .isBlank ())
68+ ? "https://fixlogsmwubucket.s3.ap-northeast-2.amazonaws.com/default/DefaulThumnail.png" : image ;
69+ System .out .println (imageUrl );
70+ return imageUrl ;
71+ }
6572
6673 // 게시글 작성하기
6774 @ Transactional
@@ -144,15 +151,21 @@ else if (categories.size() > 1)
144151
145152 // 게시글 필수 항목 다 작성했는지
146153 private void validatePost (PostRequestDto postRequestDto ){
147- if (postRequestDto .getPostTitle ().isBlank () | postRequestDto .getProblem ().isBlank ()
148- | postRequestDto .getErrorMessage ().isBlank () | postRequestDto .getEnvironment ().isBlank ()
149- | postRequestDto .getReproduceCode ().isBlank () | postRequestDto .getSolutionCode ().isBlank ())
154+ if (!StringUtils .hasText (postRequestDto .getPostTitle ())
155+ || !StringUtils .hasText (postRequestDto .getProblem ())
156+ || !StringUtils .hasText (postRequestDto .getErrorMessage ())
157+ || !StringUtils .hasText (postRequestDto .getEnvironment ())
158+ || !StringUtils .hasText (postRequestDto .getReproduceCode ())
159+ || !StringUtils .hasText (postRequestDto .getSolutionCode ()))
150160 throw new CustomException (ErrorCode .REQUIRED_CONTENT_MISSING );
151161 }
152162 private void validatePost (NewPostRequestDto newPostRequestDto ){
153- if (newPostRequestDto .getPostTitle ().isBlank () | newPostRequestDto .getProblem ().isBlank ()
154- | newPostRequestDto .getErrorMessage ().isBlank () | newPostRequestDto .getEnvironment ().isBlank ()
155- | newPostRequestDto .getReproduceCode ().isBlank () | newPostRequestDto .getSolutionCode ().isBlank ())
163+ if (!StringUtils .hasText (newPostRequestDto .getPostTitle ())
164+ || !StringUtils .hasText (newPostRequestDto .getProblem ())
165+ || !StringUtils .hasText (newPostRequestDto .getErrorMessage ())
166+ || !StringUtils .hasText (newPostRequestDto .getEnvironment ())
167+ || !StringUtils .hasText (newPostRequestDto .getReproduceCode ())
168+ || !StringUtils .hasText (newPostRequestDto .getSolutionCode ()))
156169 throw new CustomException (ErrorCode .REQUIRED_CONTENT_MISSING );
157170 }
158171
@@ -169,12 +182,18 @@ public String uploadImage(MultipartFile imageFile){
169182 return "" ;
170183 }
171184
185+ // 게시글 수정하기
172186 @ Transactional
173187 public void editPost (Long postId , NewPostRequestDto newPostRequestDto ) {
174188 Member member = memberService .getCurrentMemberInfo ();
175189 Post post = postRepository .findById (postId )
176190 .orElseThrow (() -> new CustomException (ErrorCode .POST_NOT_FOUND ));
177191
192+ // 게시글 작성자가 본인이 맞는지
193+ if (!member .getUserId ().equals (post .getUserId ().getUserId ())) {
194+ throw new CustomException (ErrorCode .POST_UPDATE_FORBIDDEN );
195+ }
196+
178197 // 북마크 카테고리별로 선택 제한 두기
179198 List <Tag > tags = fetchAndValidateTags (newPostRequestDto .getTags ());
180199
@@ -194,6 +213,8 @@ & compareTags(post.getPostTags(), tags)){
194213 }
195214
196215 // 필드 업데이트
216+ validatePost (newPostRequestDto );
217+
197218 post .changeTitle (newPostRequestDto .getPostTitle ());
198219 post .changeCoverImage (newPostRequestDto .getCoverImageUrl ());
199220 post .changeProblem (newPostRequestDto .getProblem ());
@@ -233,7 +254,7 @@ public PostResponseDto viewPost(Long postId){
233254 currentPost .getUserId ().getUserId (),
234255 currentPost .getUserId ().getNickname (),
235256 currentPost .getPostTitle (),
236- getDefaultImage (currentPost .getCoverImage ()),
257+ getDefaultCover (currentPost .getCoverImage ()),
237258 currentPost .getProblem (),
238259 currentPost .getErrorMessage (),
239260 currentPost .getEnvironment (),
@@ -253,15 +274,15 @@ public PostResponseDto viewPost(Long postId){
253274 Member member = optionalMember .get ();
254275 nickname = member .getNickname ();
255276 String imageUrl = member .getProfileImageUrl ();
256- profileImageUrl = getDefaultImage (imageUrl );
277+ profileImageUrl = getDefaultProfile (imageUrl );
257278
258279 isLiked = currentPost .getPostLikes ().stream ()
259280 .anyMatch (postLike -> postLike .getUserId ().equals (member ));
260281 isMarked = currentPost .getBookmarks ().stream ()
261282 .anyMatch (bookmark -> bookmark .getFolderId ().getUserId ().equals (member ));
262283 } else {
263284 nickname = "로그인하지 않았습니다." ;
264- profileImageUrl = "https://example. com/default-cover-image .png" ; // 비로그인 기본 이미지
285+ profileImageUrl = "https://fixlog-bucket.s3.ap-northeast-2.amazonaws. com/default/profile .png" ; // 비로그인 기본 이미지
265286 isLiked = false ;
266287 isMarked = false ;
267288 }
0 commit comments