Skip to content

Commit 612d25a

Browse files
committed
fix2
1 parent 5378fe9 commit 612d25a

15 files changed

Lines changed: 135 additions & 78 deletions

File tree

ewm-service/src/main/java/ru/practicum/ewm/comment/controller/PrivateCommentController.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import org.springframework.validation.annotation.Validated;
99
import org.springframework.web.bind.annotation.*;
1010
import ru.practicum.ewm.comment.dto.CommentDto;
11-
import ru.practicum.ewm.comment.dto.NewCommentDto;
12-
import ru.practicum.ewm.comment.dto.UpdateCommentRequest;
11+
import ru.practicum.ewm.comment.dto.CommentRequestDto;
1312
import ru.practicum.ewm.comment.service.CommentService;
1413

1514
import java.util.List;
@@ -21,19 +20,17 @@
2120
public class PrivateCommentController {
2221
private final CommentService commentService;
2322

24-
@PostMapping("/events/{eventId}/comments")
23+
@PostMapping("/comments")
2524
@ResponseStatus(HttpStatus.CREATED)
2625
public CommentDto createComment(@PathVariable Long userId,
27-
@PathVariable Long eventId,
28-
@Valid @RequestBody NewCommentDto dto) {
29-
return commentService.createComment(userId, eventId, dto);
26+
@Valid @RequestBody CommentRequestDto dto) {
27+
return commentService.createComment(userId, dto);
3028
}
3129

32-
@PatchMapping("/comments/{commentId}")
30+
@PatchMapping("/comments")
3331
public CommentDto updateComment(@PathVariable Long userId,
34-
@PathVariable Long commentId,
35-
@Valid @RequestBody UpdateCommentRequest request) {
36-
return commentService.updateComment(userId, commentId, request);
32+
@Valid @RequestBody CommentRequestDto dto) {
33+
return commentService.updateComment(userId, dto);
3734
}
3835

3936
@DeleteMapping("/comments/{commentId}")

ewm-service/src/main/java/ru/practicum/ewm/comment/dto/NewCommentDto.java renamed to ewm-service/src/main/java/ru/practicum/ewm/comment/dto/CommentRequestDto.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
import lombok.Data;
66

77
@Data
8-
public class NewCommentDto {
8+
public class CommentRequestDto {
99
@NotBlank(message = "Текст комментария не может быть пустым")
1010
@Size(min = 1, max = 2000, message = "Длина комментария от 1 до 2000 символов")
1111
private String text;
12+
13+
private Long eventId;
14+
15+
private Long commentId;
1216
}

ewm-service/src/main/java/ru/practicum/ewm/comment/dto/UpdateCommentRequest.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

ewm-service/src/main/java/ru/practicum/ewm/comment/mapper/CommentMapper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ru.practicum.ewm.comment.mapper;
22

33
import ru.practicum.ewm.comment.dto.CommentDto;
4-
import ru.practicum.ewm.comment.dto.NewCommentDto;
54
import ru.practicum.ewm.comment.model.Comment;
65
import ru.practicum.ewm.comment.model.CommentStatus;
76
import ru.practicum.ewm.event.model.Event;
@@ -12,9 +11,9 @@
1211

1312
public class CommentMapper {
1413

15-
public static Comment toComment(NewCommentDto dto, User author, Event event) {
14+
public static Comment toComment(String text, User author, Event event) {
1615
Comment comment = new Comment();
17-
comment.setText(dto.getText());
16+
comment.setText(text);
1817
comment.setAuthor(author);
1918
comment.setEvent(event);
2019
comment.setCreatedOn(LocalDateTime.now());
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
package ru.practicum.ewm.comment.model;
22

3+
import java.util.Arrays;
4+
import java.util.Optional;
5+
36
public enum CommentStatus {
47
PUBLISHED,
5-
BLOCKED
8+
BLOCKED;
9+
10+
public static Optional<CommentStatus> from(String value) {
11+
if (value == null) {
12+
return Optional.empty();
13+
}
14+
return Arrays.stream(values())
15+
.filter(s -> s.name().equalsIgnoreCase(value))
16+
.findFirst();
17+
}
618
}

ewm-service/src/main/java/ru/practicum/ewm/comment/repository/CommentRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import ru.practicum.ewm.comment.model.Comment;
1010
import ru.practicum.ewm.comment.model.CommentStatus;
1111

12+
import java.util.List;
1213
import java.util.Optional;
1314

1415
public interface CommentRepository extends JpaRepository<Comment, Long> {
@@ -32,4 +33,12 @@ Page<Comment> searchAdmin(@Param("eventId") Long eventId,
3233
@Override
3334
@EntityGraph(attributePaths = {"author", "event"})
3435
Optional<Comment> findById(Long id);
36+
37+
long countByEventIdAndStatus(Long eventId, CommentStatus status);
38+
39+
@Query("SELECT c.event.id, COUNT(c) " +
40+
"FROM Comment c " +
41+
"WHERE c.event.id IN :eventIds AND c.status = 'PUBLISHED' " +
42+
"GROUP BY c.event.id")
43+
List<Object[]> countPublishedByEventIds(@Param("eventIds") List<Long> eventIds);
3544
}

ewm-service/src/main/java/ru/practicum/ewm/comment/service/CommentService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import java.util.List;
66

77
public interface CommentService {
8-
CommentDto createComment(Long userId, Long eventId, NewCommentDto dto);
8+
CommentDto createComment(Long userId, CommentRequestDto dto);
99

10-
CommentDto updateComment(Long userId, Long commentId, UpdateCommentRequest request);
10+
CommentDto updateComment(Long userId, CommentRequestDto dto);
1111

1212
void deleteCommentByUser(Long userId, Long commentId);
1313

ewm-service/src/main/java/ru/practicum/ewm/comment/service/CommentServiceImpl.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,34 @@ public class CommentServiceImpl implements CommentService {
3333

3434
@Override
3535
@Transactional
36-
public CommentDto createComment(Long userId, Long eventId, NewCommentDto dto) {
36+
public CommentDto createComment(Long userId, CommentRequestDto dto) {
37+
if (dto.getEventId() == null) {
38+
throw new IllegalArgumentException("Для создания комментария необходимо указать eventId");
39+
}
40+
3741
User author = userRepository.findById(userId)
3842
.orElseThrow(() -> new NotFoundException("Пользователь с id=" + userId + " не найден"));
39-
Event event = eventRepository.findById(eventId)
40-
.orElseThrow(() -> new NotFoundException("Событие с id=" + eventId + " не найдено"));
43+
Event event = eventRepository.findById(dto.getEventId())
44+
.orElseThrow(() -> new NotFoundException("Событие с id=" + dto.getEventId() + " не найдено"));
4145

4246
if (event.getState() != EventState.PUBLISHED) {
4347
throw new ConflictException("Комментировать можно только опубликованные события");
4448
}
4549

46-
Comment comment = CommentMapper.toComment(dto, author, event);
50+
Comment comment = CommentMapper.toComment(dto.getText(), author, event);
4751
comment = commentRepository.save(comment);
4852
return CommentMapper.toCommentDto(comment);
4953
}
5054

5155
@Override
5256
@Transactional
53-
public CommentDto updateComment(Long userId, Long commentId, UpdateCommentRequest request) {
54-
Comment comment = commentRepository.findById(commentId)
55-
.orElseThrow(() -> new NotFoundException("Комментарий с id=" + commentId + " не найден"));
57+
public CommentDto updateComment(Long userId, CommentRequestDto dto) {
58+
if (dto.getCommentId() == null) {
59+
throw new IllegalArgumentException("Для обновления комментария необходимо указать commentId");
60+
}
61+
62+
Comment comment = commentRepository.findById(dto.getCommentId())
63+
.orElseThrow(() -> new NotFoundException("Комментарий с id=" + dto.getCommentId() + " не найден"));
5664

5765
if (!comment.getAuthor().getId().equals(userId)) {
5866
throw new ConflictException("Редактировать можно только свои комментарии");
@@ -61,7 +69,7 @@ public CommentDto updateComment(Long userId, Long commentId, UpdateCommentReques
6169
throw new ConflictException("Нельзя редактировать заблокированный комментарий");
6270
}
6371

64-
comment.setText(request.getText());
72+
comment.setText(dto.getText());
6573
comment.setEditedOn(LocalDateTime.now());
6674
comment = commentRepository.save(comment);
6775
return CommentMapper.toCommentDto(comment);
@@ -113,11 +121,8 @@ public List<CommentDto> getPublishedCommentsByEvent(Long eventId, int from, int
113121
public List<CommentDto> searchCommentsAdmin(Long eventId, Long authorId, String statusStr, int from, int size) {
114122
CommentStatus status = null;
115123
if (statusStr != null) {
116-
try {
117-
status = CommentStatus.valueOf(statusStr.toUpperCase());
118-
} catch (IllegalArgumentException e) {
119-
throw new IllegalArgumentException("Некорректный статус: " + statusStr);
120-
}
124+
status = CommentStatus.from(statusStr)
125+
.orElseThrow(() -> new IllegalArgumentException("Некорректный статус: " + statusStr));
121126
}
122127

123128
int page = from / size;

ewm-service/src/main/java/ru/practicum/ewm/event/dto/EventFullDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ public class EventFullDto {
3636
private String state;
3737
private String title;
3838
private Long views;
39+
private Long commentsCount;
3940
}

ewm-service/src/main/java/ru/practicum/ewm/event/dto/EventShortDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ public class EventShortDto {
2424
private Boolean paid;
2525
private String title;
2626
private Long views;
27+
private Long commentsCount;
2728
}

0 commit comments

Comments
 (0)