Skip to content

Commit

Permalink
feat: 한 아이디로 하루 10회 초과 점역 요청 시 예외 발생 (#142)
Browse files Browse the repository at this point in the history
* feat: 한 아이디당 10회씩 점역 가능하도록 한계 설정

* fix: 세션 아이디 파라미터로 검사하는 로직 제거 (#143)

* feat: Login ID에 unique 제약조건 설정 (#145)

* fix: logout 에러 수정 (#146)

* feat: 10회 초과 시 예외 발생
  • Loading branch information
gitchannn authored Mar 13, 2024
1 parent 8cee305 commit bc19b42
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ResponseEntity<PdfRegisterResponse> registerPdf(
throw new FileException(ErrorCode.P);
}

final Long id = transcriptionService.register(file);
final Long id = transcriptionService.register(member.getId(), file);

return ResponseEntity
.created(URI.create("/transcriptions/" + id))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sunflower.server.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import sunflower.server.entity.MemberTranscriptionsLog;
import sunflower.server.repository.MemberTranscriptionsLogRepository;

import java.time.LocalDate;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class MemberTranscriptionsLogService {

private final MemberTranscriptionsLogRepository memberTranscriptionsLogRepository;

public int count(final Long memberId) {
final Optional<MemberTranscriptionsLog> log = memberTranscriptionsLogRepository.findByMemberIdAndDate(memberId, LocalDate.now());
if (log.isEmpty()) {
final MemberTranscriptionsLog newLog = memberTranscriptionsLogRepository.save(MemberTranscriptionsLog.of(memberId));
return newLog.count();
}
return log.get().count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sunflower.server.application.dto.TranscriptionStatusDto;
import sunflower.server.application.event.OcrRegisterEvent;
import sunflower.server.entity.Transcriptions;
import sunflower.server.exception.TranscriptionException;
import sunflower.server.repository.TranscriptionsRepository;
import sunflower.server.util.FileUtil;

Expand All @@ -22,10 +23,16 @@
public class TranscriptionService {

private final TranscriptionsRepository transcriptionsRepository;
private final MemberTranscriptionsLogService memberTranscriptionsLogService;
private final ApplicationEventPublisher eventPublisher;

@Transactional
public Long register(final MultipartFile file) {
public Long register(final Long memberId, final MultipartFile file) {
final int count = memberTranscriptionsLogService.count(memberId);
if (count > 10) {
throw new TranscriptionException("C");
}

final String originalFileName = file.getOriginalFilename();
final String fileName = FileUtil.createRandomFileName(file);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sunflower.server.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

import static jakarta.persistence.GenerationType.IDENTITY;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Entity
public class MemberTranscriptionsLog {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

private Long memberId;
private Integer transcriptionCount;
private LocalDate date;

public static MemberTranscriptionsLog of(final Long memberId) {
return new MemberTranscriptionsLog(null, memberId, 0, LocalDate.now());
}

public Integer count() {
this.transcriptionCount++;
return this.transcriptionCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public enum ErrorCode {
L("Latex 파일 처리에 문제가 발생했습니다."),
T("점역 API 호출에서 문제가 발생했습니다."),
B("BRF 파일 처리에 문제가 발생했습니다."),
C("일일 가능한 점역 횟수를 초과했습니다."),
;

private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sunflower.server.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import sunflower.server.entity.MemberTranscriptionsLog;

import java.time.LocalDate;
import java.util.Optional;

public interface MemberTranscriptionsLogRepository extends JpaRepository<MemberTranscriptionsLog, Long> {

Optional<MemberTranscriptionsLog> findByMemberId(final Long memberId);

Optional<MemberTranscriptionsLog> findByMemberIdAndDate(Long memberId, LocalDate today);
}

This file was deleted.

0 comments on commit bc19b42

Please sign in to comment.