2026-01-20 03:47:51 +07:00
|
|
|
package ru.soune.nocopy.service.user;
|
2025-11-26 13:03:20 +07:00
|
|
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2026-01-20 03:47:51 +07:00
|
|
|
import ru.soune.nocopy.dto.user.UserContentRequest;
|
|
|
|
|
import ru.soune.nocopy.dto.user.UserContentUpdateRequest;
|
2025-12-17 13:41:05 +07:00
|
|
|
import ru.soune.nocopy.entity.file.FileType;
|
2026-01-16 20:20:14 +07:00
|
|
|
import ru.soune.nocopy.entity.user.User;
|
|
|
|
|
import ru.soune.nocopy.entity.user.UserContent;
|
2025-12-17 13:41:05 +07:00
|
|
|
import ru.soune.nocopy.exception.ContentNotFoundException;
|
|
|
|
|
import ru.soune.nocopy.exception.UserNotFoundException;
|
|
|
|
|
import ru.soune.nocopy.mapper.UserContentMapper;
|
|
|
|
|
import ru.soune.nocopy.repository.UserContentRepository;
|
|
|
|
|
import ru.soune.nocopy.repository.UserRepository;
|
2025-11-26 13:03:20 +07:00
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@AllArgsConstructor
|
|
|
|
|
public class UserContentService {
|
|
|
|
|
|
|
|
|
|
private final UserContentRepository userContentRepository;
|
|
|
|
|
|
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
|
|
|
|
private final UserContentMapper userContentMapper;
|
|
|
|
|
|
|
|
|
|
public List<UserContent> findByUser(long userId) {
|
|
|
|
|
return userContentRepository.findByUserId(userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public UserContent addUserContent(UserContentRequest userContentRequest) {
|
|
|
|
|
UserContent userContent = new UserContent();
|
|
|
|
|
|
|
|
|
|
Optional<User> byEmail = userRepository.findByEmail(userContentRequest.userEmail());
|
|
|
|
|
User user = byEmail.orElseThrow(() -> new UserNotFoundException(userContentRequest.userEmail()));
|
|
|
|
|
|
|
|
|
|
userContent.setUser(user);
|
|
|
|
|
userContent.setFilename(userContentRequest.fileName());
|
|
|
|
|
userContent.setFileExtension(userContentRequest.fileExtension());
|
|
|
|
|
userContent.setFilePath(userContentRequest.filePath());
|
|
|
|
|
userContent.setFileType(FileType.valueOf(userContentRequest.fileType()));
|
|
|
|
|
userContent.setOriginalFilename(userContentRequest.originalFilename());
|
|
|
|
|
userContent.setProtectionLevel(userContentRequest.protectionLevel());
|
|
|
|
|
userContent.setFileSize(userContentRequest.fileSize());
|
|
|
|
|
|
|
|
|
|
//TODO add
|
|
|
|
|
// @Column(name = "protection_hash", length = 64)
|
|
|
|
|
// private String protectionHash;
|
|
|
|
|
// @Column(name = "watermark_id", length = 50)
|
|
|
|
|
// private String watermarkId;
|
|
|
|
|
|
|
|
|
|
return userContentRepository.save(userContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public UserContent updateUserContent(UserContentUpdateRequest userContentRequest) {
|
|
|
|
|
UserContent entity = userContentRepository.findById(userContentRequest.id())
|
|
|
|
|
.orElseThrow(() -> new ContentNotFoundException("Content not found with: " + userContentRequest.id()));
|
|
|
|
|
|
|
|
|
|
userContentMapper.updateEntityFromDto(userContentRequest, entity);
|
|
|
|
|
|
|
|
|
|
return userContentRepository.save(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deleteUserContent(long userContentId) {
|
|
|
|
|
userContentRepository.deleteById(userContentId);
|
|
|
|
|
}
|
|
|
|
|
}
|