Files
rockfish/Controllers/RvfController.cs
2020-06-23 18:20:51 +00:00

72 lines
2.1 KiB
C#

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using rockfishCore.Models;
using rockfishCore.Util;
namespace rockfishCore.Controllers
{
//### CUSTOMER ROUTE CALLED FROM RAVEN NO AUTH ####
[Produces("application/json")]
[Route("rvf")]
public class RvfController : Controller //RAVEN License fetch route
{
private readonly rockfishContext ct;
public RvfController(rockfishContext context)
{
ct = context;
}
//### CUSTOMER ROUTE CALLED FROM RAVEN ####
[HttpGet("{dbid}")]
public async Task<IActionResult> Get([FromRoute] string dbid)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
string LicenseKey = null;
//check for a key for this dbid, first check licensed then check trial
var PurchasedLicense = await ct.License.Where(z => z.DbId == dbid && z.Fetched == false).FirstOrDefaultAsync();
if (PurchasedLicense != null)
{
LicenseKey = PurchasedLicense.Key;
PurchasedLicense.Fetched = true;
PurchasedLicense.DtFetched = DateUtil.NowAsEpoch();
await ct.SaveChangesAsync();
}
else
{
//is there an Approved UnFetched trial request for this DB ID?
var req = await ct.TrialRequest.Where(z => z.DbId == dbid && z.DtFetched == null && z.Status == TrialRequest.TrialRequestStatus.Approved).FirstOrDefaultAsync();
if (req == null)
{
return NotFound();
}
LicenseKey = req.Key;
req.DtFetched = DateUtil.NowAsEpoch();
await ct.SaveChangesAsync();
}
if (LicenseKey == null)
{
return NotFound();
}
return Ok(new
{
data = new
{
key = LicenseKey
}
});
}
}
}