From 2554128f4d501b1c8f2544e202ddfce983df06a2 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 27 Mar 2025 15:35:46 +0330 Subject: [PATCH] #478 refactoring --- .../ContributionStatsController.cs | 176 +++++++----------- RMuseum/RMuseum.xml | 51 +++-- 2 files changed, 90 insertions(+), 137 deletions(-) diff --git a/RMuseum/Controllers/ContributionStatsController.cs b/RMuseum/Controllers/ContributionStatsController.cs index ab406eb4..ec060f88 100644 --- a/RMuseum/Controllers/ContributionStatsController.cs +++ b/RMuseum/Controllers/ContributionStatsController.cs @@ -36,7 +36,7 @@ namespace RMuseum.Controllers [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task GetApprovedEditsGroupedByDateAsync(string dataType, [FromQuery] PagingParameterModel paging, Guid? userId = null) + public async Task GetDataGroupedByDateAsync(string dataType, [FromQuery] PagingParameterModel paging, Guid? userId = null) { var cacheKey = $"{dataType}/daily/{DateTime.Now.Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}"; if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByDateViewModel[] Tracks) pagedResult)) @@ -94,9 +94,16 @@ namespace RMuseum.Controllers } /// - /// approved edits grouped by user + /// datatype grouped by user /// - /// + /// + /// poem/corrections + /// section/corrections + /// cat/corrections + /// cat/corrections + /// suggestedsongs + /// quoteds + /// /// /// /// @@ -105,7 +112,7 @@ namespace RMuseum.Controllers [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task GetApprovedEditsGroupedByUserAsync(string dataType, [FromQuery] PagingParameterModel paging, DateTime? day, Guid? userId) + public async Task GetDataGroupedByUserAsync(string dataType, [FromQuery] PagingParameterModel paging, DateTime? day, Guid? userId) { var cacheKey = $"{dataType}/by/user/{(day ?? DateTime.Now).Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}"; if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByUserViewModel[] Tracks) pagedResult)) @@ -164,120 +171,75 @@ namespace RMuseum.Controllers } /// - /// summed up stats of approved poem corrections + /// summed up stats of data /// + /// + /// poem/corrections + /// section/corrections + /// cat/corrections + /// cat/corrections + /// suggestedsongs + /// quoteds + /// /// - [HttpGet("poem/corrections")] + [HttpGet("{dataType}")] [AllowAnonymous] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(SummedUpViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task GetApprrovedEditsSummedUpStatsAsync() + public async Task GetDataSummedUpStatsAsync(string dataType) { - if (!_memoryCache.TryGetValue($"poem/corrections/{DateTime.Now.Date}", out SummedUpViewModel result)) + if (!_memoryCache.TryGetValue($"{dataType}/{DateTime.Now.Date}", out SummedUpViewModel result)) { - var res = await _service.GetApprrovedEditsSummedUpStatsAsync(); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - result = res.Result; + switch(dataType) + { + case "poem/corrections": + { + var res = await _service.GetApprrovedEditsSummedUpStatsAsync(); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + result = res.Result; + } + break; + case "section/corrections": + { + var res = await _service.GetApprrovedSectionEditsSummedUpStatsAsync(); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + result = res.Result; + } + break; + case "cat/corrections": + { + var res = await _service.GetApprrovedCatEditsSummedUpStatsAsync(); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + result = res.Result; + } + break; + case "suggestedsongs": + { + var res = await _service.GetApprovedRelatedSongsSummedUpStatsAsync(); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + result = res.Result; + } + break; + case "quoteds": + { + var res = await _service.GetApprovedQuotedPoemsSummedUpStatsAsync(); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + result = res.Result; + } + break; + default: + return BadRequest("invalid value for the paramater: dataType"); + } + } return Ok(result); } - - - - /// - /// summed up stats of approved section corrections - /// - /// - [HttpGet("section/corrections")] - [AllowAnonymous] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(SummedUpViewModel))] - [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task GetApprrovedSedtionEditsSummedUpStatsAsync() - { - if (!_memoryCache.TryGetValue($"section/corrections/{DateTime.Now.Date}", out SummedUpViewModel result)) - { - var res = await _service.GetApprrovedSectionEditsSummedUpStatsAsync(); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - result = res.Result; - } - return Ok(result); - } - - - - - - - /// - /// summed up stats of approved cat corrections - /// - /// - [HttpGet("cat/corrections")] - [AllowAnonymous] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(SummedUpViewModel))] - [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task GetApprrovedCatEditsSummedUpStatsAsync() - { - if (!_memoryCache.TryGetValue($"cat/corrections/{DateTime.Now.Date}", out SummedUpViewModel result)) - { - var res = await _service.GetApprrovedCatEditsSummedUpStatsAsync(); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - result = res.Result; - } - return Ok(result); - } - - - /// - /// summed up stats of approved related songs - /// - /// - [HttpGet("suggestedsongs")] - [AllowAnonymous] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(SummedUpViewModel))] - [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task GetApprovedRelatedSongsSummedUpStatsAsync() - { - if (!_memoryCache.TryGetValue($"suggestedsongs/{DateTime.Now.Date}", out SummedUpViewModel result)) - { - var res = await _service.GetApprovedRelatedSongsSummedUpStatsAsync(); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - result = res.Result; - } - return Ok(result); - } - - - - - - /// - /// summed up stats of quoted poems - /// - /// - [HttpGet("quoteds")] - [AllowAnonymous] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(SummedUpViewModel))] - [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] - public async Task GetApprovedQuotedPoemsSummedUpStatsAsync() - { - if (!_memoryCache.TryGetValue($"quoteds/{DateTime.Now.Date}", out SummedUpViewModel result)) - { - var res = await _service.GetApprovedQuotedPoemsSummedUpStatsAsync(); - if (!string.IsNullOrEmpty(res.ExceptionString)) - return BadRequest(res.ExceptionString); - result = res.Result; - } - return Ok(result); - } - - - /// /// service /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 25149e16..8ae93715 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -762,7 +762,7 @@ - + daily stats @@ -778,44 +778,35 @@ - + - approved edits grouped by user + datatype grouped by user - + + poem/corrections + section/corrections + cat/corrections + cat/corrections + suggestedsongs + quoteds + - + - summed up stats of approved poem corrections - - - - - - summed up stats of approved section corrections - - - - - - summed up stats of approved cat corrections - - - - - - summed up stats of approved related songs - - - - - - summed up stats of quoted poems + summed up stats of data + + poem/corrections + section/corrections + cat/corrections + cat/corrections + suggestedsongs + quoteds +