diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/SeriesController.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/SeriesController.cs index b5e82d5..5afe5ca 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/SeriesController.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Controllers/SeriesController.cs @@ -71,5 +71,15 @@ public class SeriesController : ControllerBase #endregion + #region View count + + [HttpGet("view")] + [AllowAnonymous] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task GetSeriesViewRank([FromQuery] int first = 5, [FromQuery] int days = 7) => await _seriesControllerService.GetSeriesViewRank(first, days); + + #endregion + #endregion } \ No newline at end of file diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Series/ISeriesControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Series/ISeriesControllerService.cs index 3c78ae5..aac8fdd 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Series/ISeriesControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Series/ISeriesControllerService.cs @@ -10,4 +10,6 @@ public interface ISeriesControllerService Task PostSeries(SeriesRequest data); Task PutSeries(long id, SeriesRequest data); Task DeleteSeries(long id); + + Task GetSeriesViewRank(int first, int days); } \ No newline at end of file diff --git a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Series/SeriesControllerService.cs b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Series/SeriesControllerService.cs index 5117aec..79527ad 100644 --- a/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Series/SeriesControllerService.cs +++ b/WatchIt.WebAPI/WatchIt.WebAPI.Services/WatchIt.WebAPI.Services.Controllers/WatchIt.WebAPI.Services.Controllers.Series/SeriesControllerService.cs @@ -34,6 +34,8 @@ public class SeriesControllerService : ISeriesControllerService #region PUBLIC METHODS + #region Main + public async Task GetAllSeries(SeriesQueryParameters query) { IEnumerable data = await _database.MediaSeries.Select(x => new SeriesResponse(x)).ToListAsync(); @@ -132,4 +134,29 @@ public class SeriesControllerService : ISeriesControllerService } #endregion + + #region View count + + public async Task GetSeriesViewRank(int first, int days) + { + if (first < 1 || days < 1) + { + return RequestResult.BadRequest(); + } + + DateOnly startDate = DateOnly.FromDateTime(DateTime.Now).AddDays(-days); + IEnumerable rawData = await _database.MediaSeries.OrderByDescending(x => x.Media.ViewCountsMedia.Where(y => y.Date >= startDate) + .Sum(y => y.ViewCount)) + .ThenBy(x => x.Id) + .Take(first) + .ToListAsync(); + + IEnumerable data = rawData.Select(x => new SeriesResponse(x)); + + return RequestResult.Ok(data); + } + + #endregion + + #endregion } \ No newline at end of file