Files
rockfish/Controllers/RvfController.cs
2020-06-11 19:52:47 +00:00

121 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using rockfishCore.Models;
using rockfishCore.Util;
namespace rockfishCore.Controllers
{
[Produces("application/json")]
[Route("rvf")]
public class RvfController : Controller //RAVEN License fetch route
{
private readonly rockfishContext ct;
public RvfController(rockfishContext context)
{
ct = context;
}
[HttpGet("{dbid}")]
public async Task<IActionResult> Get([FromRoute] Guid dbid)
{
//this is called after a successful check says there is a key below so it's not hammered (in theory)
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 it a trial request
var TrialRequest = await ct.TrialRequest.Where(z => z.DbId == dbid && z.DtFetched == null).FirstOrDefaultAsync();
LicenseKey = TrialRequest.Key;
TrialRequest.DtFetched = DateUtil.NowAsEpoch();
await ct.SaveChangesAsync();
}
if (LicenseKey == null)
{
return NotFound();
}
return Ok(new
{
data = new
{
key = LicenseKey
}
});
//
// //This is to simulate the scenarios where there is no license to return
// //either due to no current account / canceled or simply no license exists
// //Changes here must be reflected in RAVEN Util.License.Fetch block
// bool bTestStatusOtherThanOk = false;
// if (bTestStatusOtherThanOk)
// {
// return Json(new {Status="NONE", Reason="No license"});
// //return Json(new {Status="Canceled", Reason="Non payment"});
// }
// else
// {
// return Ok(RavenKeyFactory.GetRavenTestKey(dbid));
// }
}
[HttpGet("hello/{dbid}")]
public ActionResult Hello([FromRoute] Guid dbid)
{
//check to see if there is anything of note for this server like a new key or a message etc
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//return a json object with results if anything to report
//This is to simulate the scenarios where there is no license to return
//either due to no current account / canceled or simply no license exists
//Changes here must be reflected in RAVEN Util.License.Fetch block
bool bTestStatusOtherThanOk = false;
if (bTestStatusOtherThanOk)
{
return Json(new { Status = "NONE", Reason = "No license" });
//return Json(new {Status="Canceled", Reason="Non payment"});
}
else
{
return Ok(RavenKeyFactory.GetRavenTestKey(dbid));
}
}
private bool LicenseExists(long id)
{
return ct.License.Any(e => e.Id == id);
}
}
}