using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json.Linq;
using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
namespace AyaNova.Api.Controllers
{
///
/// Log files controller
///
[ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Authorize]
public class EventLogController : Controller
{
private readonly AyContext ct;
private readonly ILogger log;
private readonly ApiServerState serverState;
///
/// ctor
///
///
///
///
public EventLogController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
//TODO: code the log makers in EventLogProcessor
//
///
/// Get events as text document for object specified
///
/// Required roles:
/// Read rights to object type specified
///
///
/// Event log for object
[HttpGet("ObjectLog")]
public async Task GetObjectLog([FromQuery] EventLogOptions opt)
{
if (serverState.IsClosed)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (!Authorized.IsAuthorizedToRead(HttpContext.Items, opt.AyType))
{
return StatusCode(401, new ApiNotAuthorizedResponse());
}
var result = await EventLogProcessor.GetLogForObject(opt.AyId, UserLocaleIdFromContext.LocaleId(HttpContext.Items), ct);
//Log
EventLogProcessor.AddEntry(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.Metrics, AyaEvent.Retrieved), ct);
return Content(result);
}
///
/// Get events for a user as text document for object specified
///
/// Required roles:
/// Read rights to User object or UserId specified must be requestor Id
///
///
/// Event log for user
[HttpGet("UserLog")]
public async Task GetUserLog([FromQuery] EventLogOptions opt)
{
if (serverState.IsClosed)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, null, serverState.Reason));
}
if (opt.AyType != AyaType.User)
{
//return bad request
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, "AyType", "User type required"));
}
long UserId = UserIdFromContext.Id(HttpContext.Items);
//If not authorized to read a user and also not the current user asking for their own log then NO LOG FOR YOU!
if (!Authorized.IsAuthorizedToRead(HttpContext.Items, AyaType.User) && opt.AyId != UserId)
{
return StatusCode(401, new ApiNotAuthorizedResponse());
}
var result = await EventLogProcessor.GetLogForUser(opt.AyId, UserLocaleIdFromContext.LocaleId(HttpContext.Items), ct);
//Log
EventLogProcessor.AddEntry(new Event(UserId, 0, AyaType.Metrics, AyaEvent.Retrieved), ct);
return Content(result);
}
//------------
public sealed class EventLogOptions
{
[FromQuery]
public AyaType AyType { get; set; }
[FromQuery]
public long AyId { get; set; }
[FromQuery]
public DateTime StartDate { get; set; }
[FromQuery]
public DateTime EndDate { get; set; }
}
}//eoc
}//eons