Files
rockfish/Controllers/TrialController.cs
2020-06-09 23:31:00 +00:00

197 lines
5.3 KiB
C#

using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using rockfishCore.Models;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace rockfishCore.Controllers
{
//Authentication controller
[Produces("application/json")]
[Route("api/trial")]
[Authorize]
public class TrialController : Controller
{
private readonly rockfishContext ct;
private readonly IConfiguration _configuration;
public TrialController(rockfishContext context, IConfiguration configuration)//these two are injected, see startup.cs
{
ct = context;
_configuration = configuration;
}
//Fetch list of trial requests
[HttpGet("requests")]
public ActionResult GetRequests()
{
var ret=ct.trialre
}
///////////////////////////////////////////////////////////
// STORED Trial KEY CRUD ROUTES
//
//case 3233 Get api/Trial/list a list of generated Trials
[HttpGet("list")]
public IEnumerable<dtoTrialListItem> GetList()
{
var res = from c in ct.Trial.OrderByDescending(c => c.DtCreated)
select new dtoTrialListItem
{
id = c.Id,
created = c.DtCreated,
regto = c.RegTo,
fetched = c.Fetched,
trial = (c.CustomerId==0)
};
return res.ToList();
}
//case 3233 GET: api/Trial/5
[HttpGet("{id}")]
public async Task<IActionResult> GetTrial([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var l = await ct.Trial.SingleOrDefaultAsync(m => m.Id == id);
if (l == null)
{
return NotFound();
}
string customerName = "<TRIAL>";
if (l.CustomerId != 0)
{
if (ct.Customer.Any(e => e.Id == l.CustomerId))
{
var cust = await ct.Customer
.Select(r => new { r.Id, r.Name })
.Where(r => r.Id == l.CustomerId)
.FirstAsync();
customerName=cust.Name;
}
else
{
customerName = "< Customer " + l.CustomerId.ToString() + " not found (deleted?) >";
}
}
var ret = new
{
regTo = l.RegTo,
customerName = customerName,
dtcreated = l.DtCreated,
email = l.Email,
code = l.Code,
fetched = l.Fetched,
dtfetched = l.DtFetched,
fetchFrom = l.FetchFrom,
key = l.Key
};
return Ok(ret);
}
// DELETE: api/Trial/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteTrial([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var rec = await ct.Trial.SingleOrDefaultAsync(m => m.Id == id);
if (rec == null)
{
return NotFound();
}
ct.Trial.Remove(rec);
await ct.SaveChangesAsync();
return Ok(rec);
}
// PUT: api/Trial/5/true
//Update a Trial and set it's fetched property only
//used by client to make a Trial fetchable or not ad-hoc
[HttpPut("fetched/{id}/{isFetched}")]
public async Task<IActionResult> PutTrialFetched([FromRoute] long id, [FromRoute] bool isFetched)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var rec = await ct.Trial.SingleOrDefaultAsync(m => m.Id == id);
if (rec == null)
{
return NotFound();
}
rec.Fetched = isFetched;
ct.Entry(rec).State = EntityState.Modified;
try
{
await ct.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TrialExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
private bool TrialExists(long id)
{
return ct.Trial.Any(e => e.Id == id);
}
//===================== UTILITY =============
private string GetRFAuthorizedUserStamp()
{
foreach (Claim c in User.Claims)
{
if (c.Type == "id")
{
return "RFID" + c.Value;
}
}
return "RFID unknown";
}
//------------------------------------------------------
}//eoc
}//eons