using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using AyaNova.Models;
using AyaNova.Util;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using Newtonsoft.Json.Linq;
namespace AyaNova.Api.Controllers
{
///
///Test controller class used during development
///
[ApiController]
[ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/trial")]
[Produces("application/json")]
[Authorize]
public class TrialController : ControllerBase
{
private readonly AyContext ct;
private readonly ILogger log;
private readonly ApiServerState serverState;
///
/// ctor
///
///
///
///
public TrialController(AyContext dbcontext, ILogger logger, ApiServerState apiServerState)
{
ct = dbcontext;
log = logger;
serverState = apiServerState;
}
///
/// Seed a trial database with sample data.
///
/// You can control the size and scope of the seeded data with the passed in size value
/// "Small" - a small one man shop dataset
/// "Medium" - Local service company with multiple employees and departments dataset
/// "Large" - Large corporate multi regional dataset
/// "Huge" - Used for automated testing and development, if you choose this it will take a very long time (15 minutes to overnight)
///
/// Valid values are "Small", "Medium", "Large", "Huge"
/// Value in hours of local time zone offset from UTC / GMT. This ensures that data is generated relative to the desired time zone
/// End to end testing mode for automated testing; false is default and faster
/// Job Id
[HttpPost("seed/{size}/{timeZoneOffset}/{e2e}")]
public async Task SeedTrialDatabase([FromRoute] string size, [FromRoute] decimal timeZoneOffset, [FromRoute] bool e2e = false)
{
if (serverState.IsClosed)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
if (!ModelState.IsValid)
{
return BadRequest(new ApiErrorResponse(ModelState));
}
if (!AyaNova.Core.License.ActiveKey.TrialLicense)
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, "Current license is not a trial license key. Only a trial can be seeded."));
}
Seeder.Level.SeedLevel seedLevel = Seeder.Level.StringToSeedLevel(size);
if (seedLevel == Seeder.Level.SeedLevel.NotValid)
return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, "size", "Valid values are \"small\", \"medium\", \"large\", \"huge\""));
//Create the job here
JObject o = JObject.FromObject(new
{
seedLevel = seedLevel,
timeZoneOffset = timeZoneOffset,
e2e = e2e
});
OpsJob j = new OpsJob();
j.Name = $"Seed test data (size={size})";
j.JobType = JobType.SeedTestData;
j.Exclusive = true;//don't run other jobs, this will erase the db
j.JobInfo = o.ToString();
await JobsBiz.AddJobAsync(j);
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.TrialSeeder, AyaEvent.Created, size), ct);
return Accepted(new { JobId = j.GId });//202 accepted
}
//------------
}//eoc
}//eons