@@ -6,13 +6,12 @@ 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.Violation;
|
||||
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 ru.soune.nocopy.repository.ViolationRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -23,27 +22,25 @@ import java.util.stream.Collectors;
|
||||
public class ViolationNotionService {
|
||||
|
||||
private final ViolationNotionRepository notionRepository;
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
private final FileEntityRepository fileRepository;
|
||||
private final ViolationRepository violationRepository;
|
||||
|
||||
@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);
|
||||
public ViolationNotionsListResponse getNotionsByViolationId(Long violationId, Long userId) {
|
||||
Violation violation = violationRepository.findById(violationId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Violation not found: " + violationId));
|
||||
|
||||
List<ViolationNotion> notions = notionRepository.findByFileId(fileId);
|
||||
long totalCount = notionRepository.countByFileId(fileId);
|
||||
List<ViolationNotion> notions = notionRepository.findByViolationId(violationId);
|
||||
long totalCount = notionRepository.countByViolationId(violationId);
|
||||
|
||||
List<ViolationNotionResponse> notionResponses = notions.stream()
|
||||
.map(this::mapToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return ViolationNotionsListResponse.builder()
|
||||
.fileId(fileId)
|
||||
.fileName(file.getOriginalFileName())
|
||||
.violationId(violationId)
|
||||
.url(violation.getUrl())
|
||||
.host(violation.getHost())
|
||||
.totalCount((int) totalCount)
|
||||
.notions(notionResponses)
|
||||
.build();
|
||||
@@ -54,7 +51,7 @@ public class ViolationNotionService {
|
||||
ViolationNotion notion = notionRepository.findById(notionId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Notion not found: " + notionId));
|
||||
|
||||
if (!notion.getFile().getUserId().equals(userId) && !notion.getUser().getId().equals(userId)) {
|
||||
if (!notion.getUser().getId().equals(userId)) {
|
||||
throw new SecurityException("User does not have access to this notion");
|
||||
}
|
||||
|
||||
@@ -62,33 +59,24 @@ public class ViolationNotionService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ViolationNotionResponse createNotion(String fileId, Long userId, String message) {
|
||||
public ViolationNotionResponse createNotion(Long violationId, 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));
|
||||
Violation violation = violationRepository.findById(violationId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Violation not found: " + violationId));
|
||||
|
||||
ViolationNotion notion = new ViolationNotion();
|
||||
notion.setUser(user);
|
||||
notion.setFile(file);
|
||||
notion.setViolation(violation);
|
||||
notion.setMessage(message);
|
||||
|
||||
ViolationNotion saved = notionRepository.save(notion);
|
||||
log.info("Created notion for violation: {} by user: {}", violationId, userId);
|
||||
|
||||
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)
|
||||
@@ -100,39 +88,40 @@ public class ViolationNotionService {
|
||||
|
||||
notion.setMessage(message);
|
||||
ViolationNotion updated = notionRepository.save(notion);
|
||||
log.info("Updated notion with id: {} by user: {}", notionId, userId);
|
||||
log.info("Updated notion: {} by user: {}", notionId, userId);
|
||||
|
||||
return mapToResponse(updated);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteNotion(Long notionId, Long userId) {
|
||||
ViolationNotion notion = notionRepository.findById(notionId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Notion not found: " + notionId));
|
||||
|
||||
if (!notion.getUser().getId().equals(userId)) {
|
||||
throw new SecurityException("Only author can delete this notion");
|
||||
}
|
||||
|
||||
notionRepository.delete(notion);
|
||||
log.info("Deleted notion: {} by user: {}", notionId, userId);
|
||||
}
|
||||
|
||||
private ViolationNotionResponse mapToResponse(ViolationNotion notion) {
|
||||
return ViolationNotionResponse.builder()
|
||||
.id(notion.getId())
|
||||
.message(notion.getMessage())
|
||||
.createdAt(notion.getCreatedAt())
|
||||
.updatedAt(notion.getUpdatedAt())
|
||||
.user(ViolationNotionResponse.UserInfo.builder()
|
||||
.id(notion.getUser().getId())
|
||||
.fullName(notion.getUser().getFullName())
|
||||
.email(notion.getUser().getEmail())
|
||||
.build())
|
||||
.violation(ViolationNotionResponse.ViolationInfo.builder()
|
||||
.id(notion.getViolation().getId())
|
||||
.url(notion.getViolation().getUrl())
|
||||
.host(notion.getViolation().getHost())
|
||||
.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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user