From eeea0d4d3fe8c0d667865fbfef1c5c99c2b7b655 Mon Sep 17 00:00:00 2001 From: maxnes3 <112558334+maxnes3@users.noreply.github.com> Date: Tue, 26 Dec 2023 23:27:23 +0400 Subject: [PATCH] =?UTF-8?q?Back=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Report/ReportController.java | 21 ++++++++++ .../Report/ReportCreateDTO.java | 12 ++++++ .../mobileappserver/Report/ReportDTO.java | 24 +++++++++++ .../mobileappserver/Report/ReportService.java | 41 +++++++++++++++++++ .../Story/StoryRepository.java | 7 ++++ .../mobileappserver/Story/StoryService.java | 6 +++ 6 files changed, 111 insertions(+) create mode 100644 src/main/java/com/android/mobileappserver/Report/ReportController.java create mode 100644 src/main/java/com/android/mobileappserver/Report/ReportCreateDTO.java create mode 100644 src/main/java/com/android/mobileappserver/Report/ReportDTO.java create mode 100644 src/main/java/com/android/mobileappserver/Report/ReportService.java diff --git a/src/main/java/com/android/mobileappserver/Report/ReportController.java b/src/main/java/com/android/mobileappserver/Report/ReportController.java new file mode 100644 index 0000000..a54483b --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Report/ReportController.java @@ -0,0 +1,21 @@ +package com.android.mobileappserver.Report; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("api/report") +public class ReportController { + private final ReportService reportService; + + public ReportController(ReportService reportService){ + this.reportService = reportService; + } + + @PostMapping("/createReport") + public ReportDTO createReport(@RequestBody ReportCreateDTO reportCreateDTO){ + return reportService.createReport(reportCreateDTO); + } +} diff --git a/src/main/java/com/android/mobileappserver/Report/ReportCreateDTO.java b/src/main/java/com/android/mobileappserver/Report/ReportCreateDTO.java new file mode 100644 index 0000000..13b300a --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Report/ReportCreateDTO.java @@ -0,0 +1,12 @@ +package com.android.mobileappserver.Report; + +import lombok.*; + +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class ReportCreateDTO { + private Long dateFrom; + private Long dateTo; +} diff --git a/src/main/java/com/android/mobileappserver/Report/ReportDTO.java b/src/main/java/com/android/mobileappserver/Report/ReportDTO.java new file mode 100644 index 0000000..eac6243 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Report/ReportDTO.java @@ -0,0 +1,24 @@ +package com.android.mobileappserver.Report; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class ReportDTO { + private Long dateFrom; + private Long dateTo; + private int postCount; + private String mostPostAuthor; + private int mostPostCount; + + public ReportDTO(Long dateFrom, Long dateTo, int postCount, String mostPostAuthor, int mostPostCount){ + this.dateFrom = dateFrom; + this.dateTo = dateTo; + this.postCount = postCount; + this.mostPostAuthor = mostPostAuthor; + this.mostPostCount = mostPostCount; + } +} diff --git a/src/main/java/com/android/mobileappserver/Report/ReportService.java b/src/main/java/com/android/mobileappserver/Report/ReportService.java new file mode 100644 index 0000000..c37a9a6 --- /dev/null +++ b/src/main/java/com/android/mobileappserver/Report/ReportService.java @@ -0,0 +1,41 @@ +package com.android.mobileappserver.Report; + +import com.android.mobileappserver.Story.StoryModel; +import com.android.mobileappserver.Story.StoryService; +import com.android.mobileappserver.User.UserService; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ReportService { + private final UserService userService; + private final StoryService storyService; + + public ReportService(UserService userService, StoryService storyService){ + this.userService = userService; + this.storyService = storyService; + } + + @Transactional + public ReportDTO createReport(ReportCreateDTO reportCreateDTO){ + List stories = storyService.getStoriesByDate(reportCreateDTO.getDateFrom(), reportCreateDTO.getDateTo()); + int postCount = stories.size(); + Map authorWithCount = new HashMap<>(); + for (var story: stories) { + String authorName = story.getUser().getLogin(); + if (authorWithCount.containsKey(authorName)) { + // Если автор уже есть, увеличиваем количество историй + authorWithCount.put(authorName, authorWithCount.get(authorName) + 1); + } else { + // Если автора еще нет в мапе, добавляем его с количеством 1 + authorWithCount.put(authorName, 1); + } + } + Map.Entry maxEntry = Collections.max(authorWithCount.entrySet(), Map.Entry.comparingByValue()); + return new ReportDTO(reportCreateDTO.getDateFrom(), reportCreateDTO.getDateTo(), + postCount, maxEntry.getKey(), maxEntry.getValue()); + } +} diff --git a/src/main/java/com/android/mobileappserver/Story/StoryRepository.java b/src/main/java/com/android/mobileappserver/Story/StoryRepository.java index 2ec41cf..697542b 100644 --- a/src/main/java/com/android/mobileappserver/Story/StoryRepository.java +++ b/src/main/java/com/android/mobileappserver/Story/StoryRepository.java @@ -3,7 +3,14 @@ package com.android.mobileappserver.Story; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; public interface StoryRepository extends JpaRepository { Page findAllByUserId(Long userIdб, Pageable pageable); + + @Query("SELECT s FROM _story s WHERE s.dateFrom = :dateFrom AND s.dateTo = :dateTo") + List findStoriesBetweenDates(@Param("dateFrom") Long dateFrom, @Param("dateTo") Long dateTo); } diff --git a/src/main/java/com/android/mobileappserver/Story/StoryService.java b/src/main/java/com/android/mobileappserver/Story/StoryService.java index 90bb436..4e3cbe5 100644 --- a/src/main/java/com/android/mobileappserver/Story/StoryService.java +++ b/src/main/java/com/android/mobileappserver/Story/StoryService.java @@ -7,6 +7,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.Optional; @Service @@ -64,4 +65,9 @@ public class StoryService { public Page getUserStoryPaged(Long userId, int page, int size){ return storyRepository.findAllByUserId(userId, PageRequest.of(page - 1, size)); } + + @Transactional + public List getStoriesByDate(Long dateFrom, Long dateTo){ + return storyRepository.findStoriesBetweenDates(dateFrom, dateTo); + } }