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
{
///
/// approved edits daily
///
///
///
///
[HttpGet("poem/corrections/daily")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedEditsGroupedByDateAsync([FromQuery] PagingParameterModel paging, Guid? userId = null)
{
var cacheKey = $"poem/corrections/daily/{DateTime.Now.Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByDateViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedEditsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// approved edits grouped by user
///
///
///
///
///
[HttpGet("poem/corrections/by/user")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedEditsGroupedByUserAsync([FromQuery] PagingParameterModel paging,DateTime? day, Guid? userId)
{
var cacheKey = $"poem/corrections/by/user/{(day ?? DateTime.Now).Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByUserViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedEditsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// 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);
}
///
/// approved section edits daily
///
///
///
///
[HttpGet("section/corrections/daily")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedSectionEditsGroupedByDateAsync([FromQuery] PagingParameterModel paging, Guid? userId = null)
{
var cacheKey = $"section/corrections/daily/{DateTime.Now.Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByDateViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedSectionEditsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// approved section edits grouped by user
///
///
///
///
///
[HttpGet("section/corrections/by/user")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedSectionEditsGroupedByUserAsync([FromQuery] PagingParameterModel paging, DateTime? day, Guid? userId)
{
var cacheKey = $"section/corrections/by/user/{(day ?? DateTime.Now).Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByUserViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedSectionEditsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// 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);
}
///
/// approved cat edits daily
///
///
///
///
[HttpGet("cat/corrections/daily")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedCatEditsGroupedByDateAsync([FromQuery] PagingParameterModel paging, Guid? userId = null)
{
var cacheKey = $"cat/corrections/daily/{DateTime.Now.Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByDateViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedCatEditsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// approved cat edits grouped by user
///
///
///
///
///
[HttpGet("cat/corrections/by/user")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedCatEditsGroupedByUserAsync([FromQuery] PagingParameterModel paging, DateTime? day, Guid? userId)
{
var cacheKey = $"cat/corrections/by/user/{(day ?? DateTime.Now).Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByUserViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedCatEditsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// 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);
}
///
/// approved related songs daily
///
///
///
///
[HttpGet("suggestedsongs/daily")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedRelatedSongsGroupedByDateAsync([FromQuery] PagingParameterModel paging, Guid? userId = null)
{
var cacheKey = $"suggestedsongs/daily/{DateTime.Now.Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByDateViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedRelatedSongsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// approved related songs grouped by user
///
///
///
///
///
[HttpGet("suggestedsongs/by/user")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedRelatedSongsGroupedByUserAsync([FromQuery] PagingParameterModel paging, DateTime? day, Guid? userId)
{
var cacheKey = $"suggestedsongs/by/user/{(day ?? DateTime.Now).Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByUserViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedRelatedSongsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// 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);
}
///
/// approved quoted poems daily
///
///
///
///
[HttpGet("quoteds/daily")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedQuotedPoemsGroupedByDateAsync([FromQuery] PagingParameterModel paging, Guid? userId = null)
{
var cacheKey = $"quoteds/daily/{DateTime.Now.Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByDateViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedQuotedPoemsGroupedByDateAsync(paging, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// approved quoted poems grouped by user
///
///
///
///
///
[HttpGet("quoteds/by/user")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(IEnumerable))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task GetApprovedQuotedPoemsGroupedByUserAsync([FromQuery] PagingParameterModel paging, DateTime? day, Guid? userId)
{
var cacheKey = $"quoteds/by/user/{(day ?? DateTime.Now).Date}/{paging.PageSize}/{paging.PageNumber}/{userId ?? Guid.Empty}";
if (!_memoryCache.TryGetValue(cacheKey, out (PaginationMetadata PagingMeta, GroupedByUserViewModel[] Tracks) pagedResult))
{
var res = await _service.GetApprovedQuotedPoemsGroupedByUserAsync(paging, day, userId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
pagedResult = res.Result;
}
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(pagedResult.PagingMeta));
return Ok(pagedResult.Tracks);
}
///
/// 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;
}
}
}