package com.goldenchart.service; import com.goldenchart.dto.VerificationIssueCommentDto; import com.goldenchart.entity.GcVerificationIssueComment; import com.goldenchart.repository.GcVerificationIssueCommentRepository; import com.goldenchart.repository.GcVerificationIssueRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Optional; @Service @RequiredArgsConstructor public class VerificationIssueCommentService { private final GcVerificationIssueCommentRepository commentRepository; private final GcVerificationIssueRepository issueRepository; public List listByIssue(Long issueId) { return commentRepository.findByIssueIdOrderByCreatedAtAsc(issueId).stream() .map(this::toDto) .toList(); } @Transactional public VerificationIssueCommentDto create( Long issueId, VerificationIssueCommentDto dto, Long userId, String deviceId) { if (!issueRepository.existsById(issueId)) { throw new IllegalArgumentException("issue not found: " + issueId); } GcVerificationIssueComment entity = GcVerificationIssueComment.builder() .issueId(issueId) .content(requireContent(dto.getContent())) .authorName(trimAuthor(dto.getAuthorName())) .userId(userId) .deviceId(deviceId) .build(); return toDto(commentRepository.save(entity)); } @Transactional public Optional update(Long issueId, Long commentId, VerificationIssueCommentDto dto) { return commentRepository.findById(commentId) .filter(c -> issueId.equals(c.getIssueId())) .map(c -> { c.setContent(requireContent(dto.getContent())); if (dto.getAuthorName() != null) { c.setAuthorName(trimAuthor(dto.getAuthorName())); } return toDto(commentRepository.save(c)); }); } @Transactional public boolean delete(Long issueId, Long commentId) { return commentRepository.findById(commentId) .filter(c -> issueId.equals(c.getIssueId())) .map(c -> { commentRepository.delete(c); return true; }) .orElse(false); } private VerificationIssueCommentDto toDto(GcVerificationIssueComment e) { return VerificationIssueCommentDto.builder() .id(e.getId()) .issueId(e.getIssueId()) .content(e.getContent()) .authorName(e.getAuthorName()) .createdAt(e.getCreatedAt()) .updatedAt(e.getUpdatedAt()) .build(); } private String trimContent(String content) { if (content == null) return ""; return content.trim(); } private String requireContent(String content) { String trimmed = trimContent(content); if (trimmed.isEmpty()) { throw new IllegalArgumentException("comment content required"); } return trimmed; } private String trimAuthor(String name) { if (name == null || name.isBlank()) return null; return name.trim(); } }