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
///
[ApiVersion("8.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Produces("application/json")]
[Authorize]
public class TrialController : Controller
{
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
///
/// Valid values are "Small", "Medium", "Large"
///
[HttpPost("seed/{size}")]
public ActionResult SeedTrialDatabase([FromRoute] string size)
{
if (!serverState.IsOpen)
{
return StatusCode(503, new ApiErrorResponse(ApiErrorCode.API_CLOSED, 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.SeedLevel seedLevel = Seeder.SeedLevel.SmallOneManShopTrialDataSet;
switch (size.ToLowerInvariant())
{
case "small":
seedLevel = Seeder.SeedLevel.SmallOneManShopTrialDataSet;
break;
case "medium":
seedLevel = Seeder.SeedLevel.MediumLocalServiceCompanyTrialDataSet;
break;
case "large":
seedLevel = Seeder.SeedLevel.LargeCorporateMultiRegionalTrialDataSet;
break;
default:
return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, "size", "Valid values are \"small\", \"medium\", \"large\""));
}
//Create the job here
JObject o = JObject.FromObject(new
{
seedLevel = seedLevel
});
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();
JobsBiz.AddJob(j, ct);
//Log
//EventLogProcessor.AddEntry(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.TrialSeeder, AyaEvent.Created,size), ct);
return Accepted(new { JobId = j.GId });//202 accepted
}
//------------
}//eoc
}//eons