using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using RMuseum.Models.Generic.ViewModels;
using RMuseum.Services;
using RSecurityBackend.Models.Generic;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RMuseum.Controllers
{
[Produces("application/json")]
[Route("api/contributions")]
public class ContributionStatsController : Controller
{
///
/// daily stats
///
///
/// poem/corrections
/// section/corrections
/// cat/corrections
/// cat/corrections
/// suggestedsongs
/// quoteds
///
///
///
///
[HttpGet("{dataType}/daily")]
[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)
{
var cacheKey = $"{dataType}/daily/{DateTime.Now.Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByDateViewModel[] Tracks) pagedResult))
{
switch (dataType)
{
case "poem/corrections":
{
var res = await _service.GetApprovedEditsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
case "section/corrections":
{
var res = await _service.GetApprovedSectionEditsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
case "cat/corrections":
{
var res = await _service.GetApprovedCatEditsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
case "suggestedsongs":
{
var res = await _service.GetApprovedRelatedSongsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
case "quoteds":
{
var res = await _service.GetApprovedQuotedPoemsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
default:
return BadRequest("invalid value for the paramater: dataType");
}
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// approved edits grouped by user
///
///
///
///
///
///
[HttpGet("{dataType}/by/user")]
[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)
{
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))
{
switch(dataType)
{
case "poem/corrections":
{
var res = await _service.GetApprovedEditsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
case "section/corrections":
{
var res = await _service.GetApprovedSectionEditsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
case "cat/corrections":
{
var res = await _service.GetApprovedCatEditsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
case "suggestedsongs":
{
var res = await _service.GetApprovedRelatedSongsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
case "quoteds":
{
var res = await _service.GetApprovedQuotedPoemsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
break;
default:
return BadRequest("invalid value for the paramater: dataType");
}
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// summed up stats of approved poem corrections
///
///
[HttpGet("poem/corrections")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(SummedUpViewModel))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprrovedEditsSummedUpStatsAsync()
{
if (!_memoryCache.TryGetValue($"poem/corrections/{DateTime.Now.Date}", out SummedUpViewModel result))
{
var res = await _service.GetApprrovedEditsSummedUpStatsAsync();
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
result = res.Result;
}
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
///
protected readonly IContributionStatsService _service;
///
/// IMemoryCache
///
protected readonly IMemoryCache _memoryCache;
///
/// constructor
///
///
///
public ContributionStatsController(IContributionStatsService service, IMemoryCache memoryCache)
{
_service = service;
_memoryCache = memoryCache;
}
}
}