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 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 }