|
| 1 | +package com.cloudmedia.content.application.content; |
| 2 | + |
| 3 | +import com.cloudmedia.content.api.content.dto.ContentResponse; |
| 4 | +import com.cloudmedia.content.api.content.dto.CreateContentRequest; |
| 5 | +import com.cloudmedia.content.api.content.dto.UpdateContentRequest; |
| 6 | +import com.cloudmedia.content.error.ApiException; |
| 7 | +import com.cloudmedia.content.persistence.entity.ChannelEntity; |
| 8 | +import com.cloudmedia.content.persistence.entity.ContentEntity; |
| 9 | +import com.cloudmedia.content.persistence.entity.ContentState; |
| 10 | +import com.cloudmedia.content.persistence.entity.ContentVisibility; |
| 11 | +import com.cloudmedia.content.persistence.repository.ChannelMemberRepository; |
| 12 | +import com.cloudmedia.content.persistence.repository.ChannelRepository; |
| 13 | +import com.cloudmedia.content.persistence.repository.ContentRepository; |
| 14 | +import java.time.LocalDateTime; |
| 15 | +import java.util.UUID; |
| 16 | +import org.springframework.http.HttpStatus; |
| 17 | +import org.springframework.stereotype.Service; |
| 18 | +import org.springframework.transaction.annotation.Transactional; |
| 19 | + |
| 20 | +@Service |
| 21 | +public class ContentService { |
| 22 | + |
| 23 | + private final ContentRepository contentRepository; |
| 24 | + private final ChannelRepository channelRepository; |
| 25 | + private final ChannelMemberRepository channelMemberRepository; |
| 26 | + |
| 27 | + public ContentService(ContentRepository contentRepository, ChannelRepository channelRepository, |
| 28 | + ChannelMemberRepository channelMemberRepository) { |
| 29 | + this.contentRepository = contentRepository; |
| 30 | + this.channelRepository = channelRepository; |
| 31 | + this.channelMemberRepository = channelMemberRepository; |
| 32 | + } |
| 33 | + |
| 34 | + @Transactional |
| 35 | + public ContentResponse createDraft(CreateContentRequest request) { |
| 36 | + ChannelEntity channel = channelRepository.findById(request.channelId()).orElseThrow( |
| 37 | + () -> new ApiException(HttpStatus.NOT_FOUND, "CHANNEL_NOT_FOUND", "Channel not found", null)); |
| 38 | + assertMember(request.channelId(), request.userId()); |
| 39 | + |
| 40 | + LocalDateTime now = LocalDateTime.now(); |
| 41 | + ContentEntity content = new ContentEntity(); |
| 42 | + content.setId(UUID.randomUUID().toString()); |
| 43 | + content.setChannel(channel); |
| 44 | + content.setTitle(request.title()); |
| 45 | + content.setDescription(request.description()); |
| 46 | + content.setContentType(request.contentType()); |
| 47 | + content.setState(ContentState.DRAFT); |
| 48 | + content.setVisibility(request.visibility() == null ? ContentVisibility.PRIVATE : request.visibility()); |
| 49 | + content.setPlaybackReady(false); |
| 50 | + content.setCreatedAt(now); |
| 51 | + content.setUpdatedAt(now); |
| 52 | + content.setPublishedAt(null); |
| 53 | + |
| 54 | + return toResponse(contentRepository.save(content)); |
| 55 | + } |
| 56 | + |
| 57 | + @Transactional |
| 58 | + public ContentResponse updateMetadata(String contentId, UpdateContentRequest request) { |
| 59 | + ContentEntity content = contentRepository.findById(contentId).orElseThrow( |
| 60 | + () -> new ApiException(HttpStatus.NOT_FOUND, "CONTENT_NOT_FOUND", "Content not found", null)); |
| 61 | + assertMember(content.getChannel().getId(), request.userId()); |
| 62 | + |
| 63 | + if (request.title() != null) { |
| 64 | + content.setTitle(request.title()); |
| 65 | + } |
| 66 | + if (request.description() != null) { |
| 67 | + content.setDescription(request.description()); |
| 68 | + } |
| 69 | + if (request.visibility() != null) { |
| 70 | + content.setVisibility(request.visibility()); |
| 71 | + } |
| 72 | + content.setUpdatedAt(LocalDateTime.now()); |
| 73 | + |
| 74 | + return toResponse(contentRepository.save(content)); |
| 75 | + } |
| 76 | + |
| 77 | + private void assertMember(String channelId, String userId) { |
| 78 | + if (channelMemberRepository.findByChannel_IdAndUserId(channelId, userId).isEmpty()) { |
| 79 | + throw new ApiException(HttpStatus.FORBIDDEN, "CHANNEL_ACCESS_DENIED", "User is not a member of the channel", |
| 80 | + null); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private ContentResponse toResponse(ContentEntity content) { |
| 85 | + return new ContentResponse(content.getId(), content.getChannel().getId(), content.getTitle(), |
| 86 | + content.getDescription(), content.getContentType(), content.getState(), content.getVisibility(), |
| 87 | + content.isPlaybackReady(), content.getCreatedAt(), content.getUpdatedAt(), content.getPublishedAt()); |
| 88 | + } |
| 89 | +} |
0 commit comments