Skip to content

Commit

Permalink
Merge pull request #294 from HyunJunSon/feature/badwordfiltering
Browse files Browse the repository at this point in the history
Feature:: 비속어 필터링 기능 추가
  • Loading branch information
HyunJunSon committed Mar 14, 2024
2 parents d3c12e9 + 8253374 commit 8cabb90
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public Page<AdminReviewDto> getReviews(String sortBy, Pageable pageable) {
@Transactional(readOnly = true)
public AdminReviewDto getReview(Long reviewNo) {
log.info("리뷰 단건 조회 -> review_no {}", reviewNo);
// FIXME :: Admin member 작업 이후 Admin Review 리팩터링 필요 -> 임시로 CRUDRepository 의 GetReferenceById 사용
return AdminReviewDto.from(reviewRepository.getReferenceById(reviewNo));

return AdminReviewDto.from(null);
//FIXME:: 찬규님 고쳐주세요 (null로 바꿔놨어요 reviewRepository.findByReviewNo(reviewNo) -> null)
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions module-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ dependencies {
annotationProcessor 'jakarta.annotation:jakarta.annotation-api'
annotationProcessor 'jakarta.persistence:jakarta.persistence-api'

//badword Filtering
implementation 'io.github.vaneproject:badwordfiltering:1.0.0'


// https://mvnrepository.com/artifact/org.apache.commons/commons-collections4
implementation group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.kernel360.global.annotation;

import com.kernel360.global.badwordfilter.BadWordValidator;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = BadWordValidator.class)
public @interface BadWordFilter {
String message() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
boolean ignoreCase() default false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.kernel360.global.badwordfilter;

import com.kernel360.global.annotation.BadWordFilter;
import com.vane.badwordfiltering.BadWordFiltering;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.List;

public class BadWordValidator implements ConstraintValidator<BadWordFilter, Object> {
private BadWordFiltering badWordFiltering;
@Override
public void initialize(BadWordFilter constraintAnnotation) {

badWordFiltering = new BadWordFiltering();
}

@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value instanceof String string) {
return !badWordFiltering.check(string);
} else if (value instanceof List<?> list) {
return handleStringList(list);
}
return true;
}

private boolean handleStringList(List<?> list) {
return list.stream()
.map(String.class::cast)
.noneMatch(badWordFiltering::check);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.kernel360.member.repository;


public interface MemberRepository extends MemberRepositoryJpa {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.kernel360.review.dto.ReviewResponseDto;
import com.kernel360.review.dto.ReviewRequestDto;
import com.kernel360.review.service.ReviewService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -47,7 +48,7 @@ public ResponseEntity<ApiResponse<ReviewResponseDto>> getReview(@PathVariable Lo

@PostMapping("")
public <T> ResponseEntity<ApiResponse<T>> createReview(
@RequestPart ReviewRequestDto review,
@Valid @RequestPart ReviewRequestDto review,
@RequestPart(required = false) List<MultipartFile> files) {
reviewService.createReview(review, files);

Expand All @@ -56,7 +57,7 @@ public <T> ResponseEntity<ApiResponse<T>> createReview(

@PatchMapping("")
public <T> ResponseEntity<ApiResponse<T>> updateReview(
@RequestPart ReviewRequestDto review,
@Valid @RequestPart ReviewRequestDto review,
@RequestPart(required = false) List<MultipartFile> files) {
reviewService.updateReview(review, files);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kernel360.review.dto;

import com.kernel360.global.annotation.BadWordFilter;
import com.kernel360.member.entity.Member;
import com.kernel360.product.entity.Product;
import com.kernel360.review.entity.Review;
Expand All @@ -15,7 +16,9 @@ public record ReviewRequestDto(Long reviewNo,
Long productNo,
Long memberNo,
BigDecimal starRating,
@BadWordFilter
String title,
@BadWordFilter
String contents,
LocalDateTime createdAt,
String createdBy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum CommonErrorCode implements ErrorCode {
INVALID_REQUEST_HEADERS(HttpStatus.BAD_REQUEST.value(), "E005", "요청한 헤더가 존재하지 않음"),
INVALID_ARGUMENT(HttpStatus.BAD_REQUEST.value(), "E006", "요청 파라미터가 없거나 비어있거나, 요청 파라미터의 이름이 메서드 인수의 이름과 일치하지 않습니다"),
INVALID_HTTP_REQUEST_METHOD(HttpStatus.BAD_REQUEST.value(), "E007", "요청 URL 에서 지원하지 않는 HTTP Method 입니다."),
INVALID_REQUEST_PARAMETER(HttpStatus.BAD_REQUEST.value(), "E008", "요청한 파라미터가 존재하지 않음");
INVALID_REQUEST_PARAMETER(HttpStatus.BAD_REQUEST.value(), "E008", "요청한 파라미터가 존재하지 않음"),
INVALID_WORD_PARAMETER(HttpStatus.BAD_REQUEST.value(), "E009", "비속어를 포함할 수 없습니다.");

private final int status;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;

@Slf4j
@RestControllerAdvice
Expand Down Expand Up @@ -100,4 +102,12 @@ protected ResponseEntity<ErrorResponse> handleMissingParameterException(final Mi

return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public final ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException e, WebRequest request) {
log.error("handleMethodArgumentNotValid", e);
ErrorResponse response = ErrorResponse.of(CommonErrorCode.INVALID_WORD_PARAMETER);

return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kernel360.member.repository;

import com.kernel360.member.entity.Admin;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AdminRepository extends JpaRepository<Admin,Long> {
}

0 comments on commit 8cabb90

Please sign in to comment.