Commit 762803b1 authored by George Novikov's avatar George Novikov

рефакторинг сохранения документа и проверки шаблона по templateId

parent 8dad440b
Pipeline #396 failed with stage
......@@ -14,18 +14,22 @@ import kz.project.printedFormsService.service.DocumentsService;
import kz.project.printedFormsService.service.TemplateService;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@Service
@RequiredArgsConstructor
public class DocumentsServiceImpl implements DocumentsService {
private static final Logger LOGGER = LoggerFactory.getLogger(DocumentsServiceImpl.class);
private static final Set<Long> INEXISTENT_TEMPLATES = new HashSet<>();
private final DocumentJdbcRepository documentJdbcRepository;
private final TemplateService templateService;
private final DocumentRepository documentRepository;
......@@ -74,13 +78,24 @@ public class DocumentsServiceImpl implements DocumentsService {
@Transactional
@Override
public void saveDocument(Long templateId) throws ValidationException, JsonProcessingException {
DocumentEntity documentEntity = new DocumentEntity();
TemplateEntity templateEntity = templateService.getTemplateEntity(templateId);
if (templateEntity == null){
printOrBypass(templateId);
return;
}
DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setTemplate(templateEntity);
documentEntity.setProject(templateEntity.getProject());
documentEntity.setCreatedAt(LocalDateTime.now());
documentRepository.save(documentEntity);
documentRepository.save(documentEntity);
}
private void printOrBypass(Long templateId){
if (!INEXISTENT_TEMPLATES.contains(templateId)){
LOGGER.warn("Не найден шаблон с таким идентификатором {}", templateId);
}
INEXISTENT_TEMPLATES.add(templateId);
}
}
......@@ -175,7 +175,7 @@ public class TemplateServiceImpl implements TemplateService {
@Override
public TemplateEntity getTemplateEntity(Long id) throws ValidationException {
return repository.findById(id).orElseThrow(() -> new ValidationException("Не найден шаблон с таким идентификатором", 13));
return repository.findById(id).orElse(null);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment