|
| 1 | +package ru.practicum.ewm.serices; |
| 2 | + |
| 3 | +import lombok.AllArgsConstructor; |
| 4 | +import org.springframework.data.domain.PageRequest; |
| 5 | +import org.springframework.data.domain.Pageable; |
| 6 | +import org.springframework.stereotype.Service; |
| 7 | +import org.springframework.transaction.annotation.Transactional; |
| 8 | +import ru.practicum.ewm.dto.CommentDto; |
| 9 | +import ru.practicum.ewm.exceptions.ConflictException; |
| 10 | +import ru.practicum.ewm.exceptions.NotFoundException; |
| 11 | +import ru.practicum.ewm.repository.CommentRepository; |
| 12 | +import ru.practicum.ewm.repository.EventRepository; |
| 13 | +import ru.practicum.ewm.repository.UserRepository; |
| 14 | + |
| 15 | +import java.sql.Timestamp; |
| 16 | +import java.time.LocalDateTime; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +@Service |
| 20 | +@AllArgsConstructor |
| 21 | +public class CommentService { |
| 22 | + private final CommentRepository commentRepository; |
| 23 | + private final UserRepository userRepository; |
| 24 | + private final EventRepository eventRepository; |
| 25 | + |
| 26 | + @Transactional |
| 27 | + public void deleteComment(long commentId) { |
| 28 | + if (!commentRepository.existsById(commentId)) |
| 29 | + throw new NotFoundException("Not found comment", commentId); |
| 30 | + |
| 31 | + commentRepository.deleteById(commentId); |
| 32 | + } |
| 33 | + |
| 34 | + @Transactional |
| 35 | + public void deleteComment(long commentId, long userId) { |
| 36 | + var comment = commentRepository.findById(commentId) |
| 37 | + .orElseThrow(() -> new NotFoundException("Not found comment", commentId)); |
| 38 | + |
| 39 | + if (comment.getAuthor().getId() != userId) |
| 40 | + throw new ConflictException("User is not the author of comment."); |
| 41 | + |
| 42 | + commentRepository.deleteById(commentId); |
| 43 | + } |
| 44 | + |
| 45 | + @Transactional |
| 46 | + public CommentDto createComment(long eventId, long userId, CommentDto commentDto) { |
| 47 | + var userEntity = userRepository.findById(userId) |
| 48 | + .orElseThrow(() -> new NotFoundException("Not found user", userId)); |
| 49 | + |
| 50 | + var eventEntity = eventRepository.findById(eventId) |
| 51 | + .orElseThrow(() -> new NotFoundException("Not found event", eventId)); |
| 52 | + |
| 53 | + var commentEntity = Mapper.toCommentEntity(commentDto); |
| 54 | + |
| 55 | + commentEntity.setCreated(Timestamp.valueOf(LocalDateTime.now())); |
| 56 | + commentEntity.setAuthor(userEntity); |
| 57 | + commentEntity.setEventEntity(eventEntity); |
| 58 | + |
| 59 | + commentRepository.saveAndFlush(commentEntity); |
| 60 | + |
| 61 | + return Mapper.toCommentDto(commentEntity); |
| 62 | + } |
| 63 | + |
| 64 | + @Transactional |
| 65 | + public CommentDto updateComment(long commentId, long userId, CommentDto commentDto) { |
| 66 | + var text = commentDto.text(); |
| 67 | + |
| 68 | + if (text == null || text.isBlank()) |
| 69 | + return commentDto; |
| 70 | + |
| 71 | + var commentEntity = commentRepository.findById(commentId) |
| 72 | + .orElseThrow(() -> new NotFoundException("Not found comment", commentId)); |
| 73 | + |
| 74 | + if (commentEntity.getAuthor().getId() != userId) |
| 75 | + throw new ConflictException("User is not the author of comment."); |
| 76 | + |
| 77 | + commentEntity.setText(text); |
| 78 | + commentEntity.setLastUpdateTime(Timestamp.valueOf(LocalDateTime.now())); |
| 79 | + |
| 80 | + commentRepository.saveAndFlush(commentEntity); |
| 81 | + |
| 82 | + return Mapper.toCommentDto(commentEntity); |
| 83 | + } |
| 84 | + |
| 85 | + public List<CommentDto> getAll(long eventId, int from, int size) { |
| 86 | + var pageable = initPageable(from, size); |
| 87 | + |
| 88 | + var page = commentRepository.findByEventEntityId(eventId, pageable); |
| 89 | + |
| 90 | + return page.stream() |
| 91 | + .map(Mapper::toCommentDto) |
| 92 | + .toList(); |
| 93 | + } |
| 94 | + |
| 95 | + public static Pageable initPageable(int from, int size) { |
| 96 | + if (from <= 0) |
| 97 | + return PageRequest.ofSize(size); |
| 98 | + |
| 99 | + return PageRequest.of(from / size, size); |
| 100 | + } |
| 101 | +} |
0 commit comments