80 lines
2.4 KiB
C#
80 lines
2.4 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using rockfishCore.Models;
|
|
using rockfishCore.Util;
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
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;
|
|
}
|
|
|
|
public class dtoFetchRequest
|
|
{
|
|
[Required]
|
|
public string DbId { get; set; }
|
|
}
|
|
|
|
//### CUSTOMER ROUTE CALLED FROM RAVEN ####
|
|
[HttpPost]
|
|
public async Task<IActionResult> PostLicenseFetchRequest([FromBody] dtoFetchRequest fetchRequest)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
string FetchRequestDbId=fetchRequest.DbId;
|
|
|
|
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 == FetchRequestDbId && 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 == FetchRequestDbId && 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
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
}
|
|
} |