This commit is contained in:
124
server/AyaNova/Controllers/TrialController.cs
Normal file
124
server/AyaNova/Controllers/TrialController.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
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
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
///Test controller class used during development
|
||||
/// </summary>
|
||||
[ApiVersion("8.0")]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
public class TrialController : Controller
|
||||
{
|
||||
private readonly AyContext ct;
|
||||
private readonly ILogger<WidgetController> log;
|
||||
private readonly ApiServerState serverState;
|
||||
|
||||
/// <summary>
|
||||
/// ctor
|
||||
/// </summary>
|
||||
/// <param name="dbcontext"></param>
|
||||
/// <param name="logger"></param>
|
||||
/// <param name="apiServerState"></param>
|
||||
public TrialController(AyContext dbcontext, ILogger<WidgetController> logger, ApiServerState apiServerState)
|
||||
{
|
||||
ct = dbcontext;
|
||||
log = logger;
|
||||
serverState = apiServerState;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
/// <param name="size">Valid values are "Small", "Medium", "Large"</param>
|
||||
/// <returns></returns>
|
||||
[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
|
||||
// channel = new
|
||||
// {
|
||||
// title = "Star Wars",
|
||||
// link = "http://www.starwars.com",
|
||||
// description = "Star Wars blog.",
|
||||
// item =
|
||||
// from p in posts
|
||||
// orderby p.Title
|
||||
// select new
|
||||
// {
|
||||
// title = p.Title,
|
||||
// description = p.Description,
|
||||
// link = p.Link,
|
||||
// category = p.Categories
|
||||
// }
|
||||
// }
|
||||
});
|
||||
|
||||
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);
|
||||
return Accepted(new { JobId = j.GId });//202 accepted
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//------------
|
||||
|
||||
|
||||
}//eoc
|
||||
}//eons
|
||||
Reference in New Issue
Block a user