Back готов

This commit is contained in:
maxnes3 2023-12-26 23:27:23 +04:00
parent 1e86222db0
commit eeea0d4d3f
6 changed files with 111 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,12 @@
package com.android.mobileappserver.Report;
import lombok.*;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ReportCreateDTO {
private Long dateFrom;
private Long dateTo;
}

View File

@ -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;
}
}

View File

@ -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<StoryModel> stories = storyService.getStoriesByDate(reportCreateDTO.getDateFrom(), reportCreateDTO.getDateTo());
int postCount = stories.size();
Map<String, Integer> 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<String, Integer> maxEntry = Collections.max(authorWithCount.entrySet(), Map.Entry.comparingByValue());
return new ReportDTO(reportCreateDTO.getDateFrom(), reportCreateDTO.getDateTo(),
postCount, maxEntry.getKey(), maxEntry.getValue());
}
}

View File

@ -3,7 +3,14 @@ package com.android.mobileappserver.Story;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; 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<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 _story s WHERE s.dateFrom = :dateFrom AND s.dateTo = :dateTo")
List<StoryModel> findStoriesBetweenDates(@Param("dateFrom") Long dateFrom, @Param("dateTo") Long dateTo);
} }

View File

@ -7,6 +7,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Service @Service
@ -64,4 +65,9 @@ public class StoryService {
public Page<StoryModel> getUserStoryPaged(Long userId, int page, int size){ public Page<StoryModel> getUserStoryPaged(Long userId, int page, int size){
return storyRepository.findAllByUserId(userId, PageRequest.of(page - 1, size)); return storyRepository.findAllByUserId(userId, PageRequest.of(page - 1, size));
} }
@Transactional
public List<StoryModel> getStoriesByDate(Long dateFrom, Long dateTo){
return storyRepository.findStoriesBetweenDates(dateFrom, dateTo);
}
} }