This commit is contained in:
@@ -11,6 +11,7 @@ import ru.soune.nocopy.entity.user.ProtectedFileCheck;
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.exception.RetryableException;
|
||||
import ru.soune.nocopy.repository.ProtectedFileCheckRepository;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -19,22 +20,25 @@ public class CheckCounterService {
|
||||
|
||||
private final ProtectedFileCheckRepository protectedFileCheckRepository;
|
||||
|
||||
@Transactional
|
||||
public void incrementCheckCount(Long userId) {
|
||||
try {
|
||||
ProtectedFileCheck check = protectedFileCheckRepository.findByUser_Id(userId)
|
||||
.orElseThrow(NullPointerException::new);
|
||||
private final UserRepository userRepository;
|
||||
|
||||
boolean success = check.incrementCheck();
|
||||
@Transactional
|
||||
public void incrementCheckCount(Long userId, String mimeType) {
|
||||
try {
|
||||
ProtectedFileCheck check = protectedFileCheckRepository.findByUser_Id(userId).orElse(null);
|
||||
User user = userRepository.findById(userId).orElse(null);
|
||||
|
||||
if (check == null) {
|
||||
check = initProtectCheckLimit(user,0);
|
||||
}
|
||||
|
||||
boolean success = check.writeCheck(mimeType);
|
||||
|
||||
if (success) {
|
||||
protectedFileCheckRepository.save(check);
|
||||
log.info("Check count incremented for user {}: count={}, limit={}",
|
||||
userId, check.getCountChecked(), check.getLimitCheck());
|
||||
CheckIncrementResult.success(check.getCountChecked(), check.getLimitCheck());
|
||||
} else {
|
||||
log.warn("Check limit exceeded for user {}", userId);
|
||||
CheckIncrementResult.limitExceeded(check.getLimitCheck());
|
||||
log.info("Check count incremented for user {}: count={}",
|
||||
userId, check.getCountChecked());
|
||||
CheckIncrementResult.success(check.getCountChecked());
|
||||
}
|
||||
|
||||
} catch (ObjectOptimisticLockingFailureException e) {
|
||||
@@ -51,27 +55,23 @@ public class CheckCounterService {
|
||||
return CheckStatus.builder()
|
||||
.userId(userId)
|
||||
.countChecked(check.getCountChecked())
|
||||
.remainingLimit(check.getLimitCheck())
|
||||
.lastCheckAt(check.getLastCheckAt())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
public void initProtectCheckLimit(User user, Integer checked, Integer limit) {
|
||||
public ProtectedFileCheck initProtectCheckLimit(User user, Integer checked) {
|
||||
ProtectedFileCheck check = ProtectedFileCheck.builder()
|
||||
.user(user)
|
||||
.limitCheck(limit)
|
||||
.countChecked(checked)
|
||||
.build();
|
||||
|
||||
protectedFileCheckRepository.save(check);
|
||||
return protectedFileCheckRepository.save(check);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ProtectedFileCheck updateLimit(User user, Integer limit) {
|
||||
ProtectedFileCheck check = ProtectedFileCheck.builder()
|
||||
.user(user)
|
||||
.limitCheck(limit)
|
||||
.build();
|
||||
|
||||
return protectedFileCheckRepository.save(check);
|
||||
|
||||
Reference in New Issue
Block a user