public async Task <MarsResponse> CreateOrUpdateAsync(UserModel model) { var stopwatch = new Stopwatch(); stopwatch.Start(); var item = await this.ModelSet.SingleOrDefaultAsync(c => c.UserId == model.UserId); if (item == null) { await this.ModelSet.AddAsync(model); item = model; } else { var id = item.Id; item.Copy(model); item.Id = id; } await this.SaveChangesAsync(); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " CreateOrUpdateAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(model, HttpStatusCode.Created)); }
public async Task <MarsResponse> UpdateStatus(AppointmentModel item) { var stopwatch = new Stopwatch(); stopwatch.Start(); var mod = await this.ModelSet.FirstOrDefaultAsync(c => c.Id == item.Id); if (mod != null) { if (item.Status != 0) { mod.Status = item.Status; } if (item.WorkflowState != 0) { mod.WorkflowState = item.WorkflowState; } await this.SaveChangesAsync(); } stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " UpdateStatus", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(mod, mod == null ? HttpStatusCode.NotFound : HttpStatusCode.OK)); }
public virtual async Task <MarsResponse> SearchAsync(TModel item) { var stopwatch = new Stopwatch(); stopwatch.Start(); IEnumerable <TModel> itemList = null; await Task.Run(() => { itemList = FilterSearch(item); MarsResponse resp = MarsResponse.GetResponse(itemList); if (itemList == null) { resp.Code = HttpStatusCode.NotAcceptable; } else if (itemList.Count() == 0) { resp.Code = HttpStatusCode.NotFound; } }); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " SearchAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(itemList)); }
public async Task <IActionResult> GetAllvailabilty() { var lstavilabity = await this.availabilitycontext.GetAllAsync(DateTime.Now.Date); var res = (from avilabity in lstavilabity join resource in resourceContext.ModelSet on avilabity.ResourceId equals resource.Id select new { Id = resource.Id, ResourceId = resource.Id, Name = resource.ResourceName, Date = avilabity.Date, StartTime = avilabity.StartTime, EndTime = avilabity.EndTime }).ToArray(); HttpStatusCode code = HttpStatusCode.OK; if (res == null) { code = HttpStatusCode.NotFound; } var resMars = Task.Run(() => MarsResponse.GetResponse(res, code)); return(await this.exceptionHandler.SendResponse(this, resMars)); }
public async Task <MarsResponse> SignIn(string userName, string password) { //return await Task.Run(() => //{ var resp = new MarsResponse(); resp.Code = HttpStatusCode.ServiceUnavailable; try { var tokenConfig = configuration.GetSection("TokenScheme").GetSection("Token").Value; var tokenResp = await oauthToken.GetToken(userName, password); if (tokenResp == null || string.IsNullOrEmpty(tokenResp.access_token)) { resp.Code = HttpStatusCode.Unauthorized; resp.Error = new MarsException("Unautherized access"); } else { resp.Data = tokenResp; resp.Code = HttpStatusCode.OK; } } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } return(resp); //}); }
/// <summary> /// /// </summary> /// <param name="item"></param> /// <returns></returns> public async Task <MarsResponse> GetAllAsyncAsPerDateNResourceId(AvailabilityModel item) { var resourceList = await Task.Run(() => this.ModelSet.Where(m => m.ResourceId == item.ResourceId && DateTime.Compare(m.Date, item.Date) == 0).ToArray()); HttpStatusCode code = HttpStatusCode.OK; if (resourceList == null) { code = HttpStatusCode.NotFound; } return(MarsResponse.GetResponse(resourceList, code)); }
public async Task <MarsResponse> DeleteAsync(int id) { var stopwatch = new Stopwatch(); stopwatch.Start(); var bDelete = await DeleteModelAsync(id); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " DeleteAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(bDelete, bDelete ? HttpStatusCode.OK : HttpStatusCode.NotFound)); }
public async Task <IActionResult> SendResponse(ControllerBase controller, Task <MarsResponse> response) { try { var resp = await response; return(controller.StatusCode((int)resp.Code, resp)); } catch (Exception e) { this.log.LogError(e); return(controller.StatusCode(500, MarsResponse.GetError(e))); } }
public async Task <MarsResponse> UpdateAsync(int id, TModel item) { var stopwatch = new Stopwatch(); stopwatch.Start(); var mod = await UpdateModelAsync(id, item); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " UpdateAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(mod, mod == null ? HttpStatusCode.NotFound : HttpStatusCode.OK)); }
public async Task <MarsResponse> CreateAsync(TModel item) { var stopwatch = new Stopwatch(); stopwatch.Start(); await this.CreateModelAsync(item); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " CreateAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(item, HttpStatusCode.Created)); }
public async Task <MarsResponse> GetAppAsync(int id, string token) { var stopwatch = new Stopwatch(); stopwatch.Start(); var appt = await base.GetModelAsync(id); await AddOtherValues(appt, token); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " GetAppAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(appt)); }
public async Task <MarsResponse> GetAsync(int id) { var stopwatch = new Stopwatch(); stopwatch.Start(); var item = await this.GetModelAsync(id); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " GetAsync", stopwatch.ElapsedMilliseconds); HttpStatusCode code = HttpStatusCode.OK; if (item == null) { code = HttpStatusCode.NotFound; } return(MarsResponse.GetResponse(item, code)); }
public async Task <MarsResponse> GetParentModelsAsync(int id) { return(await Task.Run(() => { var stopwatch = new Stopwatch(); stopwatch.Start(); var itemList = this.ModelSet.Where(c => c.ParentId == id).ToArray(); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " GetParentModelsAsync", stopwatch.ElapsedMilliseconds); HttpStatusCode code = HttpStatusCode.OK; if (itemList == null) { code = HttpStatusCode.NotFound; } return MarsResponse.GetResponse(itemList, code); })); }
public async Task <MarsResponse> GetAllCustomersAsync() { return(await Task.Run(() => { var stopwatch = new Stopwatch(); stopwatch.Start(); var itemList = (from item in this.ModelSet select new { item.Id, item.Name, item.ImageUrl }).ToArray(); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " GetAllCustomersAsync", stopwatch.ElapsedMilliseconds); HttpStatusCode code = HttpStatusCode.OK; if (itemList == null || itemList.Count() == 0) { code = HttpStatusCode.NotFound; } return MarsResponse.GetResponse(itemList, code); })); }
public async Task <MarsResponse> SearchAllAsync(IEnumerable <int> idList) { var stopwatch = new Stopwatch(); stopwatch.Start(); var itemList = await SearchAllModelAsync(idList); MarsResponse resp = MarsResponse.GetResponse(itemList); if (itemList == null) { resp.Code = HttpStatusCode.NotAcceptable; } else if (itemList.Count() == 0) { resp.Code = HttpStatusCode.NotFound; } stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " SearchAllAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(itemList)); }
public async Task <MarsResponse> CreateAsync(PatientModel item) { var stopwatch = new Stopwatch(); stopwatch.Start(); if (string.IsNullOrEmpty(item.ImageUrl)) { if (!string.IsNullOrEmpty(item.SpeciesName)) { if (item.SpeciesName.ToUpper().CompareTo("CAT") == 0) { item.ImageUrl = DefaultPatientsUrl.DEFAULT_CAT; } else if (item.SpeciesName.ToUpper().CompareTo("DOG") == 0) { item.ImageUrl = DefaultPatientsUrl.DEFAULT_DOG; } else { item.ImageUrl = DefaultPatientsUrl.DEFAULT_IMG; } } else { item.ImageUrl = DefaultPatientsUrl.DEFAULT_IMG; } } await this.CreateModelAsync(item); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " CreateAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(item, HttpStatusCode.Created)); }
public async Task <MarsResponse> CreateAllAsync(object jsonObj, string token) { var appts = (JsonConvert.DeserializeObject(jsonObj.ToString()) as dynamic)["appointments"]; if (appts == null) { throw new Exception("Wrong Parameters"); } //var sendEmail = (JsonConvert.DeserializeObject(jsonObj.ToString()) as dynamic)["sendEmail"]; //var emailAddress = (JsonConvert.DeserializeObject(jsonObj.ToString()) as dynamic)["emailAddress"]; //bool shouldSendEmail = true; //string ccEmailAddress = ""; //if (sendEmail != null) // shouldSendEmail = Convert.ToBoolean(sendEmail); //if (emailAddress != null) // ccEmailAddress = emailAddress.ToString(); AppointmentModel[] appointmentList = JsonConvert.DeserializeObject <AppointmentModel[]>(appts.ToString()); if (appointmentList == null || !appointmentList.Any()) { throw new Exception("No Values inside"); } var stopwatch = new Stopwatch(); stopwatch.Start(); foreach (var item in appointmentList) { await this.ModelSet.AddAsync(item); } await this.SaveChangesAsync(); stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " CreateAllAsync", stopwatch.ElapsedMilliseconds); List <NotificationPayload> payload = new List <NotificationPayload>(); foreach (var item in appointmentList) { payload.Add(new NotificationPayload(item.Id)); } SendNotification(null, payload, token, NotificationEvent.NEW_APPT); var appt = appointmentList[0]; await AddOtherValues(appt, token); if (appt.ClientId > 0 && appt.IsEmail) { string subject = "Your appointment has been scheduled"; string body = AppointmentMailFormat.GetAppointmentMail(appt); SendEmailNotification(null, appt.ClientId, subject, body, token, appt.Email != null ? appt.Email : ""); } return(MarsResponse.GetResponse(appointmentList)); }
public async Task <MarsResponse> SearchAppointmentsAsync(object jsonObj, string token) { var stopwatch = new Stopwatch(); stopwatch.Start(); AppointmentModel item = JsonConvert.DeserializeObject <AppointmentModel>(jsonObj.ToString()); IEnumerable <AppointmentModel> apptList = null; List <Func <AppointmentModel, bool> > filterList = new List <Func <AppointmentModel, bool> >(); if (item.StartTime != default(DateTime) && item.EndTime != default(DateTime)) { filterList.Add(appt => appt.StartTime >= item.StartTime && appt.EndTime <= item.EndTime); } if (item.DoctorId > 0) { filterList.Add(x => x.DoctorId == item.DoctorId); } if (item.TypeId > 0) { filterList.Add(x => x.TypeId == item.TypeId); } var doc_str = (JsonConvert.DeserializeObject(jsonObj.ToString()) as dynamic)?["doctor_ids"]?.ToString(); if (doc_str != null) { int[] doc_ids = JsonConvert.DeserializeObject <int[]>(doc_str); if (doc_ids != null && doc_ids.Any()) { filterList.Add(appt => doc_ids.Contains(appt.DoctorId)); } } var apptStat_str = (JsonConvert.DeserializeObject(jsonObj.ToString()) as dynamic)?["appt_statuses"]?.ToString(); if (apptStat_str != null) { int[] apptStat_ids = JsonConvert.DeserializeObject <int[]>(apptStat_str); if (apptStat_ids != null && apptStat_ids.Any()) { filterList.Add(appt => apptStat_ids.Contains(appt.Status)); } } if (item.Status > 0) { filterList.Add(appt => appt.Status == item.Status); } if (item.ClientId > 0) { filterList.Add(appt => appt.ClientId == item.ClientId); } if (filterList.Count > 0) { apptList = this.ModelSet.Where(appt => filterList.All(filter => filter(appt))).ToArray(); } //if (apptList != null) // foreach (var appt in apptList) // await AddOtherValues(appt, token); apptList = await AddAllOtherValues(apptList, token); MarsResponse resp = MarsResponse.GetResponse(apptList); if (apptList == null) { resp.Code = HttpStatusCode.NotAcceptable; } else if (apptList.Count() == 0) { resp.Code = HttpStatusCode.NotFound; } stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " SearchAppointmentsAsync", stopwatch.ElapsedMilliseconds); return(MarsResponse.GetResponse(apptList)); }
public async Task <MarsResponse> UpdateStatusWithNotification(AppointmentModel item, string eventCode, string token) { var stopwatch = new Stopwatch(); stopwatch.Start(); var mod = await this.ModelSet.FirstOrDefaultAsync(c => c.Id == item.Id); if (mod != null) { if (item.Status != 0) { mod.Status = item.Status; } if (item.WorkflowState != 0) { mod.WorkflowState = item.WorkflowState; } await this.SaveChangesAsync(); } stopwatch.Stop(); this.logHandler.LogMetric(this.GetType().ToString() + " UpdateStatus", stopwatch.ElapsedMilliseconds); if (mod != null) { NotificationPayload payload = new NotificationPayload(mod.Id); string email = string.Empty; string name = string.Empty; if (mod.DoctorId > 0 && (eventCode.CompareTo(NotificationEvent.CHECKIN) == 0 || eventCode.CompareTo(NotificationEvent.RESPOND) == 0 || eventCode.CompareTo(NotificationEvent.CLAIM) == 0)) { var resourceResult = await InterServiceCommunication.GetAsync(ServiceAddress.ResourceSearchUrl + mod.DoctorId.ToString(), token); if (resourceResult != null) { var data = (JsonConvert.DeserializeObject(resourceResult) as dynamic)?["data"]; if (data?["email"] != null) { email = data["email"]; } if (data?["resource_name"] != null) { name = data["resource_name"]; } } } if (eventCode.CompareTo(NotificationEvent.RESPOND) == 0) { payload.SenderEmail = email; payload.SenderName = name; if (!string.IsNullOrEmpty(item.Response)) { payload.Message = item.Response; } else { payload.Message = string.Empty; } } else if (eventCode.CompareTo(NotificationEvent.CLAIM) == 0) { payload.SenderEmail = email; } else if (eventCode.CompareTo(NotificationEvent.CHECKIN) == 0) { payload.SenderEmail = email; } if (eventCode.CompareTo(NotificationEvent.CHECKIN) == 0 && !string.IsNullOrEmpty(email)) { List <string> objEmailList = new List <string>(); objEmailList.Add(email); SendNotification(objEmailList, new List <NotificationPayload>() { payload }, token, NotificationEvent.CHECKIN_NOTIFY); } SendNotification(null, new List <NotificationPayload>() { payload }, token, eventCode); } return(MarsResponse.GetResponse(mod, mod == null ? HttpStatusCode.NotFound : HttpStatusCode.OK)); }
public async Task <MarsResponse> SearchAvailabilityAsync(AvailabilitySerachModel availabilitySearch, String token) { string errMsg = string.Empty; //Validate input errMsg = await ValidateAvailabilitySearchCriteriaAsync(availabilitySearch.PatientId, availabilitySearch.AppointmentDate); if (!string.IsNullOrEmpty(errMsg)) { var errResp = MarsResponse.GetResponse(string.Empty, HttpStatusCode.BadRequest); errResp.Error = new MarsException(errMsg); return(errResp); } //Fetch prefered doctor for given patient id from marsstorage.[dbo].[Patients] tablle. string patientsUrl = ServiceAddress.PatientsBaseUrl + availabilitySearch.PatientId; var patient = await InterServiceCommunication.GetAsync(patientsUrl, token); int prefDoctorID = 0; if (patient != null) { dynamic patientData = JsonConvert.DeserializeObject(patient); var data = patientData["data"]; if (data["pref_doctor"] != null) { prefDoctorID = data["pref_doctor"]; } } //Fetch doctor availability from marspoc.dbo.[Availability] string resourcesUrl = ServiceAddress.AvailabilityBaseUrl; //"http://localhost:5000/api/resource/availability/"; // var availability = string.Empty; if (prefDoctorID > 0) { var body = new AvailabilityRequest { Id = 0, Date = availabilitySearch.AppointmentDate.Date, StartTime = new TimeSpan(), EndTime = new TimeSpan(), ResourceId = prefDoctorID }; availability = await InterServiceCommunication.PostAsync(resourcesUrl, token, body); } dynamic availabilityData = JsonConvert.DeserializeObject(availability); dynamic avData = null; if (!string.IsNullOrEmpty(availability)) { avData = availabilityData["data"]; } if (prefDoctorID == 0 || avData.First == null) { availability = await InterServiceCommunication.GetAsync(resourcesUrl, token); availabilityData = JsonConvert.DeserializeObject(availability); if (!string.IsNullOrEmpty(availability)) { avData = availabilityData["data"]; } } DateTime startTime;// = new DateTime(); DateTime endTime; String doctorName = string.Empty; int docId = 0; //DateTime bookingTime; TimeSpan hospitalStartTime; TimeSpan hospitalEndTime; TimeSpan.TryParse(configuration.GetSection("Hospital").GetSection("StartTime").Value, out hospitalStartTime); TimeSpan.TryParse(configuration.GetValue <string>("Hospital:EndTime"), out hospitalEndTime); List <AvailabilityModel> timeslots = new List <AvailabilityModel>(); if (!string.IsNullOrEmpty(availability) && avData.First != null) { //Fetch default time for given appointment type from master data(mars_appointments.[dbo].[appointment_types]) int defaultDuration = availabilitySearch.SubTypeId > 0? GetDefaultDurationOfApptType(availabilitySearch.SubTypeId) : 30; if (defaultDuration > 0) { //dynamic availabilityData = JsonConvert.DeserializeObject(availability); //dynamic data = availabilityData["data"]; //List<AvailabilityModel> availableResources = JsonConvert.DeserializeObject<List<AvailabilityModel>>(JsonConvert.SerializeObject(data)); foreach (dynamic d in avData) { if (timeslots.Count > 0) { break; } //startTime = ((DateTime)d["date"]).Add(((TimeSpan)d["startTime"] > hospitalStartTime && (TimeSpan)d["startTime"] < hospitalEndTime) ? (TimeSpan)d["startTime"] : hospitalStartTime); //endTime = ((DateTime)d["date"]).Add( (TimeSpan)d["endTime"] ); startTime = ((DateTime)d["date"]).Add((TimeSpan)d["startTime"]); endTime = ((DateTime)d["date"]).Add((TimeSpan)d["endTime"]); docId = d["resourceId"]; doctorName = d["name"]; //bookingTime = new DateTime(1970, 1, 1).AddMilliseconds(availabilitySearch.ClientReqTime).ToLocalTime();// .AddHours(-7); //setting to PST time // .ToLocalTime(); DateTime bookingTime = RoundUp(DateTime.Parse(DateTime.Now.ToString()), TimeSpan.FromMinutes(30)); if (bookingTime >= endTime) { continue; } else if (bookingTime > startTime && bookingTime < endTime) { //string s = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"); //string st = String.Format("{0:s}", DateTime.Now); //string st = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:00"); startTime = Convert.ToDateTime(bookingTime.ToString("yyyy-MM-ddTHH:mm:00")); //startTime = startTime.Add(DateTime.Now.AddSeconds(-DateTime.Now.Second).TimeOfDay); if (endTime.Subtract(startTime).TotalMinutes < defaultDuration) { continue; } } //Fetch Appointments for given resource from mars_appointment.[dbo].[appointments] table AppointmentModel appModel = new AppointmentModel { DoctorId = docId //prefDoctorID > 0 ? prefDoctorID : docId }; var appointments = this.appointmentContext.SearchModelByResourceIdAndDate(appModel, startTime.Date); if (appointments.ToList().Count > 1) { appointments = appointments.Where(a => a.EndTime >= startTime && a.StartTime <= endTime).OrderBy(a => a.StartTime); } //Find availability based on default duration of appointment type and already booked appointments if any. if (availabilitySearch.DefaultSlot) { timeslots = FindDefaultTimeslot(appointments.ToList(), startTime, endTime, defaultDuration); } else { timeslots = FindAllAvailableTimeslots(appointments.ToList(), startTime, endTime, defaultDuration); } if (timeslots != null && timeslots.Count > 0) { foreach (var t in timeslots) { t.DoctorId = docId; t.DoctorName = doctorName; t.AppointmentDate = startTime.Date; } //if (availabilitySearch.DefaultSlot || timeslots.Count == 1) //{ // timeslots.FirstOrDefault().DoctorId = prefDoctorID; // timeslots.FirstOrDefault().AppointmentDate = availabilitySearch.AppointmentDate.Date; // timeslots.FirstOrDefault().DoctorName = doctorName; //} } } } } MarsResponse resp = MarsResponse.GetResponse(timeslots); if (timeslots == null) { resp.Code = HttpStatusCode.NotAcceptable; } else if (timeslots.Count() == 0) { //resp.Code = HttpStatusCode.Accepted; resp.Error = new MarsException("Time slots not found."); } return(resp); }