diff --git a/Controllers/TrialController.cs b/Controllers/TrialController.cs new file mode 100644 index 0000000..c2e0b08 --- /dev/null +++ b/Controllers/TrialController.cs @@ -0,0 +1,197 @@ +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 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 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 = ""; + + 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 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 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 \ No newline at end of file diff --git a/Models/TrialRequest.cs b/Models/TrialRequest.cs new file mode 100644 index 0000000..31986bf --- /dev/null +++ b/Models/TrialRequest.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + + +namespace rockfishCore.Models +{ + /* + exec("CREATE TABLE trialrequest (" + + "id INTEGER PRIMARY KEY, dbid text not null, companyname text not null, contactname text not null, notes text, email text not null, " + + "emailvalidated boolean default 0 NOT NULL CHECK (emailvalidated IN (0,1)), dtrequested integer, " + + "dtprocessed integer, status integer default 0 not null, rejectreason text, key text, dtfetched integer" + + ")"); + */ + public partial class TrialRequest + { + public long Id { get; set; } + public string DbId { get; set; } + public string CompanyName { get; set; } + public string ContactName { get; set; } + public string Notes { get; set; } + public string Email { get; set; } + public bool EmailValidated { get; set; } + public long? DtRequested { get; set; } + public long? DtProcessed { get; set; } + public int Status { get; set; } + public string RejectReason { get; set; } + public string Key { get; set; } + public long? DtFetched { get; set; } + } +} diff --git a/Models/rockfishContext.cs b/Models/rockfishContext.cs index 648ecb0..05fb365 100644 --- a/Models/rockfishContext.cs +++ b/Models/rockfishContext.cs @@ -30,7 +30,8 @@ namespace rockfishCore.Models //schema 10 case 3233 public virtual DbSet License { get; set; } - +//raven + public virtual DbSet TrialRequest { get; set; } //Note: had to add this constructor to work with the code in startup.cs that gets the connection string from the appsettings.json file //and commented out the above on configuring diff --git a/wwwroot/js/app.shell.js b/wwwroot/js/app.shell.js index 3ed427d..484c039 100644 --- a/wwwroot/js/app.shell.js +++ b/wwwroot/js/app.shell.js @@ -238,6 +238,8 @@ app.shell = (function () { page('/rfcaseEdit/:id', rfcaseEdit); page('/rfsettings', rfsettings); page('/ops', ops); + page('/trials', trials); + page('/trialEdit/:id', trialEdit); page('*', notFound); page({ hashbang: true @@ -549,6 +551,25 @@ app.shell = (function () { } + //case 3233 + var trials = function (ctx) { + app.nav.setSelectedMenuItem('trials'); + app.trials.configModule({ + context: ctx + }); + app.trials.initModule(); + } + + //case 3233 + var trialEdit = function (ctx) { + app.nav.setSelectedMenuItem('trials'); + app.trialEdit.configModule({ + context: ctx + }); + app.trialEdit.initModule(); + } + + var notFound = function (ctx) { app.fourohfour.configModule({ context: ctx diff --git a/wwwroot/js/app.trialEdit.js b/wwwroot/js/app.trialEdit.js new file mode 100644 index 0000000..f2d21a2 --- /dev/null +++ b/wwwroot/js/app.trialEdit.js @@ -0,0 +1,116 @@ +/*jslint browser : true, continue : true, + devel : true, indent : 2, maxerr : 50, + newcap : true, nomen : true, plusplus : true, + regexp : true, sloppy : true, vars : false, + white : true +*/ + +/*global $, app */ + +app.trialEdit = (function () { + 'use strict'; + //---------------- BEGIN MODULE SCOPE VARIABLES -------------- + var + stateMap = {}, + onSave, onDelete, + configModule, initModule; + //----------------- END MODULE SCOPE VARIABLES --------------- + + + //------------------- BEGIN EVENT HANDLERS ------------------- + + //ONSAVE + // + onSave = function (event) { + event.preventDefault(); + $.gevent.publish('app-clear-error'); + + var isFetched=$('#fetched').prop('checked') + + // var submitData = { isFetched: isFetched }; + app.api.putAction('trial/fetched/' + stateMap.id + "/" + isFetched, function (res) { + if (res.error) { + $.gevent.publish('app-show-error', res.msg); + } + }); + + return false; //prevent default? + }; + + //ONDELETE + // + onDelete = function (event) { + event.preventDefault(); + $.gevent.publish('app-clear-error'); + var r = confirm("Are you sure you want to delete this record?"); + if (r == true) { + app.api.remove('trial/' + stateMap.id, function (res) { + if (res.error) { + $.gevent.publish('app-show-error', res.msg); + } else { + page('#!/trials'); + return false; + } + }); + + } else { + return false; + } + return false; //prevent default? + }; + + + //-------------------- END EVENT HANDLERS -------------------- + + //------------------- BEGIN PUBLIC METHODS ------------------- + + + //CONFIGMODULE + // + configModule = function (context) { + stateMap.context = context.context; + if (stateMap.context.params.id) { + stateMap.id = stateMap.context.params.id; + } + }; + + //INITMODULE + // + initModule = function ($container) { + if (typeof $container === 'undefined') { + $container = $('#app-shell-main-content'); + } + + $container.html(Handlebars.templates['app.trialEdit']({})); + + document.title = 'trial '; + + //fetch existing record + app.api.get('trial/' + stateMap.id, function (res) { + if (res.error) { + $.gevent.publish('app-show-error', res.msg); + } else { + //fill out form + app.utilB.formData(res); + } + }); + //Context menu + app.nav.contextClear(); + app.nav.contextAddLink("trials/", "List", ""); + + + + // bind actions + $('#btn-save').bind('click', onSave); + $('#btn-delete').bind('click', onDelete); + }; + + + // RETURN PUBLIC METHODS + // + return { + configModule: configModule, + initModule: initModule + }; + //------------------- END PUBLIC METHODS --------------------- +}()); \ No newline at end of file diff --git a/wwwroot/js/app.trials.js b/wwwroot/js/app.trials.js new file mode 100644 index 0000000..80f6f8b --- /dev/null +++ b/wwwroot/js/app.trials.js @@ -0,0 +1,108 @@ +/*jslint browser : true, continue : true, + devel : true, indent : 2, maxerr : 50, + newcap : true, nomen : true, plusplus : true, + regexp : true, sloppy : true, vars : false, + white : true +*/ + +/*global $, app */ + +app.trials = (function() { + "use strict"; + //---------------- BEGIN MODULE SCOPE VARIABLES -------------- + var stateMap = {}, + configModule, + initModule; + //----------------- END MODULE SCOPE VARIABLES --------------- + + //------------------- BEGIN UTILITY METHODS ------------------ + + //-------------------- END UTILITY METHODS ------------------- + + //------------------- BEGIN EVENT HANDLERS ------------------- + + //-------------------- END EVENT HANDLERS -------------------- + + //------------------- BEGIN PUBLIC METHODS ------------------- + //CONFIGMODULE + // + configModule = function(context) { + stateMap.context = context.context; + if (stateMap.context.params.id) { + stateMap.id = stateMap.context.params.id; + } + }; + + //INITMODULE + // + initModule = function($container) { + if (typeof $container === "undefined") { + $container = $("#app-shell-main-content"); + } + $container.html(Handlebars.templates["app.trials"]({})); + + //case 3513 + document.title = "trials"; + + //=================== + //Get trials + app.api.get("trial/list", function(res) { + if (res.error) { + $.gevent.publish("app-show-error", res.msg); + } else { + var $appList = $("#rf-list"); + + $appList.append('"); + } + }); + //=========/trials============== + + app.nav.contextClear(); + }; + + //PUBLIC METHODS + // + return { + configModule: configModule, + initModule: initModule + }; + //------------------- END PUBLIC METHODS --------------------- +})(); diff --git a/wwwroot/js/templates/app.shell.handlebars b/wwwroot/js/templates/app.shell.handlebars index eea1f66..f24e565 100644 --- a/wwwroot/js/templates/app.shell.handlebars +++ b/wwwroot/js/templates/app.shell.handlebars @@ -1,9 +1,9 @@