-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Profanity 도메인 분리 및 금칙어 추가(파일, 단일), 삭제, csv 다운 �api 추가 (#329)
- Loading branch information
1 parent
8cb6e2f
commit ff5ba8c
Showing
26 changed files
with
670 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
:sectnums: | ||
== 금칙어 관리 | ||
|
||
=== 금칙어를 파일로 등록 | ||
:sectnums!: | ||
|
||
==== Request | ||
include::{snippets}/profanity/upload-banword-file/http-request.adoc[] | ||
|
||
===== Request Part | ||
include::{snippets}/profanity/upload-banword-file/request-parts.adoc[] | ||
|
||
==== Response | ||
include::{snippets}/profanity/upload-banword-file/http-response.adoc[] | ||
|
||
:sectnums: | ||
|
||
=== 금칙어 정보 csv 파일로 저장 | ||
:sectnums!: | ||
|
||
==== Request | ||
include::{snippets}/profanity/download-csv-file/http-request.adoc[] | ||
|
||
==== Response | ||
include::{snippets}/profanity/download-csv-file/http-response.adoc[] | ||
|
||
===== Response Body | ||
include::{snippets}/profanity/download-csv-file/response-fields.adoc[] | ||
|
||
:sectnums: | ||
|
||
=== 금칙어 정보 단일 추가 | ||
:sectnums!: | ||
|
||
==== Request | ||
include::{snippets}/profanity/add-single-banword/http-request.adoc[] | ||
|
||
===== Request Body | ||
include::{snippets}/profanity/add-single-banword/request-fields.adoc[] | ||
|
||
==== Response | ||
include::{snippets}/profanity/add-single-banword/http-response.adoc[] | ||
|
||
:sectnums: | ||
|
||
=== 금칙어 정보 단일 삭제 | ||
:sectnums!: | ||
|
||
==== Request | ||
include::{snippets}/profanity/delete-banword/http-request.adoc[] | ||
|
||
===== Request Body | ||
include::{snippets}/profanity/delete-banword/request-fields.adoc[] | ||
|
||
==== Response | ||
include::{snippets}/profanity/delete-banword/http-response.adoc[] | ||
|
||
:sectnums: |
21 changes: 20 additions & 1 deletion
21
src/main/java/com/spaceclub/global/annotation/profanity/ProfanityCheckValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...main/java/com/spaceclub/global/annotation/profanity/ProfanityCheckValidatorUsingList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.spaceclub.global.annotation.profanity; | ||
|
||
import com.spaceclub.global.annotation.profanity.domain.Profanity; | ||
import com.spaceclub.global.annotation.profanity.domain.repository.ProfanityRepository; | ||
import com.spaceclub.global.timer.StopWatch; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class ProfanityCheckValidatorUsingList implements ConstraintValidator<ProfanityCheck, String> { | ||
|
||
private final ProfanityRepository profanityRepository; | ||
|
||
@StopWatch | ||
@Override | ||
@Transactional | ||
public boolean isValid(String text, ConstraintValidatorContext constraintValidatorContext) { | ||
List<Profanity> banWords = profanityRepository.findAll(); | ||
List<String> detectedBanWords = new ArrayList<>(); | ||
for (Profanity banWord : banWords) { | ||
if (text.contains(banWord.getBanWord())) { | ||
detectedBanWords.add(banWord.getBanWord()); | ||
} | ||
} | ||
|
||
detectedBanWords.forEach(banWord -> { | ||
Profanity profanity = profanityRepository.findByBanWord(banWord); | ||
profanity.increaseUseCount(); | ||
}); | ||
|
||
log.debug("Detected profanities: {}", detectedBanWords); | ||
return detectedBanWords.isEmpty(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/com/spaceclub/global/annotation/profanity/controller/ProfanityController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.spaceclub.global.annotation.profanity.controller; | ||
|
||
import com.spaceclub.global.annotation.profanity.controller.request.BanWordRequest; | ||
import com.spaceclub.global.annotation.profanity.controller.response.UrlResponse; | ||
import com.spaceclub.global.annotation.profanity.service.ProfanityService; | ||
import com.spaceclub.global.config.ProfanityConfig; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestPart; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import static com.spaceclub.global.annotation.profanity.ProfanityExceptionMessage.INVALID_EXTENSION; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping("/api/v1/profanities") | ||
@RequiredArgsConstructor | ||
public class ProfanityController { | ||
|
||
private final ProfanityService profanityService; | ||
private final ProfanityConfig profanityConfig; | ||
|
||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) | ||
public ResponseEntity<String> uploadFile(@RequestPart MultipartFile file) { | ||
log.info("금칙어 목록 파일을 업로드 합니다 : {}", file.getOriginalFilename()); | ||
if (!isValidExtension(file)) { | ||
throw new IllegalArgumentException(INVALID_EXTENSION.getMessage()); | ||
} | ||
profanityService.saveProfanitiesFromFile(file); | ||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
} | ||
|
||
@GetMapping("/csv") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public UrlResponse createCsvFile() { | ||
String filePath = profanityService.createCsvFile(); | ||
return new UrlResponse(filePath); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<String> addProfanity(@RequestBody BanWordRequest request) { | ||
profanityService.saveProfanity(request.word()); | ||
return ResponseEntity.status(HttpStatus.CREATED).build(); | ||
} | ||
|
||
@DeleteMapping | ||
public ResponseEntity<String> deleteProfanity(@RequestBody BanWordRequest request) { | ||
profanityService.deleteProfanity(request.word()); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
private boolean isValidExtension(MultipartFile file) { | ||
String fileName = file.getOriginalFilename(); | ||
if (fileName == null || !fileName.contains(".")) { | ||
return false; // 파일 이름이 null이거나 확장자가 없는 경우 | ||
} | ||
|
||
String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); | ||
return profanityConfig.validExtensions().stream() // 대소문자 구분 없이 확장자 비교 | ||
.anyMatch(validExtension -> validExtension.equalsIgnoreCase(extension)); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ain/java/com/spaceclub/global/annotation/profanity/controller/request/BanWordRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.spaceclub.global.annotation.profanity.controller.request; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record BanWordRequest( | ||
@NotNull String word | ||
) { | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/spaceclub/global/annotation/profanity/controller/response/UrlResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.spaceclub.global.annotation.profanity.controller.response; | ||
|
||
public record UrlResponse(String url) { | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/spaceclub/global/annotation/profanity/domain/Profanity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.spaceclub.global.annotation.profanity.domain; | ||
|
||
import com.spaceclub.global.BaseTimeEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.Getter; | ||
|
||
@Entity | ||
@Getter | ||
public class Profanity extends BaseTimeEntity { | ||
|
||
protected Profanity() {} | ||
|
||
public Profanity(String banWord) { | ||
this.banWord = banWord; | ||
this.useCount = 0L; | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false, unique = true) | ||
private String banWord; | ||
|
||
@Column(nullable = false) | ||
private long useCount; | ||
|
||
public void increaseUseCount() { | ||
this.useCount++; | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...om/spaceclub/global/annotation/profanity/domain/repository/ProfanityCustomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.spaceclub.global.annotation.profanity.domain.repository; | ||
|
||
import com.spaceclub.global.annotation.profanity.domain.Profanity; | ||
|
||
import java.util.List; | ||
|
||
public interface ProfanityCustomRepository { | ||
|
||
void bulkInsert(List<Profanity> profanities); | ||
|
||
} |
Oops, something went wrong.