Skip to content

Commit

Permalink
Merge branch 'feature/#97-community-post-search/GAJI-124' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
strongmhk committed Aug 17, 2024
2 parents 21ad8c2 + cfdfb7a commit 9ff8ab9
Show file tree
Hide file tree
Showing 59 changed files with 778 additions and 605 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package gaji.service.domain.common.annotation;

import gaji.service.domain.common.validation.HashtagListElementValidator;
import gaji.service.domain.common.validation.HashtagBlankValidator;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = HashtagListElementValidator.class)
@Constraint(validatedBy = HashtagBlankValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckHashtagListElement {
public @interface CheckHashtagBlank {
String message() default "올바른 해시태그 값을 입력해주세요.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gaji.service.domain.common.annotation;

import gaji.service.domain.common.validation.HashtagLengthValidator;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.*;

@Documented
@Constraint(validatedBy = HashtagLengthValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckHashtagLength {
String message() default "올바른 해시태그 값을 입력해주세요.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ public static Category toCategory(CategoryEnum category) {
.build();
}

public static List<SelectCategory> toSelectCategoryList(List<Category> categoryList, Long entityId, PostTypeEnum type) {
return categoryList.stream()
.map(category ->
SelectCategory.builder()
.category(category)
.entityId(entityId)
.type(type)
.build()
).collect(Collectors.toList());
public static SelectCategory toSelectCategory(Category category, Long entityId, PostTypeEnum type) {
return SelectCategory.builder()
.category(category)
.entityId(entityId)
.type(type)
.build();
}

public static CategoryResponseDTO.BaseDTO toBaseDTO(Category category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Hashtag extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, length = 30)
@Column(nullable = false, length = 15)
private String name;

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

public interface CategoryService {
Category saveCategory(Category category);
SelectCategory saveSelectCategory(SelectCategory selectCategory);
Category findByCategory(CategoryEnum category);
Category findByCategoryId(Long categoryId);
List<Long> findEntityIdListByCategoryIdAndPostType(Long categoryId, PostTypeEnum postType);
boolean existsByCategory(CategoryEnum category);
boolean existsByCategoryId(Long categoryId);
List<SelectCategory> findAllFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType);
List<Category> findCategoryEntityList(List<Long> categoryIdList);
List<CategoryResponseDTO.BaseDTO> findAllCategory();
void saveAllSelectCategory(List<SelectCategory> selectCategoryList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public Category saveCategory(Category category) {
return categoryRepository.save(category);
}

@Override
public SelectCategory saveSelectCategory(SelectCategory selectCategory) {
return selectCategoryRepository.save(selectCategory);
}

@Override
public Category findByCategory(CategoryEnum category) {
return categoryRepository.findByCategory(category);
Expand Down Expand Up @@ -61,20 +66,6 @@ public List<SelectCategory> findAllFetchJoinWithCategoryByEntityIdAndPostType(Lo
return selectCategoryRepository.findAllFetchJoinWithCategoryByEntityIdAndPostType(entityId, postType);
}

// 카테고리가 존재하면 category에 찾아서 저장, 존재하지 않으면 예외 발생
@Override
public List<Category> findCategoryEntityList(List<Long> categoryIdList) {
return categoryIdList.stream()
.map(categoryId -> {
if (existsByCategoryId(categoryId)) {
return findByCategoryId(categoryId);
} else {
throw new RestApiException(GlobalErrorStatus._INVALID_CATEGORY);
}
})
.collect(Collectors.toList());
}

@Override
@Transactional
public List<CategoryResponseDTO.BaseDTO> findAllCategory() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gaji.service.domain.common.validation;

import gaji.service.domain.common.annotation.CheckHashtagListElement;
import gaji.service.domain.common.annotation.CheckHashtagBlank;
import gaji.service.global.exception.code.status.GlobalErrorStatus;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
Expand All @@ -9,10 +9,10 @@
import java.util.List;

@Component
public class HashtagListElementValidator implements ConstraintValidator<CheckHashtagListElement, List<String>> {
public class HashtagBlankValidator implements ConstraintValidator<CheckHashtagBlank, List<String>> {

@Override
public void initialize(CheckHashtagListElement constraintAnnotation) {
public void initialize(CheckHashtagBlank constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}

Expand All @@ -23,7 +23,8 @@ public boolean isValid(List<String> values, ConstraintValidatorContext context)
return true;
}

// 리스트 안의 element중에서 하나라도 ["", " ", null]값에 해당되면 false, 아니면 true
// 유효성 검사 통과 조건
// 리스트 안의 모든 element가 ["", " ", null]값에 해당되지 않아야함
boolean isValid = values.stream()
.allMatch(value -> value != null && !value.trim().isEmpty());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gaji.service.domain.common.validation;

import gaji.service.domain.common.annotation.CheckHashtagBlank;
import gaji.service.domain.common.annotation.CheckHashtagLength;
import gaji.service.global.exception.code.status.GlobalErrorStatus;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class HashtagLengthValidator implements ConstraintValidator<CheckHashtagLength, List<String>> {

@Override
public void initialize(CheckHashtagLength constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
}

@Override
public boolean isValid(List<String> values, ConstraintValidatorContext context) {
// 리스트 자체가 null인 경우 유효성 검사에서 제외
if (values == null) {
return true;
}

// 유효성 검사 통과 조건
// 리스트 안의 모든 element의 글자 수가 15자 이하여야함
boolean isValid = values.stream()
.allMatch(value -> value.length() <= 15);

if (!isValid) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(GlobalErrorStatus._INVALID_HASHTAG_LENGTH.getMessage()).addConstraintViolation();
}

return isValid;
}
}
4 changes: 2 additions & 2 deletions src/main/java/gaji/service/domain/enums/PostStatusEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import gaji.service.domain.post.code.PostErrorStatus;
import gaji.service.domain.post.code.CommunityPostErrorStatus;
import gaji.service.global.exception.RestApiException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -33,6 +33,6 @@ public static PostStatusEnum from(String param) {
}
}
log.error("PostStatusEnum.from() exception occur param: {}", param);
throw new RestApiException(PostErrorStatus._INVALID_POST_STATUS);
throw new RestApiException(CommunityPostErrorStatus._INVALID_POST_STATUS);
}
}
4 changes: 2 additions & 2 deletions src/main/java/gaji/service/domain/enums/PostTypeEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import gaji.service.domain.post.code.PostErrorStatus;
import gaji.service.domain.post.code.CommunityPostErrorStatus;
import gaji.service.global.exception.RestApiException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,7 +28,7 @@ public static PostTypeEnum from(String param) {
}
}
log.error("PostTypeEnum.from() exception occur param: {}", param);
throw new RestApiException(PostErrorStatus._INVALID_POST_TYPE);
throw new RestApiException(CommunityPostErrorStatus._INVALID_POST_TYPE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gaji.service.domain.post.code;

import gaji.service.global.exception.code.BaseCodeDto;
import gaji.service.global.exception.code.BaseErrorCodeInterface;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;


@Getter
@AllArgsConstructor
public enum CommunityCommentErrorStatus implements BaseErrorCodeInterface {

_COMMENT_NOT_FOUND(HttpStatus.BAD_REQUEST, "COMMENT_4001", "존재하지 않는 댓글입니다."),
_NOT_AUTHORIZED(HttpStatus.FORBIDDEN, "COMMENT_4031", "해당 댓글에 접근 권한이 없습니다.")
;

private final HttpStatus httpStatus;
private final boolean isSuccess = false;
private final String code;
private final String message;

@Override
public BaseCodeDto getErrorCode() {
return BaseCodeDto.builder()
.httpStatus(httpStatus)
.isSuccess(isSuccess)
.code(code)
.message(message)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

@Getter
@AllArgsConstructor
public enum PostErrorStatus implements BaseErrorCodeInterface {
public enum CommunityPostErrorStatus implements BaseErrorCodeInterface {

_POST_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST_4001", "존재하지 않는 게시글입니다."),
_INVALID_POST_TYPE(HttpStatus.BAD_REQUEST, "POST_4002", "유효하지 않은 게시글 유형입니다."),
_INVALID_POST_STATUS(HttpStatus.BAD_REQUEST, "POST_4003", "유효하지 않은 게시글 상태값입니다."),
_ALREADY_EXIST_POST_LIKES(HttpStatus.BAD_REQUEST, "POST_4004", "이미 좋아요한 게시글입니다."),
_ALREADY_EXIST_POST_BOOKMARK(HttpStatus.BAD_REQUEST, "POST_4005", "이미 북마크한 게시글입니다."),


_COMMENT_NOT_FOUND(HttpStatus.BAD_REQUEST, "POST_4004", "존재하지 않는 댓글입니다."),

_NOT_AUTHORIZED(HttpStatus.FORBIDDEN, "POST_4031", "해당 게시글에 접근 권한이 없습니다.")
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package gaji.service.domain.post.converter;

import gaji.service.domain.post.entity.Comment;
import gaji.service.domain.post.entity.CommunityComment;
import gaji.service.domain.post.web.dto.CommunityPostCommentResponseDTO;
import gaji.service.global.converter.DateConverter;

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

public class CommentConverter {
public class CommunityCommentConverter {

public static CommunityPostCommentResponseDTO.WriteCommentDTO toWriteCommentDTO(Comment comment) {
public static CommunityPostCommentResponseDTO.WriteCommentDTO toWriteCommentDTO(CommunityComment comment) {
return CommunityPostCommentResponseDTO.WriteCommentDTO.builder()
.commentId(comment.getId())
.build();
}

public static CommunityPostCommentResponseDTO.PostCommentDTO toPostCommentDTO(Comment comment) {
public static CommunityPostCommentResponseDTO.PostCommentDTO toPostCommentDTO(CommunityComment comment) {
return CommunityPostCommentResponseDTO.PostCommentDTO.builder()
.commentId(comment.getId())
.userId(comment.getUser().getId())
Expand All @@ -28,9 +28,9 @@ public static CommunityPostCommentResponseDTO.PostCommentDTO toPostCommentDTO(Co
.build();
}

public static CommunityPostCommentResponseDTO.PostCommentListDTO toPostCommentListDTO(List<Comment> commentList, boolean hasNext) {
public static CommunityPostCommentResponseDTO.PostCommentListDTO toPostCommentListDTO(List<CommunityComment> commentList, boolean hasNext) {
List<CommunityPostCommentResponseDTO.PostCommentDTO> postCommentDTOList = commentList.stream()
.map(CommentConverter::toPostCommentDTO)
.map(CommunityCommentConverter::toPostCommentDTO)
.collect(Collectors.toList());

return CommunityPostCommentResponseDTO.PostCommentListDTO.builder()
Expand Down
Loading

0 comments on commit 9ff8ab9

Please sign in to comment.