public async Task <OperationResult> Update([FromBody] SurveyConfigUpdateModel updateModel) { return(await _surveysService.Update(updateModel)); }
public async Task <OperationResult> Update(SurveyConfigUpdateModel updateModel) { try { var core = await _coreHelper.GetCore(); await using var sdkContext = core.DbContextHelper.GetDbContext(); var surveyConfiguration = await sdkContext.SurveyConfigurations .Include(x => x.SiteSurveyConfigurations) .Where(x => x.Id == updateModel.Id) .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .FirstOrDefaultAsync(); if (surveyConfiguration == null) { //transaction.Commit(); return(new OperationResult( false, _localizationService.GetString("SurveyConfigurationNotFound"))); } surveyConfiguration.QuestionSetId = updateModel.SurveyId; await surveyConfiguration.Update(sdkContext); // Locations var siteIds = surveyConfiguration.SiteSurveyConfigurations .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Select(x => x.SiteId) .ToList(); var forRemove = siteIds .Where(x => !updateModel.LocationsIds.Contains(x)) .ToList(); foreach (var siteIdForRemove in forRemove) { var siteSurveyConfigurations = await sdkContext .SiteSurveyConfigurations .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .FirstOrDefaultAsync( x => x.SurveyConfigurationId == surveyConfiguration.Id && x.SiteId == siteIdForRemove); if (siteSurveyConfigurations != null) { await siteSurveyConfigurations.Delete(sdkContext); } } var forCreate = updateModel.LocationsIds .Where(x => !siteIds.Contains(x)) .ToList(); foreach (var siteIdForCreate in forCreate) { var siteSurveyConfigurations = new SiteSurveyConfiguration() { SurveyConfigurationId = surveyConfiguration.Id, SiteId = siteIdForCreate }; await siteSurveyConfigurations.Create(sdkContext); } return(new OperationResult( true, _localizationService.GetString("SurveyConfigurationUpdatedSuccessfully"))); } catch (Exception e) { Trace.TraceError(e.Message); _logger.LogError(e.Message); return(new OperationResult( false, _localizationService.GetString("ErrorWhileUpdatingSurveyConfiguration"))); } }