CernerUserSessionController.cs 2.87 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ADAM.CernerFHIR.Helper;
using AIAHTML5.API.Models;
using eLearningPlayer.model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace AIAHTML5.API.Controllers
{
    public class CernerUserSessionController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public HttpResponseMessage Get(int id)
        {
            var db = new DBModel();
            var result = db.GetAIAImageIdWithJsonCerner(id);

            HttpResponseMessage response = null;
            response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(result)) };
            return response;
        }

        // POST api/<controller>
        public void Post([FromBody] string CernerSessionValues)
        {
            var cernerUserSessionInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<CernerUserSessionInfo>(CernerSessionValues);
            var db = new DBModel();
            var result = db.SaveCernerFhirSessionInfo(cernerUserSessionInfo);

            HttpResponseMessage response = null;
            response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = null };
            //return response;
        }
        
        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }

        [HttpGet]
        [Route("UpdateCernerToken")]
        public HttpResponseMessage UpdateCernerToken(string sessionId)
        {
            //Update cerner user session values.
            var cernerSessionId = sessionId;
            var db = new DBModel();
            var CernerUserSession = db.CheckCernerUserSession(cernerSessionId).SingleOrDefault();
            var NewLoginResult = AsyncToSyncTaskHelper.AsyncHelpers.RunSync(() => ADAM.CernerFHIR.Client.ResourceProcessor.GetCernerResourceRefreshToken(CernerUserSession.LoginResult));
            NewLoginResult.EndPointURL = NewLoginResult.EndPointURL ?? CernerUserSession.LoginResult.EndPointURL;
            var UpdatedCernerUserSession = new CernerUserSessionInfo()
            {
                CernerCode = CernerUserSession.CernerCode,
                FhirUserSessionId = cernerSessionId,
                CernerInfoJWT = CernerUserSession.CernerInfoJWT,
                LoginResult = NewLoginResult
            };
            db.UpdateCernerFhirSessionInfo(UpdatedCernerUserSession);

            HttpResponseMessage response = null;
            return response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = null };
        }
    }
}