Skip to content

Диплом (2 часть)#2

Merged
AndreyMMGH merged 15 commits into
mainfrom
main_svc
Aug 25, 2025
Merged

Диплом (2 часть)#2
AndreyMMGH merged 15 commits into
mainfrom
main_svc

Conversation

@AndreyMMGH

Copy link
Copy Markdown
Owner

No description provided.

@kesch9 kesch9 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Несколько комментариев

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Category)) return false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно упростить запись equals
if (this == o) return true;
if (!(o instanceof Category category)) return false;
return Objects.equals(id, category.id);

private Long id;

@Email
@Size(max = 254)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Валидационные аннотации для Entity не нужны

LocalDateTime endDate = end;

return (root, query, cb) -> {
if (startDate != null && endDate != null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь вы немного запутались с условиями и даже idea подсвечивает об этом
startDate не может быть null, вы выше инициализируете переменную, даже если null и в итоге всё сводится

    public static Specification<Event> dateBetween(LocalDateTime start, LocalDateTime end) {
        LocalDateTime startDate = (start != null) ? start : LocalDateTime.now();
        return (root, query, cb) -> {
            if (end != null) {
                return cb.between(root.get("eventDate"), startDate, end);
            } else {
                return cb.greaterThanOrEqualTo(root.get("eventDate"), startDate);
            }
        };
    }

@Column(nullable = false, length = 120)
private String title;

private Long views;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

views не храниться в бд, а получаем из сервиса статистики

Event event = eventRepository.findByIdAndState(id, EventState.PUBLISHED)
.orElseThrow(() -> new NotFoundException("Опубликованное событие с id:" + id + " не найдено"));

statsClient.createHit(new StatsRequestDto(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Помимо сохранения, вы должны получить статистику из statsClient.
Рекомендую также создать dto для запроса и получиться вот так

        Long views = statsClient.getStats(
                        ViewsStatsRequest.builder()
                                .uri("/events/" + id)
                                .start(event.getPublishedOn())
                                .end(LocalDateTime.now())
                                .unique(true)
                                .build()
                )
                .stream()
                .findAny()
                .map(StatsResponseDto::getHits)
                .orElse(0L);

List<Event> events = eventRepository.findAll(spec, pageable).getContent();

statsClient.createHit(new StatsRequestDto("ewm", request.getRequestURI(), request.getRemoteAddr(), LocalDateTime.now()));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для заполнения views нужно запросить количество просмотров каждого события у сервиса статистики

		Map<String, Long> eventUriAndIdMap = events.stream()
				.map(Event::getId)
				.collect(Collectors.toMap(id -> "/events/" + id, Function.identity()));

		List<ViewStats> stats = statsClient.getStats(
				ViewsStatsRequest.builder()
						.uris(eventUriAndIdMap.keySet())
						.unique(true)
						.build()
		);

@kesch9 kesch9 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve

@AndreyMMGH
AndreyMMGH merged commit d1eff12 into main Aug 25, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants