Files
divan/RMuseum/Controllers/UserVisitsTrackingController.cs
T

116 lines
4.6 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RMuseum.Models.Ganjoor.ViewModels;
using RMuseum.Services;
using RSecurityBackend.Models.Generic;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace RMuseum.Controllers
{
/// <summary>
/// user visits tracking controller
/// </summary>
[Produces("application/json")]
[Route("api/tracking")]
public class UserVisitsTrackingController : Controller
{
/// <summary>
/// get user history
/// </summary>
/// <param name="paging"></param>
/// <returns></returns>
[HttpGet]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorUserBookmarkViewModel[]))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public async Task<IActionResult> GetUserHistoryAsync(PagingParameterModel paging)
{
var loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
var res = await _userVisitsTrackingService.GetUserHistoryAsync(paging, loggedOnUserId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
// Paging Header
HttpContext.Response.Headers.Append("paging-headers", JsonConvert.SerializeObject(res.Result.PagingMeta));
return Ok(res.Result.HistoryItems);
}
/// <summary>
/// add a user history track
/// </summary>
/// <param name="poemId"></param>
/// <returns>GanjoorUserPrePoemVisitViewModel(LastVisit, TotalVisits)</returns>
[HttpPost]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorUserPrePoemVisitViewModel))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task<IActionResult> AddAsync([FromBody] int poemId)
{
Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
var res = await _userVisitsTrackingService.AddAsync(loggedOnUserId, poemId);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// delete a user history track
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete("{id}")]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task<IActionResult> DeleteAsync(Guid id)
{
Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
var res = await _userVisitsTrackingService.DeleteAsync(loggedOnUserId, id);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// start or stop tracking user
/// </summary>
/// <param name="start"></param>
/// <returns></returns>
[HttpPut]
[Authorize]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.Unauthorized)]
public async Task<IActionResult> SwitchTrackingAsync([FromBody] bool start)
{
Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value);
var res = await _userVisitsTrackingService.SwitchTrackingAsync(loggedOnUserId, start);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
return Ok(res.Result);
}
/// <summary>
/// User Visits Tracking Service
/// </summary>
protected readonly IUserVisitsTrackingService _userVisitsTrackingService;
/// <summary>
/// constructor
/// </summary>
/// <param name="userVisitsTrackingService"></param>
public UserVisitsTrackingController(IUserVisitsTrackingService userVisitsTrackingService)
{
_userVisitsTrackingService = userVisitsTrackingService;
}
}
}