Compare commits
No commits in common. "c44b373be0cfcd958c5545fff55024b9cac06953" and "eeea0d4d3fe8c0d667865fbfef1c5c99c2b7b655" have entirely different histories.
c44b373be0
...
eeea0d4d3f
@ -1,6 +1,9 @@
|
|||||||
package com.android.mobileappserver.Report;
|
package com.android.mobileappserver.Report;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
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
|
@RestController
|
||||||
@RequestMapping("api/report")
|
@RequestMapping("api/report")
|
||||||
@ -11,9 +14,8 @@ public class ReportController {
|
|||||||
this.reportService = reportService;
|
this.reportService = reportService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/createReport/{dateFrom}/{dateTo}")
|
@PostMapping("/createReport")
|
||||||
public ReportDTO createReport(@PathVariable("dateFrom") Long dateFrom,
|
public ReportDTO createReport(@RequestBody ReportCreateDTO reportCreateDTO){
|
||||||
@PathVariable("dateTo") Long dateTo){
|
return reportService.createReport(reportCreateDTO);
|
||||||
return reportService.createReport(dateFrom, dateTo);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.android.mobileappserver.Report;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class ReportCreateDTO {
|
||||||
|
private Long dateFrom;
|
||||||
|
private Long dateTo;
|
||||||
|
}
|
@ -1,13 +1,9 @@
|
|||||||
package com.android.mobileappserver.Report;
|
package com.android.mobileappserver.Report;
|
||||||
|
|
||||||
import com.android.mobileappserver.Story.StoryDTO;
|
|
||||||
import com.android.mobileappserver.User.UserDTO;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@ -15,16 +11,14 @@ public class ReportDTO {
|
|||||||
private Long dateFrom;
|
private Long dateFrom;
|
||||||
private Long dateTo;
|
private Long dateTo;
|
||||||
private int postCount;
|
private int postCount;
|
||||||
private UserDTO mostPostAuthor;
|
private String mostPostAuthor;
|
||||||
private int mostPostCount;
|
private int mostPostCount;
|
||||||
private List<StoryDTO> listPostAuthor;
|
|
||||||
|
|
||||||
public ReportDTO(Long dateFrom, Long dateTo, int postCount, UserDTO mostPostAuthor, int mostPostCount, List<StoryDTO> listPostAuthor){
|
public ReportDTO(Long dateFrom, Long dateTo, int postCount, String mostPostAuthor, int mostPostCount){
|
||||||
this.dateFrom = dateFrom;
|
this.dateFrom = dateFrom;
|
||||||
this.dateTo = dateTo;
|
this.dateTo = dateTo;
|
||||||
this.postCount = postCount;
|
this.postCount = postCount;
|
||||||
this.mostPostAuthor = mostPostAuthor;
|
this.mostPostAuthor = mostPostAuthor;
|
||||||
this.mostPostCount = mostPostCount;
|
this.mostPostCount = mostPostCount;
|
||||||
this.listPostAuthor = listPostAuthor;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,8 @@
|
|||||||
package com.android.mobileappserver.Report;
|
package com.android.mobileappserver.Report;
|
||||||
|
|
||||||
import com.android.mobileappserver.Story.StoryDTO;
|
|
||||||
import com.android.mobileappserver.Story.StoryModel;
|
import com.android.mobileappserver.Story.StoryModel;
|
||||||
import com.android.mobileappserver.Story.StoryService;
|
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 com.android.mobileappserver.User.UserService;
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -14,7 +10,6 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Service
|
|
||||||
public class ReportService {
|
public class ReportService {
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final StoryService storyService;
|
private final StoryService storyService;
|
||||||
@ -25,25 +20,22 @@ public class ReportService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public ReportDTO createReport(Long dateFrom, Long dateTo){
|
public ReportDTO createReport(ReportCreateDTO reportCreateDTO){
|
||||||
List<StoryModel> stories = storyService.getStoriesByDate(dateFrom, dateTo);
|
List<StoryModel> stories = storyService.getStoriesByDate(reportCreateDTO.getDateFrom(), reportCreateDTO.getDateTo());
|
||||||
int postCount = stories.size();
|
int postCount = stories.size();
|
||||||
Map<Long, Integer> authorWithCount = new HashMap<>();
|
Map<String, Integer> authorWithCount = new HashMap<>();
|
||||||
for (var story: stories) {
|
for (var story: stories) {
|
||||||
Long authorId = story.getUser().getId();
|
String authorName = story.getUser().getLogin();
|
||||||
if (authorWithCount.containsKey(authorId)) {
|
if (authorWithCount.containsKey(authorName)) {
|
||||||
// Если автор уже есть, увеличиваем количество историй
|
// Если автор уже есть, увеличиваем количество историй
|
||||||
authorWithCount.put(authorId, authorWithCount.get(authorId) + 1);
|
authorWithCount.put(authorName, authorWithCount.get(authorName) + 1);
|
||||||
} else {
|
} else {
|
||||||
// Если автора еще нет в мапе, добавляем его с количеством 1
|
// Если автора еще нет в мапе, добавляем его с количеством 1
|
||||||
authorWithCount.put(authorId, 1);
|
authorWithCount.put(authorName, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Map.Entry<Long, Integer> maxEntry = Collections.max(authorWithCount.entrySet(), Map.Entry.comparingByValue());
|
Map.Entry<String, Integer> maxEntry = Collections.max(authorWithCount.entrySet(), Map.Entry.comparingByValue());
|
||||||
UserModel mostPostAuthor = userService.getUserById(maxEntry.getKey());
|
return new ReportDTO(reportCreateDTO.getDateFrom(), reportCreateDTO.getDateTo(),
|
||||||
return new ReportDTO(dateFrom, dateTo, postCount, new UserDTO(mostPostAuthor), maxEntry.getValue(),
|
postCount, maxEntry.getKey(), maxEntry.getValue());
|
||||||
mostPostAuthor.getStories().stream().map(
|
|
||||||
StoryDTO::new
|
|
||||||
).toList());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,6 @@ import java.util.List;
|
|||||||
public interface StoryRepository extends JpaRepository<StoryModel, Long> {
|
public interface StoryRepository extends JpaRepository<StoryModel, Long> {
|
||||||
Page<StoryModel> findAllByUserId(Long userIdб, Pageable pageable);
|
Page<StoryModel> findAllByUserId(Long userIdб, Pageable pageable);
|
||||||
|
|
||||||
@Query("SELECT s FROM StoryModel s WHERE s.postdate >= :dateFrom AND s.postdate <= :dateTo")
|
@Query("SELECT s FROM _story s WHERE s.dateFrom = :dateFrom AND s.dateTo = :dateTo")
|
||||||
List<StoryModel> findStoriesBetweenDates(@Param("dateFrom") Long dateFrom, @Param("dateTo") Long dateTo);
|
List<StoryModel> findStoriesBetweenDates(@Param("dateFrom") Long dateFrom, @Param("dateTo") Long dateTo);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package com.android.mobileappserver.User;
|
package com.android.mobileappserver.User;
|
||||||
|
|
||||||
import com.android.mobileappserver.Story.StoryModel;
|
import com.android.mobileappserver.Story.StoryModel;
|
||||||
|
import com.android.mobileappserver.Story.StoryNotFoundException;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user