117 lines
3.1 KiB
C#
117 lines
3.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
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("list")]
|
|
public async Task<IActionResult> GetRequestsAsync()
|
|
{
|
|
var ret = await ct.TrialRequest.AsNoTracking().OrderByDescending(z => z.DtRequested).ToListAsync();
|
|
return Ok(ret);
|
|
}
|
|
|
|
|
|
//case 3233 GET: api/Trial/5
|
|
[HttpGet("{id}")]
|
|
public async Task<IActionResult> GetTrial([FromRoute] long id)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
var ret = await ct.TrialRequest.AsNoTracking().SingleOrDefaultAsync(m => m.Id == id);
|
|
if (ret == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
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.TrialRequest.SingleOrDefaultAsync(m => m.Id == id);
|
|
if (rec == null)
|
|
return NotFound();
|
|
ct.TrialRequest.Remove(rec);
|
|
await ct.SaveChangesAsync();
|
|
return Ok(rec);
|
|
}
|
|
|
|
|
|
// PUT: api/TrialRequest/5
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutTrialRequest([FromRoute] long id, [FromBody] TrialRequest TrialRequest)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
if (id != TrialRequest.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
ct.Entry(TrialRequest).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await ct.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!await TrialRequestExistsAsync(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<bool> TrialRequestExistsAsync(long id)
|
|
{
|
|
return await ct.TrialRequest.AnyAsync(e => e.Id == id);
|
|
}
|
|
|
|
|
|
//------------------------------------------------------
|
|
|
|
}//eoc
|
|
}//eons |