public async Task <string> Insert_BrandDoseTemplate(BrandDoseTemplateCreateVM vm, int userId) { try { //## First get the Strength id- read the list from Cache- don't go to DB. var strengthTypeList = _cacheService.GetCacheValue <List <StrengthType> >(CacheKey.StrengthTypes); var strengthType = strengthTypeList.FirstOrDefault(s => s.Name.Equals(vm.StrengthTypeText)); //## Use the Text to find the id, ie: '10 mg' = Id:2 if (strengthType is null) { //## Something peculiar strength may be, ie: "199 mg"--> insert it in the DB strengthType = new StrengthType() { Name = vm.StrengthTypeText }; await _context.StrengthTypes.AddAsync(strengthType); await _context.SaveChangesAsync(); } string teamplateName = CreateTemplateName(vm); BrandDoseTemplate doseTemplate = new BrandDoseTemplate() { DrugBrandId = vm.DrugBrandId, ModeOfDeliveryId = vm.ModeOfDeliveryId, StrengthTypeId = strengthType.Id, Dose = vm.Dose, Frequency = vm.Frequency, IntakePatternId = vm.IntakePatternId, Duration = vm.Duration, CreatedBy = userId, DateCreated = DateTime.Now, TemplateText = teamplateName }; await _context.BrandDoseTemplates.AddAsync(doseTemplate); await _context.SaveChangesAsync(); //## Add this new BrandDoseTemplate to the Cache- so others can see it immediately UpdateBrandDoseTemplateIn_Cache(doseTemplate); string result = $"{doseTemplate.Id};{teamplateName}"; //## Combine the Id of newly inserted Template and the TemplateTExt- to add to the Dropdown- on Success return(result); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return("error"); //throw; } }
/// <summary>This will create DrugDose Template, ie: '10 mg 1x3T-7D'</summary> /// <param name="vm">BrandDoseTemplateCreateVM view model</param> /// <returns></returns> private string CreateTemplateName(BrandDoseTemplateCreateVM vm) { //## Get ModeOfDelivery List- and find the 'Delivery' Text var listOfDeliveryMode = _cacheService.GetCacheValue <IList <ModeOfDelivery> >(CacheKey.ModeOfDeliveries); var deliveryMode = listOfDeliveryMode.First(d => d.Id == vm.ModeOfDeliveryId).Name; //## we will know- Tablet/Capsule/Syrup! string drugDose = vm.Dose == (short)DoseSolid.Half ? "half" : vm.Dose.ToString(); string durationText = vm.Duration == 0 ? "TBC" : vm.Duration.ToString() + " Days"; string teamplateName = $"{vm.StrengthTypeText}-{deliveryMode}-{vm.Dose}*{vm.Frequency}Times-{durationText}"; //## ie: "10mg-Tablet-1*3Times-7 Days" return(teamplateName); }
public async Task <ActionResult> Insert_BrandDoseTemplate(BrandDoseTemplateCreateVM vm) { //## This will create a Template for a Specific Drug Brand. ie: 'Ibuprofen 200mg Tablet 4 Times a Day for 7 days' if (vm is null || vm.StrengthTypeText is null || vm.ModeOfDeliveryId == 0 || vm.DrugBrandId == 0 || vm.Duration == 0) { return(Json("error")); } AppUserDetailsVM currentUser = await GetCurrentUser(); var result = await _drugService.Insert_BrandDoseTemplate(vm, currentUser.Id); return(Json(result)); }
/// <summary>This will Map the 'BrandDoseTemplate' to 'BrandDoseTemplateCreateVM' and make it easier to store in the Redis Cache</summary> /// <param name="dose">BrandDoseTemplate object</param> /// <returns>BrandDoseTemplateCreateVM View Model</returns> private BrandDoseTemplateCreateVM MapTo_BrandDoseTemplateVM(BrandDoseTemplate dose) { BrandDoseTemplateCreateVM result = new BrandDoseTemplateCreateVM() { DrugBrandId = dose.DrugBrandId, IntakePatternId = dose.IntakePatternId, Duration = dose.Duration, ModeOfDeliveryId = dose.ModeOfDeliveryId, StrengthTypeId = dose.StrengthTypeId.Value, Dose = dose.Dose.Value, Frequency = dose.Frequency }; return(result); }
private void UpdateBrandDoseTemplateIn_Cache(BrandDoseTemplate doseTemplate) { //## Add this new BrandDoseTemplate to the Cache- so others can see it immediately var existingBrandDoseTemplate = _cacheService.GetCacheValue <IList <BrandDoseTemplateCreateVM> >(CacheKey.BrandDoseTemplate); //## Get from Cache if (existingBrandDoseTemplate is null) { existingBrandDoseTemplate = new List <BrandDoseTemplateCreateVM>(); } BrandDoseTemplateCreateVM newBrandDoseTeamplate = MapTo_BrandDoseTemplateVM(doseTemplate); existingBrandDoseTemplate.Add(newBrandDoseTeamplate); //## Add this new BrandTemplate _cacheService.SetCacheValue(CacheKey.BrandDoseTemplate, existingBrandDoseTemplate); //## Push that new list to Cache }