Files
no-copy/src/main/java/ru/soune/nocopy/service/violation/ViolationNotionService.java
T

139 lines
5.3 KiB
Java
Raw Normal View History

2026-03-17 16:35:27 +07:00
package ru.soune.nocopy.service.violation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.soune.nocopy.dto.violation.ViolationNotionResponse;
import ru.soune.nocopy.dto.violation.ViolationNotionsListResponse;
import ru.soune.nocopy.entity.company.Company;
import ru.soune.nocopy.entity.file.FileEntity;
import ru.soune.nocopy.entity.user.User;
import ru.soune.nocopy.entity.violation.ViolationNotion;
import ru.soune.nocopy.repository.FileEntityRepository;
import ru.soune.nocopy.repository.UserRepository;
import ru.soune.nocopy.repository.ViolationNotionRepository;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
@RequiredArgsConstructor
public class ViolationNotionService {
private final ViolationNotionRepository notionRepository;
private final UserRepository userRepository;
private final FileEntityRepository fileRepository;
@Transactional(readOnly = true)
public ViolationNotionsListResponse getNotionsByFileId(String fileId, Long userId) {
FileEntity file = fileRepository.findById(fileId)
.orElseThrow(() -> new IllegalArgumentException("File not found: " + fileId));
checkPermission(userId, file);
List<ViolationNotion> notions = notionRepository.findByFileId(fileId);
long totalCount = notionRepository.countByFileId(fileId);
List<ViolationNotionResponse> notionResponses = notions.stream()
.map(this::mapToResponse)
.collect(Collectors.toList());
return ViolationNotionsListResponse.builder()
.fileId(fileId)
.fileName(file.getOriginalFileName())
.totalCount((int) totalCount)
.notions(notionResponses)
.build();
}
@Transactional(readOnly = true)
public ViolationNotionResponse getNotionById(Long notionId, Long userId) {
ViolationNotion notion = notionRepository.findById(notionId)
.orElseThrow(() -> new IllegalArgumentException("Notion not found: " + notionId));
if (!notion.getFile().getUserId().equals(userId) && !notion.getUser().getId().equals(userId)) {
throw new SecurityException("User does not have access to this notion");
}
return mapToResponse(notion);
}
@Transactional
public ViolationNotionResponse createNotion(String fileId, Long userId, String message) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("User not found: " + userId));
FileEntity file = fileRepository.findById(fileId)
.orElseThrow(() -> new IllegalArgumentException("File not found: " + fileId));
ViolationNotion notion = new ViolationNotion();
notion.setUser(user);
notion.setFile(file);
notion.setMessage(message);
ViolationNotion saved = notionRepository.save(notion);
return mapToResponse(saved);
}
@Transactional
public void deleteNotion(Long notionId, Long userId) {
ViolationNotion notion = notionRepository.findById(notionId)
.orElseThrow(() -> new IllegalArgumentException("Notion not found: " + notionId));
checkPermission(userId, notion.getFile());
notionRepository.delete(notion);
}
@Transactional
public ViolationNotionResponse updateNotion(Long notionId, Long userId, String message) {
ViolationNotion notion = notionRepository.findById(notionId)
.orElseThrow(() -> new IllegalArgumentException("Notion not found: " + notionId));
if (!notion.getUser().getId().equals(userId)) {
throw new SecurityException("Only author can update this notion");
}
notion.setMessage(message);
ViolationNotion updated = notionRepository.save(notion);
log.info("Updated notion with id: {} by user: {}", notionId, userId);
return mapToResponse(updated);
}
private ViolationNotionResponse mapToResponse(ViolationNotion notion) {
return ViolationNotionResponse.builder()
.id(notion.getId())
.message(notion.getMessage())
.createdAt(notion.getCreatedAt())
.user(ViolationNotionResponse.UserInfo.builder()
.id(notion.getUser().getId())
.fullName(notion.getUser().getFullName())
.email(notion.getUser().getEmail())
.build())
.build();
}
private void checkPermission(long userId, FileEntity file) {
User user = userRepository.findById(userId).orElseThrow(() ->
new IllegalArgumentException("User not found: " + userId));
Company company = user.getCompany();
if (company != null) {
List<Long> companyUsers = company.getUsers()
.stream()
.map(User::getId)
.toList();
if (companyUsers.contains(userId)) {
throw new SecurityException("User does not have access to this file");
}
} else if (!file.getUserId().equals(userId)) {
throw new SecurityException("User does not have access to this file");
}
}
}