80 lines
2.9 KiB
Java
80 lines
2.9 KiB
Java
package ru.soune.nocopy.service.file;
|
|||
|
|
|
||
|
|
import lombok.RequiredArgsConstructor;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.springframework.orm.ObjectOptimisticLockingFailureException;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
import org.springframework.transaction.annotation.Transactional;
|
||
|
|
import ru.soune.nocopy.dto.file.CheckIncrementResult;
|
||
|
|
import ru.soune.nocopy.dto.file.CheckStatus;
|
||
|
|
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;
|
||
|
|
|
||
|
|
@Service
|
||
|
|
@RequiredArgsConstructor
|
||
|
|
@Slf4j
|
||
|
|
public class CheckCounterService {
|
||
|
|
|
||
|
|
private final ProtectedFileCheckRepository protectedFileCheckRepository;
|
||
|
|
|
||
|
|
@Transactional
|
||
|
|
public void incrementCheckCount(Long userId) {
|
||
|
|
try {
|
||
|
|
ProtectedFileCheck check = protectedFileCheckRepository.findByUser_Id(userId)
|
||
|
|
.orElseThrow(NullPointerException::new);
|
||
|
|
|
||
|
|
boolean success = check.incrementCheck();
|
||
|
|
|
||
|
|
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());
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (ObjectOptimisticLockingFailureException e) {
|
||
|
|
log.warn("Optimistic lock failure for user {}, retrying...", userId);
|
||
|
|
throw new RetryableException("Please try again");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Transactional(readOnly = true)
|
||
|
|
public CheckStatus getCurrentStatus(Long userId) {
|
||
|
|
ProtectedFileCheck check = protectedFileCheckRepository.findByUser_Id(userId)
|
||
|
|
.orElseThrow(NullPointerException::new);
|
||
|
|
|
||
|
|
return CheckStatus.builder()
|
||
|
|
.userId(userId)
|
||
|
|
.countChecked(check.getCountChecked())
|
||
|
|
.remainingLimit(check.getLimitCheck())
|
||
|
|
.lastCheckAt(check.getLastCheckAt())
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public void initProtectCheckLimit(User user, Integer checked, Integer limit) {
|
||
|
|
ProtectedFileCheck check = ProtectedFileCheck.builder()
|
||
|
|
.user(user)
|
||
|
|
.limitCheck(limit)
|
||
|
|
.countChecked(checked)
|
||
|
|
.build();
|
||
|
|
|
||
|
|
protectedFileCheckRepository.save(check);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Transactional
|
||
|
|
public ProtectedFileCheck updateLimit(User user, Integer limit) {
|
||
|
|
ProtectedFileCheck check = ProtectedFileCheck.builder()
|
||
|
|
.user(user)
|
||
|
|
.limitCheck(limit)
|
||
|
|
.build();
|
||
|
|
|
||
|
|
return protectedFileCheckRepository.save(check);
|
||
|
|
}
|
||
|
|
}
|