diff --git a/src/main/java/com/android/mobileappserver/Report/ReportDTO.java b/src/main/java/com/android/mobileappserver/Report/ReportDTO.java index eac6243..011604d 100644 --- a/src/main/java/com/android/mobileappserver/Report/ReportDTO.java +++ b/src/main/java/com/android/mobileappserver/Report/ReportDTO.java @@ -1,9 +1,13 @@ package com.android.mobileappserver.Report; +import com.android.mobileappserver.Story.StoryDTO; +import com.android.mobileappserver.User.UserDTO; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import java.util.List; + @Getter @Setter @NoArgsConstructor @@ -11,14 +15,16 @@ public class ReportDTO { private Long dateFrom; private Long dateTo; private int postCount; - private String mostPostAuthor; + private UserDTO mostPostAuthor; private int mostPostCount; + private List listPostAuthor; - public ReportDTO(Long dateFrom, Long dateTo, int postCount, String mostPostAuthor, int mostPostCount){ + public ReportDTO(Long dateFrom, Long dateTo, int postCount, UserDTO mostPostAuthor, int mostPostCount, List listPostAuthor){ this.dateFrom = dateFrom; this.dateTo = dateTo; this.postCount = postCount; this.mostPostAuthor = mostPostAuthor; this.mostPostCount = mostPostCount; + this.listPostAuthor = listPostAuthor; } } diff --git a/src/main/java/com/android/mobileappserver/Report/ReportService.java b/src/main/java/com/android/mobileappserver/Report/ReportService.java index 634c6fa..77b5e54 100644 --- a/src/main/java/com/android/mobileappserver/Report/ReportService.java +++ b/src/main/java/com/android/mobileappserver/Report/ReportService.java @@ -1,7 +1,10 @@ package com.android.mobileappserver.Report; +import com.android.mobileappserver.Story.StoryDTO; import com.android.mobileappserver.Story.StoryModel; import com.android.mobileappserver.Story.StoryService; +import com.android.mobileappserver.User.UserDTO; +import com.android.mobileappserver.User.UserModel; import com.android.mobileappserver.User.UserService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -25,19 +28,22 @@ public class ReportService { public ReportDTO createReport(Long dateFrom, Long dateTo){ List stories = storyService.getStoriesByDate(dateFrom, dateTo); int postCount = stories.size(); - Map authorWithCount = new HashMap<>(); + Map authorWithCount = new HashMap<>(); for (var story: stories) { - String authorName = story.getUser().getLogin(); - if (authorWithCount.containsKey(authorName)) { + Long authorId = story.getUser().getId(); + if (authorWithCount.containsKey(authorId)) { // Если автор уже есть, увеличиваем количество историй - authorWithCount.put(authorName, authorWithCount.get(authorName) + 1); + authorWithCount.put(authorId, authorWithCount.get(authorId) + 1); } else { // Если автора еще нет в мапе, добавляем его с количеством 1 - authorWithCount.put(authorName, 1); + authorWithCount.put(authorId, 1); } } - Map.Entry maxEntry = Collections.max(authorWithCount.entrySet(), Map.Entry.comparingByValue()); - return new ReportDTO(dateFrom, dateTo, - postCount, maxEntry.getKey(), maxEntry.getValue()); + Map.Entry maxEntry = Collections.max(authorWithCount.entrySet(), Map.Entry.comparingByValue()); + UserModel mostPostAuthor = userService.getUserById(maxEntry.getKey()); + return new ReportDTO(dateFrom, dateTo, postCount, new UserDTO(mostPostAuthor), maxEntry.getValue(), + mostPostAuthor.getStories().stream().map( + StoryDTO::new + ).toList()); } }