/// <summary> /// Creates instance of SelectionAnswerChoiceString using dto data. /// </summary> /// <param name="dto"></param> /// <returns></returns> private async Task <QuestionElementString> MapAnswerString(CreateLocalizedStringRequestDto dto) { if (dto == null) { throw new ArgumentNullException("dto"); } var result = Mapper.Map <QuestionElementString>(dto); result.Language = string.IsNullOrEmpty(careElementContext.Language) ? careElementContext.DefaultLanguage : careElementContext.Language; if (dto.AudioFileMedia == null) { return(result); } result.AudioFileMedia = await mediaFileHelper.CreateMediaFile(dto.AudioFileMedia); if (result.AudioFileMedia != null) { result.AudioFileMedia.Tags = await tagsService.BuildTagsList(careElementContext.CustomerId, dto.AudioFileMedia.Tags); } return(result); }
/// <summary> /// Creates the specified model. /// </summary> /// <param name="customerId">The customer identifier.</param> /// <param name="model">The model.</param> /// <returns></returns> public async Task <OperationResultDto <Guid, CreateScaleAnswerSetStatus> > Create(int customerId, CreateScaleAnswerSetRequestDto model) { var status = ValidateLabels(model); if (status > 0) { return(new OperationResultDto <Guid, CreateScaleAnswerSetStatus>() { Status = status }); } var answerSet = Mapper.Map <ScaleAnswerSet>(model); answerSet.CustomerId = customerId; answerSet.Tags = await tagsService.BuildTagsList(customerId, model.Tags); var lowLabel = await MapLabelToLocalizedString <LowLabelScaleAnswerSetString>( model.Labels.LowLabel, careElementContext.DefaultLanguage); answerSet.LowLabelScaleAnswerSetStrings.Add(lowLabel); // Mid is optional field. if (model.Labels.MidLabel != null) { var midLabel = await MapLabelToLocalizedString <MidLabelScaleAnswerSetString> (model.Labels.MidLabel, careElementContext.DefaultLanguage); answerSet.MidLabelScaleAnswerSetStrings.Add(midLabel); } var highLabel = await MapLabelToLocalizedString < HighLabelScaleAnswerSetString>(model.Labels.HighLabel, careElementContext.DefaultLanguage); answerSet.HighLabelScaleAnswerSetStrings.Add(highLabel); var result = await scaleAnswerSetService.Create(answerSet); await globalSearchCacheHelper.AddOrUpdateEntry(customerId, Mapper.Map <ScaleAnswerSet, SearchEntryDto>(result)); await tagsSearchCacheHelper.AddOrUpdateTags(customerId, result.Tags.Select(t => t.Name).ToList()); var unusedTags = await tagsService.RemoveUnusedTags(customerId); await tagsSearchCacheHelper.RemoveTags(customerId, unusedTags); return(new OperationResultDto <Guid, CreateScaleAnswerSetStatus>() { Content = result.Id, Status = CreateScaleAnswerSetStatus.Success }); }
/// <summary> /// Creates the media file. /// </summary> /// <param name="source">The source.</param> /// <returns></returns> public async Task <Media> CreateMediaFile(BaseMediaRequestDto source) { var contentModel = Mapper.Map <BaseMediaRequestDto, ContentModel>(source); var uploadResult = await contentStorage.UploadContent(contentModel); if (uploadResult == null) { return(await Task.FromResult <Media>(null)); } var media = new Media { ContentLength = uploadResult.OriginalContentLength, OriginalStorageKey = uploadResult.OriginalStorageKey, ThumbnailStorageKey = uploadResult.ThumbnailStorageKey, OriginalFileName = uploadResult.OriginalFileName, CustomerId = CareElementRequestContext.Current.CustomerId, Tags = await tagsService.BuildTagsList(CareElementRequestContext.Current.CustomerId, source.Tags) }; Mapper.Map <BaseMediaRequestDto, Media>(source, media); return(media); }
/// <summary> /// Generates Program instance using data of ProgramRequestDto. /// </summary> /// <param name="customerId">The customer identifier.</param> /// <param name="programDto">The program dto.</param> /// <returns></returns> private async Task <OperationResultDto <Program, CreateUpdateProgramStatus> > BuildProgram(int customerId, ProgramRequestDto programDto) { var resultProgram = Mapper.Map <Program>(programDto); resultProgram.CustomerId = customerId; resultProgram.Tags = await tagsService.BuildTagsList(customerId, programDto.Tags); resultProgram.ProgramElements = new List <ProgramElement>(); foreach (var programElementDto in programDto.ProgramElements) { var programElement = Mapper.Map <ProgramElement>(programElementDto); var protocol = await this.protocolService.GetProtocol(customerId, programElement.ProtocolId); if (protocol == null) { return(new OperationResultDto <Program, CreateUpdateProgramStatus>() { Content = null, Status = CreateUpdateProgramStatus.InvalidProtocolId }); } foreach (var programDayElementDto in programElementDto.ProgramDayElements) { var programDayElement = BuildProgramDayElement(programDayElementDto, resultProgram.Recurrences); if (programDayElement == null) { return(new OperationResultDto <Program, CreateUpdateProgramStatus>() { Status = CreateUpdateProgramStatus.InvalidRecurrenceReferenceProvided }); } programElement.ProgramDayElements.Add(programDayElement); resultProgram.ProgramDayElements.Add(programDayElement); } resultProgram.ProgramElements.Add(programElement); } if (resultProgram.Recurrences != null) { resultProgram.Recurrences.Each(r => r.Id = default(Guid)); } return(new OperationResultDto <Program, CreateUpdateProgramStatus>() { Status = CreateUpdateProgramStatus.Success, Content = resultProgram }); }
/// <summary> /// Creates the specified media dto. /// </summary> /// <param name="customerId">The customer identifier.</param> /// <param name="mediaDto">The media dto.</param> /// <returns></returns> public async Task <OperationResultDto <Guid, CreateMediaStatus> > CreateMedia(int customerId, CreateMediaRequestDto mediaDto) { var contentModel = Mapper.Map <CreateMediaRequestDto, ContentModel>(mediaDto); var uploadResult = await contentStorage.UploadContent(contentModel); if (uploadResult == null) { return(new OperationResultDto <Guid, CreateMediaStatus>() { Status = CreateMediaStatus.InvalidContentOrSourceContentUrlProvided }); } var media = new Media() { CustomerId = customerId, Name = mediaDto.Name, Description = mediaDto.Description, ContentType = mediaDto.ContentType, ContentLength = uploadResult.OriginalContentLength, Tags = await tagsService.BuildTagsList(customerId, mediaDto.Tags), OriginalStorageKey = uploadResult.OriginalStorageKey, ThumbnailStorageKey = uploadResult.ThumbnailStorageKey, OriginalFileName = uploadResult.OriginalFileName }; var createdMedia = await mediaService.Create(media); await tagsSearchCacheHelper.AddOrUpdateTags(customerId, createdMedia.Tags.Select(t => t.Name).ToList()); var unusedTags = await tagsService.RemoveUnusedTags(customerId); await tagsSearchCacheHelper.RemoveTags(customerId, unusedTags); return(new OperationResultDto <Guid, CreateMediaStatus>() { Content = createdMedia.Id, Status = CreateMediaStatus.Success }); }
/// <summary> /// Creates entity and saves data in datasource. /// </summary> /// <param name="customerId">The customer identifier.</param> /// <param name="model">The model.</param> /// <returns> /// Id of created entity /// </returns> public async Task <Guid> Create(int customerId, CreateSelectionAnswerSetRequestDto model) { var answerSet = Mapper.Map <SelectionAnswerSet>(model); answerSet.CustomerId = customerId; answerSet.Tags = await tagsService.BuildTagsList(customerId, model.Tags); var selectionAnswerChoices = model.SelectionAnswerChoices.ToList(); foreach (var answerDto in selectionAnswerChoices) { var answer = await this.MapSelectionAnswerChoice(answerDto, selectionAnswerChoices); answerSet.SelectionAnswerChoices.Add(answer); } var result = await this.selectionAnswerSetService.Create(answerSet); await UpdateCachedLists(customerId, result); return(result.Id); }
/// <summary> /// Creates the specified new text media element dto. /// </summary> /// <param name="customerId">The customer identifier.</param> /// <param name="newTextMediaElementDto">The new text media element dto.</param> /// <returns></returns> public async Task <OperationResultDto <Guid, CreateTextMediaElementStatus> > Create( int customerId, CreateTextMediaElementRequestDto newTextMediaElementDto ) { var validationResult = await ValidateCreateTextMediaElement(customerId, newTextMediaElementDto); if (!validationResult.HasFlag(CreateTextMediaElementStatus.Success)) { return(new OperationResultDto <Guid, CreateTextMediaElementStatus>() { Status = validationResult }); } var elementTags = await tagsService.BuildTagsList(customerId, newTextMediaElementDto.Tags); var mappedTextMediaElement = Mapper.Map <CreateTextMediaElementRequestDto, TextMediaElement>(newTextMediaElementDto); mappedTextMediaElement.CustomerId = customerId; mappedTextMediaElement.Tags = elementTags; var elementString = Mapper.Map <CreateLocalizedStringRequestDto, TextMediaElementString>(newTextMediaElementDto.Text); if (elementString != null) { elementString.Language = CareElementRequestContext.Current.DefaultLanguage; mappedTextMediaElement.TextLocalizedStrings = new List <TextMediaElementString>() { elementString }; if (newTextMediaElementDto.Text.AudioFileMedia != null) { elementString.AudioFileMedia = await mediaFileHelper.CreateMediaFile(newTextMediaElementDto.Text.AudioFileMedia); } } if (newTextMediaElementDto.MediaId.HasValue) { mappedTextMediaElement.TextMediaElementsToMedias = new List <TextMediaElementToMedia>() { new TextMediaElementToMedia() { MediaId = newTextMediaElementDto.MediaId.Value, Language = CareElementRequestContext.Current.DefaultLanguage } }; } else if (newTextMediaElementDto.Media != null) { var media = await mediaFileHelper.CreateMediaFile(newTextMediaElementDto.Media); if (media != null) { mappedTextMediaElement.TextMediaElementsToMedias = new List <TextMediaElementToMedia>() { new TextMediaElementToMedia() { Media = media, Language = CareElementRequestContext.Current.DefaultLanguage } }; } } var result = await textMediaElementsService.Create(mappedTextMediaElement); await globalSearchCacheHelper.AddOrUpdateEntry(customerId, Mapper.Map <TextMediaElement, SearchTextAndMediaDto>(result)); await tagsSearchCacheHelper.AddOrUpdateTags(customerId, result.Tags.Select(t => t.Name).ToList()); var unusedTags = await tagsService.RemoveUnusedTags(customerId); await tagsSearchCacheHelper.RemoveTags(customerId, unusedTags); return(new OperationResultDto <Guid, CreateTextMediaElementStatus>() { Status = CreateTextMediaElementStatus.Success, Content = mappedTextMediaElement.Id }); }
/// <summary> /// Builds the protocol. /// </summary> /// <param name="customerId"></param> /// <param name="request">The request.</param> /// <returns></returns> private async Task <Protocol> BuildProtocol(int customerId, CreateProtocolRequestDto request) { var protocol = new Protocol { CustomerId = customerId, NameLocalizedStrings = new List <ProtocolString>() { new ProtocolString() { Value = request.Name, Language = careElementContext.DefaultLanguage } }, IsPrivate = request.IsPrivate, Tags = await tagsService.BuildTagsList(customerId, request.Tags), ProtocolElements = new List <ProtocolElement>() }; foreach (var protocolElementDto in request.ProtocolElements) { var protocolElement = new ProtocolElement { Id = protocolElementDto.Id, // Temporary storing client id to build up relationships and dump it before saving to the database ElementId = protocolElementDto.ElementId, Sort = protocolElementDto.Sort, Branches = new List <Branch>(), Alerts = new List <Alert>() }; if (protocolElementDto.Branches != null) { foreach (var branchDto in protocolElementDto.Branches) { if (branchDto.Conditions != null) { var branch = new Branch() { NextProtocolElementId = branchDto.NextProtocolElementId, ThresholdAlertSeverityId = branchDto.ThresholdAlertSeverityId, Conditions = new List <Condition>() }; foreach (var conditionDto in branchDto.Conditions) { var condition = new Condition() { Operand = conditionDto.Operand, Operator = conditionDto.Operator, Value = conditionDto.Value }; branch.Conditions.Add(condition); } protocolElement.Branches.Add(branch); } } } if (protocolElementDto.Alerts != null) { foreach (var alertDto in protocolElementDto.Alerts) { if (alertDto.Conditions != null) { var alert = new Alert() { Conditions = new List <Condition>() }; foreach (var conditionDto in alertDto.Conditions) { var condition = new Condition() { Operand = conditionDto.Operand, Operator = conditionDto.Operator, Value = conditionDto.Value }; alert.Conditions.Add(condition); } alert.AlertSeverityId = alertDto.AlertSeverityId; protocolElement.Alerts.Add(alert); } } } protocol.ProtocolElements.Add(protocolElement); } // Assigning FirstProtocolElement var firstProtocolElement = protocol.ProtocolElements.Single(pe => pe.Id == request.FirstProtocolElementId); firstProtocolElement.IsFirstProtocolElement = true; // Assigning NextProtocolElements and removing client Ids foreach (var protocolElement in protocol.ProtocolElements) { var nextProtocolElementIdForProtocolElement = request.ProtocolElements.Single(pe => pe.Id == protocolElement.Id).NextProtocolElementId; if (nextProtocolElementIdForProtocolElement != null) { protocolElement.NextProtocolElement = protocol.ProtocolElements.Single(pe => pe.Id == nextProtocolElementIdForProtocolElement); } foreach (var branch in protocolElement.Branches.Where(b => b.NextProtocolElementId != null)) { branch.NextProtocolElement = protocol.ProtocolElements.Single(pe => pe.Id == branch.NextProtocolElementId); branch.NextProtocolElementId = null; // Removing client Id } } foreach (var protocolElement in protocol.ProtocolElements) { protocolElement.Id = default(Guid); // Removing client Id } return(protocol); }