@@ -0,0 +1,148 @@
|
||||
package ru.soune.nocopy.service.complaint;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.complaint.ComplaintRequest;
|
||||
import ru.soune.nocopy.dto.complaint.ComplaintResponse;
|
||||
import ru.soune.nocopy.entity.complaint.ComplaintEntity;
|
||||
import ru.soune.nocopy.entity.complaint.ComplaintStatus;
|
||||
import ru.soune.nocopy.entity.violation.Violation;
|
||||
import ru.soune.nocopy.exception.ComplaintNotFoundException;
|
||||
import ru.soune.nocopy.exception.DuplicateComplaintException;
|
||||
import ru.soune.nocopy.exception.ViolationNotFoundException;
|
||||
import ru.soune.nocopy.repository.ComplaintEntityRepository;
|
||||
import ru.soune.nocopy.repository.ViolationRepository;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional
|
||||
public class ComplaintEntityService {
|
||||
private final ComplaintEntityRepository complaintRepository;
|
||||
|
||||
private final ViolationRepository violationRepository;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
public ComplaintResponse createComplaint(ComplaintRequest request) {
|
||||
Violation violation = violationRepository.findById(request.getViolationId())
|
||||
.orElseThrow(() -> new ViolationNotFoundException("Violation not found: " + request.getViolationId()));
|
||||
|
||||
if (complaintRepository.existsByViolationId(request.getViolationId())) {
|
||||
throw new DuplicateComplaintException("Complaint already exists for violation: " + request.getViolationId());
|
||||
}
|
||||
|
||||
String linksJson = null;
|
||||
if (request.getLinks() != null && !request.getLinks().isEmpty()) {
|
||||
try {
|
||||
linksJson = objectMapper.writeValueAsString(request.getLinks());
|
||||
} catch (JsonProcessingException e) {
|
||||
linksJson = request.getLinks().toString();
|
||||
}
|
||||
}
|
||||
|
||||
ComplaintEntity complaint = ComplaintEntity.builder()
|
||||
.status(ComplaintStatus.CREATED)
|
||||
.complaintText(request.getComplaintText())
|
||||
.email(request.getEmail())
|
||||
.links(linksJson)
|
||||
.violation(violation)
|
||||
.build();
|
||||
|
||||
return mapToResponse(complaintRepository.save(complaint));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ComplaintResponse getComplaintById(Long id) {
|
||||
return complaintRepository.findById(id)
|
||||
.map(this::mapToResponse)
|
||||
.orElseThrow(() -> new ComplaintNotFoundException("Complaint not found: " + id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Page<ComplaintResponse> getAllComplaints(Pageable pageable) {
|
||||
return complaintRepository.findAll(pageable).map(this::mapToResponse);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ComplaintResponse> getComplaintsByViolationId(Long violationId) {
|
||||
if (!violationRepository.existsById(violationId)) {
|
||||
throw new ViolationNotFoundException("Violation not found: " + violationId);
|
||||
}
|
||||
return complaintRepository.findByViolationId(violationId)
|
||||
.stream()
|
||||
.map(this::mapToResponse)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public ComplaintResponse updateComplaintStatus(Long id, ComplaintStatus status) {
|
||||
ComplaintEntity complaint = complaintRepository.findById(id)
|
||||
.orElseThrow(() -> new ComplaintNotFoundException("Complaint not found: " + id));
|
||||
complaint.setStatus(status);
|
||||
return mapToResponse(complaintRepository.save(complaint));
|
||||
}
|
||||
|
||||
public ComplaintResponse updateComplaint(Long id, ComplaintRequest request) {
|
||||
ComplaintEntity complaint = complaintRepository.findById(id)
|
||||
.orElseThrow(() -> new ComplaintNotFoundException("Complaint not found: " + id));
|
||||
|
||||
if (request.getViolationId() != null) {
|
||||
Violation violation = violationRepository.findById(request.getViolationId())
|
||||
.orElseThrow(() -> new ViolationNotFoundException("Violation not found: " + request.getViolationId()));
|
||||
complaint.setViolation(violation);
|
||||
}
|
||||
if (request.getComplaintText() != null) complaint.setComplaintText(request.getComplaintText());
|
||||
if (request.getEmail() != null) complaint.setEmail(request.getEmail());
|
||||
if (request.getLinks() != null) {
|
||||
try {
|
||||
complaint.setLinks(objectMapper.writeValueAsString(request.getLinks()));
|
||||
} catch (JsonProcessingException e) {
|
||||
complaint.setLinks(request.getLinks().toString());
|
||||
}
|
||||
}
|
||||
|
||||
return mapToResponse(complaintRepository.save(complaint));
|
||||
}
|
||||
|
||||
public ComplaintResponse deleteComplaint(Long id) {
|
||||
ComplaintEntity complaint = complaintRepository.findById(id)
|
||||
.orElseThrow(() -> new ComplaintNotFoundException("Complaint not found: " + id));
|
||||
complaint.setStatus(ComplaintStatus.CANCELLED);
|
||||
return mapToResponse(complaintRepository.save(complaint));
|
||||
}
|
||||
|
||||
private ComplaintResponse mapToResponse(ComplaintEntity entity) {
|
||||
List<String> linksList = null;
|
||||
if (entity.getLinks() != null) {
|
||||
try {
|
||||
linksList = objectMapper.readValue(entity.getLinks(),
|
||||
objectMapper.getTypeFactory().constructCollectionType(List.class, String.class));
|
||||
} catch (JsonProcessingException e) {
|
||||
log.warn("Failed to parse links JSON: {}", entity.getLinks());
|
||||
}
|
||||
}
|
||||
|
||||
return ComplaintResponse.builder()
|
||||
.id(entity.getId())
|
||||
.status(entity.getStatus() != null ? entity.getStatus().name() : null)
|
||||
.complaintText(entity.getComplaintText())
|
||||
.email(entity.getEmail())
|
||||
.links(linksList)
|
||||
.violationId(entity.getViolation() != null ? entity.getViolation().getId() : null)
|
||||
.createdAt(entity.getCreatedAt() != null ? entity.getCreatedAt().format(DATE_FORMATTER) : null)
|
||||
.updatedAt(entity.getUpdatedAt() != null ? entity.getUpdatedAt().format(DATE_FORMATTER) : null)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user