diff --git a/src/main/java/com/example/FixLog/controller/PostController.java b/src/main/java/com/example/FixLog/controller/PostController.java index 9497952..84d6601 100644 --- a/src/main/java/com/example/FixLog/controller/PostController.java +++ b/src/main/java/com/example/FixLog/controller/PostController.java @@ -1,6 +1,5 @@ package com.example.FixLog.controller; -import com.example.FixLog.dto.post.NewPostRequestDto; import com.example.FixLog.dto.post.PostRequestDto; import com.example.FixLog.dto.Response; import com.example.FixLog.dto.post.PostResponseDto; @@ -34,8 +33,8 @@ public Response uploadImage(@RequestPart("imageFile") MultipartFile imag // 게시글 수정하기 @PatchMapping("/{postId}/edit") public Response editPost(@PathVariable("postId") Long postId, - @RequestBody NewPostRequestDto newPostRequestDto){ - postService.editPost(postId, newPostRequestDto); + @RequestBody PostRequestDto postRequestDto){ + postService.editPost(postId, postRequestDto); return Response.success("게시글 수정 성공.", null); } diff --git a/src/main/java/com/example/FixLog/domain/tag/Tag.java b/src/main/java/com/example/FixLog/domain/tag/Tag.java index 69fa2b6..c4297d4 100644 --- a/src/main/java/com/example/FixLog/domain/tag/Tag.java +++ b/src/main/java/com/example/FixLog/domain/tag/Tag.java @@ -21,9 +21,15 @@ public class Tag { @Column(length = 20, nullable = false) private String tagName; - @Column(nullable = false) private String tagInfo; + public static Tag of(TagCategory tagCategory, String tagName) { + Tag tag = new Tag(); + tag.tagCategory = tagCategory; + tag.tagName = tagName; + return tag; + } + public static Tag of(TagCategory tagCategory, String tagName, String tagInfo) { Tag tag = new Tag(); tag.tagCategory = tagCategory; diff --git a/src/main/java/com/example/FixLog/dto/post/MyPostPageResponseDto.java b/src/main/java/com/example/FixLog/dto/post/MyPostPageResponseDto.java index 8bc8529..7a94dbe 100644 --- a/src/main/java/com/example/FixLog/dto/post/MyPostPageResponseDto.java +++ b/src/main/java/com/example/FixLog/dto/post/MyPostPageResponseDto.java @@ -23,6 +23,22 @@ public class MyPostPageResponseDto { private int likeCount; private int forkCount; private String nickname; + private String profileImageUrl; + + // 이미지 null일 때 default 사진으로 변경 - 프로필 사진 + public static String getDefaultProfile(String image){ + String imageUrl = (image == null || image.isBlank()) + ? "https://fixlog-bucket.s3.ap-northeast-2.amazonaws.com/default/profile.png" : image; + System.out.println(imageUrl); + return imageUrl; + } + // 이미지 null일 때 default 사진으로 변경 - 썸네일 + public static String getDefaultCover(String image){ + String imageUrl = (image == null || image.isBlank()) + ? "https://fixlogsmwubucket.s3.ap-northeast-2.amazonaws.com/default/DefaulThumnail.png" : image; + System.out.println(imageUrl); + return imageUrl; + } public static MyPostPageResponseDto from(Post post, int forkCount) { return MyPostPageResponseDto.builder() @@ -30,7 +46,8 @@ public static MyPostPageResponseDto from(Post post, int forkCount) { .nickname(post.getUserId().getNickname()) .postTitle(post.getPostTitle()) .postSummary(generateSummary(post.getProblem())) - .imageUrl(post.getCoverImage()) + .imageUrl(getDefaultCover(post.getCoverImage())) + .profileImageUrl(getDefaultProfile(post.getUserId().getProfileImageUrl())) .tags(post.getPostTags().stream().map(tag -> tag.getTagId().getTagName()).toList()) .createdAt(post.getCreatedAt()) .likeCount(post.getPostLikes().size()) diff --git a/src/main/java/com/example/FixLog/dto/post/NewPostRequestDto.java b/src/main/java/com/example/FixLog/dto/post/NewPostRequestDto.java deleted file mode 100644 index 34c4696..0000000 --- a/src/main/java/com/example/FixLog/dto/post/NewPostRequestDto.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.FixLog.dto.post; - -import lombok.Getter; - -import java.util.List; - -@Getter -public class NewPostRequestDto { - private String postTitle; - private String coverImageUrl; - private String problem; - private String errorMessage; - private String environment; - private String reproduceCode; - private String solutionCode; - private String causeAnalysis; - private String referenceLink; - private String extraContent; - - private List tags; -} diff --git a/src/main/java/com/example/FixLog/dto/post/PostRequestDto.java b/src/main/java/com/example/FixLog/dto/post/PostRequestDto.java index 558c662..6efad06 100644 --- a/src/main/java/com/example/FixLog/dto/post/PostRequestDto.java +++ b/src/main/java/com/example/FixLog/dto/post/PostRequestDto.java @@ -17,5 +17,5 @@ public class PostRequestDto { private String referenceLink; private String extraContent; - private List tags; + private List tags; } diff --git a/src/main/java/com/example/FixLog/exception/CustomException.java b/src/main/java/com/example/FixLog/exception/CustomException.java index 3c61011..4db97c7 100644 --- a/src/main/java/com/example/FixLog/exception/CustomException.java +++ b/src/main/java/com/example/FixLog/exception/CustomException.java @@ -4,7 +4,16 @@ import lombok.RequiredArgsConstructor; @Getter -@RequiredArgsConstructor public class CustomException extends RuntimeException { private final ErrorCode errorCode; + + public CustomException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } + + public CustomException(ErrorCode errorCode, String detailMessage) { + super(detailMessage); + this.errorCode = errorCode; + } } diff --git a/src/main/java/com/example/FixLog/exception/ErrorCode.java b/src/main/java/com/example/FixLog/exception/ErrorCode.java index db86c04..9e64cb0 100644 --- a/src/main/java/com/example/FixLog/exception/ErrorCode.java +++ b/src/main/java/com/example/FixLog/exception/ErrorCode.java @@ -24,6 +24,7 @@ public enum ErrorCode { TAG_NOT_FOUND(HttpStatus.NOT_FOUND, "없는 태그 번호입니다."), SORT_NOT_EXIST(HttpStatus.BAD_REQUEST, "사용할 수 없는 정렬입니다."), INVALID_PASSWORD(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다."), + POST_UPDATE_FORBIDDEN(HttpStatus.FORBIDDEN, "본인 게시글만 수정할 수 있습니다."), REQUIRED_TAGS_MISSING(HttpStatus.BAD_REQUEST, "태그를 선택해주세요."), REQUIRED_CONTENT_MISSING(HttpStatus.BAD_REQUEST, "필수 본문이 입력되지 않았습니다."), SAME_AS_OLD_PASSWORD(HttpStatus.BAD_REQUEST, "다른 비밀번호 입력 바랍니다."), diff --git a/src/main/java/com/example/FixLog/mock/TagMockDataInitializer.java b/src/main/java/com/example/FixLog/mock/TagMockDataInitializer.java index 51d3ee9..7284d14 100644 --- a/src/main/java/com/example/FixLog/mock/TagMockDataInitializer.java +++ b/src/main/java/com/example/FixLog/mock/TagMockDataInitializer.java @@ -32,45 +32,46 @@ public void run(String... args) { List tagsToInsert = new ArrayList<>(); // BIG_CATEGORY - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "백엔드", "backend")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "머신러닝", "machine learning")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "프론트엔드", "frontend")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "backend")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "machine-learning")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "frontend")); // MAJOR_CATEGORY - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "장고", "django")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "스프링부트", "spring-boot")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "넥스트", "next.js")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "케라스", "keras")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "파이토치", "pytorch")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "사이킷런", "scikit-learn")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "노드", "node.js")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "리액트", "react")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "리액트 네이티브", "react-native")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "django")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "spring-boot")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "next.js")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "keras")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "pytorch")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "scikit-learn")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "node.js")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "react")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "react-native")); // MIDDLE_CATEGORY - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "CSS", "css")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "자바스크립트", "javascript")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "R", "r")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "JSON", "json")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "자바", "java")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "Haskell", "haskell")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "파이썬", "python")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "C", "c")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "css")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "javascript")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "r")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "json")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "java")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "haskell")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "python")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "c")); // MINOR_CATEGORY - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "NullPointerException", "null-pointer-exception")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "500 Internal Server Error", "500-error")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "CORS 정책 오류", "cors-error")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "Database connection timeout", "db-timeout")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "ClassNotFoundException", "class-not-found")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "Cannot read property of undefined", "undefined-property")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "상태(state) 업데이트 누락", "state-missing")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "HTTP 에러", "http-error")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "렌더링 무한 루프", "render-loop")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "스타일 깨짐", "style-break")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "404 Not Found", "404-error")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "Permission Error", "permission-error")); - addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "OutOfMemoryError", "out-of-memory")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "null-pointer-exception", "NullPointerException")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "500-error", "500 Internal Server Error")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "cors-error", "CORS 정책 오류")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "db-timeout", "Database connection timeout")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "class-not-found", "ClassNotFoundException")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "undefined-property", "Cannot read property of undefined")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "state-missing", "상태(state) 업데이트 누락")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "http-error", "HTTP 에러")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "render-loop", "렌더링 무한 루프")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "style-break", "스타일 깨짐")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "404-error", "404 Not Found")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "permission-error", "Permission Error")); + addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "out-of-memory", "OutOfMemoryError")); + if (!tagsToInsert.isEmpty()) { tagRepository.saveAll(tagsToInsert); diff --git a/src/main/java/com/example/FixLog/repository/tag/TagRepository.java b/src/main/java/com/example/FixLog/repository/tag/TagRepository.java index 29ef940..af00f35 100644 --- a/src/main/java/com/example/FixLog/repository/tag/TagRepository.java +++ b/src/main/java/com/example/FixLog/repository/tag/TagRepository.java @@ -5,6 +5,9 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface TagRepository extends JpaRepository { Page findAll(Pageable pageable); + Optional findByTagName(String tagName); } diff --git a/src/main/java/com/example/FixLog/service/MainPageService.java b/src/main/java/com/example/FixLog/service/MainPageService.java index 3b27b2d..9c8ff00 100644 --- a/src/main/java/com/example/FixLog/service/MainPageService.java +++ b/src/main/java/com/example/FixLog/service/MainPageService.java @@ -27,13 +27,20 @@ public MainPageService(PostRepository postRepository, MemberService memberServic this.memberService = memberService; } - // 이미지 null일 때 default 사진으로 변경 (프로필 사진, - public String getDefaultImage(String image){ + // 이미지 null일 때 default 사진으로 변경 - 프로필 사진 + public String getDefaultProfile(String image){ String imageUrl = (image == null || image.isBlank()) ? "https://fixlog-bucket.s3.ap-northeast-2.amazonaws.com/default/profile.png" : image; System.out.println(imageUrl); return imageUrl; } + // 이미지 null일 때 default 사진으로 변경 - 썸네일 + public String getDefaultCover(String image){ + String imageUrl = (image == null || image.isBlank()) + ? "https://fixlogsmwubucket.s3.ap-northeast-2.amazonaws.com/default/DefaulThumnail.png" : image; + System.out.println(imageUrl); + return imageUrl; + } // 메인페이지 보기 public MainPageResponseDto mainPageView(int sort, int size){ @@ -44,9 +51,9 @@ public MainPageResponseDto mainPageView(int sort, int size){ if (optionalMember.isPresent()) { Member member = optionalMember.get(); String imageUrl = member.getProfileImageUrl(); - profileImageUrl = getDefaultImage(imageUrl); + profileImageUrl = getDefaultProfile(imageUrl); } else { - profileImageUrl = "https://example.com/default-cover-image.png"; // 비로그인 기본 이미지 + profileImageUrl = "https://fixlog-bucket.s3.ap-northeast-2.amazonaws.com/default/profile.png"; // 비로그인 기본 이미지 } // 페이지 (글 12개) 불러오기 @@ -67,11 +74,11 @@ public MainPageResponseDto mainPageView(int sort, int size){ List postList = posts.stream() .map(post -> new MainPagePostResponseDto( post.getPostTitle(), - getDefaultImage(post.getCoverImage()), + getDefaultCover(post.getCoverImage()), post.getPostTags().stream() .map(postTag -> postTag.getTagId().getTagName()) .collect(Collectors.toList()), - getDefaultImage(post.getUserId().getProfileImageUrl()), + getDefaultProfile(post.getUserId().getProfileImageUrl()), post.getUserId().getNickname(), post.getCreatedAt().toLocalDate(), post.getPostLikes().size() @@ -90,9 +97,9 @@ public MainPageResponseDto mainPageFullView(int sort, int page, int size){ if (optionalMember.isPresent()) { Member member = optionalMember.get(); String imageUrl = member.getProfileImageUrl(); - profileImageUrl = getDefaultImage(imageUrl); + profileImageUrl = getDefaultProfile(imageUrl); } else { - profileImageUrl = "https://example.com/default-cover-image.png"; // 비로그인 기본 이미지 + profileImageUrl = "https://fixlog-bucket.s3.ap-northeast-2.amazonaws.com/default/profile.png"; // 비로그인 기본 이미지 } // 페이지 설정 (한 페이지당 12개) @@ -109,11 +116,11 @@ public MainPageResponseDto mainPageFullView(int sort, int page, int size){ List postList = postPage.stream() .map(post -> new MainPagePostResponseDto( post.getPostTitle(), - getDefaultImage(post.getCoverImage()), + getDefaultCover(post.getCoverImage()), post.getPostTags().stream() .map(postTag -> postTag.getTagId().getTagName()) .collect(Collectors.toList()), - getDefaultImage(post.getUserId().getProfileImageUrl()), + getDefaultProfile(post.getUserId().getProfileImageUrl()), post.getUserId().getNickname(), post.getCreatedAt().toLocalDate(), post.getPostLikes().size() diff --git a/src/main/java/com/example/FixLog/service/PostService.java b/src/main/java/com/example/FixLog/service/PostService.java index 6970fe5..2197a18 100644 --- a/src/main/java/com/example/FixLog/service/PostService.java +++ b/src/main/java/com/example/FixLog/service/PostService.java @@ -8,7 +8,6 @@ import com.example.FixLog.domain.post.PostTag; import com.example.FixLog.domain.tag.Tag; import com.example.FixLog.domain.tag.TagCategory; -import com.example.FixLog.dto.post.NewPostRequestDto; import com.example.FixLog.dto.post.PostDto; import com.example.FixLog.dto.post.PostRequestDto; import com.example.FixLog.dto.post.PostResponseDto; @@ -20,9 +19,9 @@ import com.example.FixLog.repository.post.PostRepository; import com.example.FixLog.repository.tag.TagRepository; import jakarta.transaction.Transactional; -import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import java.time.LocalDate; @@ -55,13 +54,20 @@ public PostService(PostRepository postRepository, PostLikeRepository postLikeRep this.s3Service = s3Service; } - // 이미지 null일 때 default 사진으로 변경 (프로필 사진, - public String getDefaultImage(String image){ + // 이미지 null일 때 default 사진으로 변경 - 프로필 사진 + public String getDefaultProfile(String image){ String imageUrl = (image == null || image.isBlank()) ? "https://fixlog-bucket.s3.ap-northeast-2.amazonaws.com/default/profile.png" : image; System.out.println(imageUrl); return imageUrl; } + // 이미지 null일 때 default 사진으로 변경 - 썸네일 + public String getDefaultCover(String image){ + String imageUrl = (image == null || image.isBlank()) + ? "https://fixlogsmwubucket.s3.ap-northeast-2.amazonaws.com/default/DefaulThumnail.png" : image; + System.out.println(imageUrl); + return imageUrl; + } // 게시글 작성하기 @Transactional @@ -101,10 +107,10 @@ public void createPost(PostRequestDto postRequestDto){ } // 태그 다 선택 했는지 - private List fetchAndValidateTags(List tagIds){ - // 태그 ID로 Tag 엔티티 조회 - List tags = tagIds.stream() - .map(tagId -> tagRepository.findById(tagId) + private List fetchAndValidateTags(List tagNames){ + // 태그 이름으로 Tag 엔티티 조회 + List tags = tagNames.stream() + .map(tagName -> tagRepository.findByTagName(tagName) .orElseThrow(() -> new CustomException(ErrorCode.TAG_NOT_FOUND))) .toList(); @@ -134,25 +140,20 @@ else if (categories.size() > 1) } if (!issues.isEmpty()) { - throw new CustomException(ErrorCode.REQUIRED_TAGS_MISSING); - // throw new CustomException(ErrorCode.REQUIRED_TAGS_MISSING, String.join(", ", issues)); - // throw new CustomException(ErrorCode.REQUIRED_TAGS_MISSING.withDetail(missingTypes.toString())); - // Todo 어떤 태그가 선택 안된건지 보여지도록 수정 + String message = String.join(" / ", issues); + throw new CustomException(ErrorCode.REQUIRED_TAGS_MISSING, message); } return tags; } // 게시글 필수 항목 다 작성했는지 private void validatePost(PostRequestDto postRequestDto){ - if (postRequestDto.getPostTitle().isBlank() | postRequestDto.getProblem().isBlank() - | postRequestDto.getErrorMessage().isBlank() | postRequestDto.getEnvironment().isBlank() - | postRequestDto.getReproduceCode().isBlank() | postRequestDto.getSolutionCode().isBlank()) - throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING); - } - private void validatePost(NewPostRequestDto newPostRequestDto){ - if (newPostRequestDto.getPostTitle().isBlank() | newPostRequestDto.getProblem().isBlank() - | newPostRequestDto.getErrorMessage().isBlank() | newPostRequestDto.getEnvironment().isBlank() - | newPostRequestDto.getReproduceCode().isBlank() | newPostRequestDto.getSolutionCode().isBlank()) + if (!StringUtils.hasText(postRequestDto.getPostTitle()) + || !StringUtils.hasText(postRequestDto.getProblem()) + || !StringUtils.hasText(postRequestDto.getErrorMessage()) + || !StringUtils.hasText(postRequestDto.getEnvironment()) + || !StringUtils.hasText(postRequestDto.getReproduceCode()) + || !StringUtils.hasText(postRequestDto.getSolutionCode())) throw new CustomException(ErrorCode.REQUIRED_CONTENT_MISSING); } @@ -169,41 +170,49 @@ public String uploadImage(MultipartFile imageFile){ return "![image](" + imageUrl + ")"; } + // 게시글 수정하기 @Transactional - public void editPost(Long postId, NewPostRequestDto newPostRequestDto) { + public void editPost(Long postId, PostRequestDto postRequestDto) { Member member = memberService.getCurrentMemberInfo(); Post post = postRepository.findById(postId) .orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND)); + // 게시글 작성자가 본인이 맞는지 + if (!member.getUserId().equals(post.getUserId().getUserId())) { + throw new CustomException(ErrorCode.POST_UPDATE_FORBIDDEN); + } + // 북마크 카테고리별로 선택 제한 두기 - List tags = fetchAndValidateTags(newPostRequestDto.getTags()); + List tags = fetchAndValidateTags(postRequestDto.getTags()); // 아무것도 변경이 없으면 예외처리 - if (Objects.equals(post.getPostTitle(), newPostRequestDto.getPostTitle()) - & Objects.equals(post.getCoverImage(), newPostRequestDto.getCoverImageUrl()) - & Objects.equals(post.getProblem(), newPostRequestDto.getProblem()) - & Objects.equals(post.getErrorMessage(), newPostRequestDto.getErrorMessage()) - & Objects.equals(post.getEnvironment(), newPostRequestDto.getEnvironment()) - & Objects.equals(post.getReproduceCode(), newPostRequestDto.getReproduceCode()) - & Objects.equals(post.getSolutionCode(), newPostRequestDto.getSolutionCode()) - & Objects.equals(post.getCauseAnalysis(), newPostRequestDto.getCauseAnalysis()) - & Objects.equals(post.getReferenceLink(), newPostRequestDto.getReferenceLink()) - & Objects.equals(post.getExtraContent(), newPostRequestDto.getExtraContent()) - & compareTags(post.getPostTags(), tags)){ + if (Objects.equals(post.getPostTitle(), postRequestDto.getPostTitle()) + && Objects.equals(post.getCoverImage(), postRequestDto.getCoverImageUrl()) + && Objects.equals(post.getProblem(), postRequestDto.getProblem()) + && Objects.equals(post.getErrorMessage(), postRequestDto.getErrorMessage()) + && Objects.equals(post.getEnvironment(), postRequestDto.getEnvironment()) + && Objects.equals(post.getReproduceCode(), postRequestDto.getReproduceCode()) + && Objects.equals(post.getSolutionCode(), postRequestDto.getSolutionCode()) + && Objects.equals(post.getCauseAnalysis(), postRequestDto.getCauseAnalysis()) + && Objects.equals(post.getReferenceLink(), postRequestDto.getReferenceLink()) + && Objects.equals(post.getExtraContent(), postRequestDto.getExtraContent()) + && compareTags(post.getPostTags(), tags)){ throw new CustomException(ErrorCode.NO_CONTENT_CHANGED); } // 필드 업데이트 - post.changeTitle(newPostRequestDto.getPostTitle()); - post.changeCoverImage(newPostRequestDto.getCoverImageUrl()); - post.changeProblem(newPostRequestDto.getProblem()); - post.changeErrorMessage(newPostRequestDto.getErrorMessage()); - post.changeEnvironment(newPostRequestDto.getEnvironment()); - post.changeReproduceCode(newPostRequestDto.getReproduceCode()); - post.changeSolutionCode(newPostRequestDto.getSolutionCode()); - post.changeCauseAnalysis(newPostRequestDto.getCauseAnalysis()); - post.changeReferenceLink(newPostRequestDto.getReferenceLink()); - post.changeExtraContent(newPostRequestDto.getExtraContent()); + validatePost(postRequestDto); + + post.changeTitle(postRequestDto.getPostTitle()); + post.changeCoverImage(postRequestDto.getCoverImageUrl()); + post.changeProblem(postRequestDto.getProblem()); + post.changeErrorMessage(postRequestDto.getErrorMessage()); + post.changeEnvironment(postRequestDto.getEnvironment()); + post.changeReproduceCode(postRequestDto.getReproduceCode()); + post.changeSolutionCode(postRequestDto.getSolutionCode()); + post.changeCauseAnalysis(postRequestDto.getCauseAnalysis()); + post.changeReferenceLink(postRequestDto.getReferenceLink()); + post.changeExtraContent(postRequestDto.getExtraContent()); post.updateEditedAt(LocalDateTime.now()); // 태그 저장 @@ -233,7 +242,7 @@ public PostResponseDto viewPost(Long postId){ currentPost.getUserId().getUserId(), currentPost.getUserId().getNickname(), currentPost.getPostTitle(), - getDefaultImage(currentPost.getCoverImage()), + getDefaultCover(currentPost.getCoverImage()), currentPost.getProblem(), currentPost.getErrorMessage(), currentPost.getEnvironment(), @@ -253,7 +262,7 @@ public PostResponseDto viewPost(Long postId){ Member member = optionalMember.get(); nickname = member.getNickname(); String imageUrl = member.getProfileImageUrl(); - profileImageUrl = getDefaultImage(imageUrl); + profileImageUrl = getDefaultProfile(imageUrl); isLiked = currentPost.getPostLikes().stream() .anyMatch(postLike -> postLike.getUserId().equals(member)); @@ -261,7 +270,7 @@ public PostResponseDto viewPost(Long postId){ .anyMatch(bookmark -> bookmark.getFolderId().getUserId().equals(member)); } else { nickname = "로그인하지 않았습니다."; - profileImageUrl = "https://example.com/default-cover-image.png"; // 비로그인 기본 이미지 + profileImageUrl = "https://fixlog-bucket.s3.ap-northeast-2.amazonaws.com/default/profile.png"; // 비로그인 기본 이미지 isLiked = false; isMarked = false; }