public async Task SaveTenantAsync(Tenant tenant) { await this.tenantBlobContainer.SaveAsync(tenant.TenantId.ToLowerInvariant(), tenant).ConfigureAwait(false); if (this.CacheEnabled) { await TenantCacheHelper.AddToCacheAsync(tenant.TenantId, TenantAccountTag, tenant).ConfigureAwait(false); } }
public void SaveTenant(Tenant tenant) { this.tenantBlobContainer.Save(tenant.Name.ToLowerInvariant(), tenant); if (this.CacheEnabled) { TenantCacheHelper.AddToCache(tenant.Name, TenantAccountTag, tenant); } }
public async Task <Tenant> GetTenantAsync(string tenantId) { Func <Task <Tenant> > resolver = async() => await this.tenantBlobContainer.GetAsync(tenantId.ToLowerInvariant()).ConfigureAwait(false); if (this.CacheEnabled) { return(await TenantCacheHelper.GetFromCacheAsync(tenantId, TenantAccountTag, resolver).ConfigureAwait(false)); } else { return(await resolver().ConfigureAwait(false)); } }
public async Task <Survey> GetSurveyByTenantAndSlugNameAsync(string tenant, string slugName, bool getQuestions) { Func <Task <Survey> > resolver = async() => { var surveyRow = await GetSurveyRowByTenantAndSlugNameAsync(this.surveyTable, tenant, slugName); if (surveyRow == null) { return(null); } var survey = new Survey(surveyRow.SlugName) { TenantId = surveyRow.PartitionKey, Title = surveyRow.Title, CreatedOn = surveyRow.CreatedOn }; if (getQuestions) { var surveyQuestionRows = await GetSurveyQuestionRowsByTenantAndSlugNameAsync(this.questionTable, tenant, slugName); foreach (var questionRow in surveyQuestionRows) { survey.Questions.Add( new Question { Text = questionRow.Text, Type = (QuestionType)Enum.Parse(typeof(QuestionType), questionRow.Type), PossibleAnswers = questionRow.PossibleAnswers }); } } return(survey); }; if (this.CacheEnabled) { return(await TenantCacheHelper.GetFromCacheAsync(tenant, slugName, resolver).ConfigureAwait(false)); } else { return(await resolver().ConfigureAwait(false)); } }
public async Task DeleteSurveyByTenantAndSlugNameAsync(string tenant, string slugName) { var surveyRow = await GetSurveyRowByTenantAndSlugNameAsync(this.surveyTable, tenant, slugName); if (surveyRow == null) { return; } await this.surveyTable.DeleteAsync(surveyRow); var surveyQuestionRows = await GetSurveyQuestionRowsByTenantAndSlugNameAsync(this.questionTable, tenant, slugName); await this.questionTable.DeleteAsync(surveyQuestionRows); if (this.CacheEnabled) { await TenantCacheHelper.RemoveFromCacheAsync(tenant, slugName).ConfigureAwait(false); } }
public void DeleteSurveyByTenantAndSlugName(string tenant, string slugName) { var surveyRow = GetSurveyRowByTenantAndSlugName(this.surveyTable, tenant, slugName); if (surveyRow == null) { return; } this.surveyTable.Delete(surveyRow); var surveyQuestionRows = GetSurveyQuestionRowsByTenantAndSlugName(this.questionTable, tenant, slugName); this.questionTable.Delete(surveyQuestionRows); if (this.CacheEnabled) { TenantCacheHelper.RemoveFromCache(tenant, slugName); } }
public Survey GetSurveyByTenantAndSlugName(string tenant, string slugName, bool getQuestions) { Func <Survey> resolver = () => { var surveyRow = GetSurveyRowByTenantAndSlugName(this.surveyTable, tenant, slugName); if (surveyRow == null) { return(null); } var survey = new Survey(surveyRow.SlugName) { Tenant = surveyRow.PartitionKey, Title = surveyRow.Title, CreatedOn = surveyRow.CreatedOn, UserDefinedFields = surveyRow.UserDefinedFields }; if (getQuestions) { var surveyQuestionRows = GetSurveyQuestionRowsByTenantAndSlugName(this.questionTable, tenant, slugName); foreach (var questionRow in surveyQuestionRows) { survey.Questions.Add( new Question { Text = questionRow.Text, Type = (QuestionType)Enum.Parse(typeof(QuestionType), questionRow.Type), PossibleAnswers = questionRow.PossibleAnswers }); } } return(survey); }; return(this.CacheEnabled ? TenantCacheHelper.GetFromCache(tenant, slugName, resolver) : resolver()); }
public async Task SaveSurveyAsync(Survey survey) { if (string.IsNullOrEmpty(survey.SlugName) && string.IsNullOrEmpty(survey.Title)) { throw new ArgumentNullException("survey", "The survey for saving has to have the slug or the title."); } var slugName = string.IsNullOrEmpty(survey.SlugName) ? GenerateSlug(survey.Title, 100) : survey.SlugName; var surveyRow = new SurveyRow { SlugName = slugName, Title = survey.Title, CreatedOn = DateTime.UtcNow, PartitionKey = survey.TenantId }; surveyRow.RowKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", survey.TenantId, surveyRow.SlugName); var questionRows = new List <QuestionRow>(survey.Questions.Count); for (int i = 0; i < survey.Questions.Count; i++) { var question = survey.Questions[i]; var questionRow = new QuestionRow { Text = question.Text, Type = Enum.GetName(typeof(QuestionType), question.Type), PossibleAnswers = question.PossibleAnswers }; questionRow.PartitionKey = surveyRow.RowKey; questionRow.RowKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", DateTime.UtcNow.GetFormatedTicks(), i.ToString("D3")); questionRows.Add(questionRow); } //// First add the questions await this.questionTable.AddAsync(questionRows); try { //// Then add the survey //// If this fails, the questions may end up orphan but data will be consistent for the user await this.surveyTable.AddAsync(surveyRow); if (this.CacheEnabled) { await TenantCacheHelper.AddToCacheAsync(survey.TenantId, slugName, survey).ConfigureAwait(false); } } //TODO: update this catch for Azure Table client SDK catch (DataServiceRequestException ex) { TraceHelper.TraceError(ex.TraceInformation()); var dataServiceClientException = ex.InnerException as DataServiceClientException; if (dataServiceClientException != null) { if (dataServiceClientException.StatusCode == 409) { await this.questionTable.DeleteAsync(questionRows); throw; } } throw; } }
public void SaveSurvey(Survey survey) { if (string.IsNullOrEmpty(survey.SlugName) && string.IsNullOrEmpty(survey.Title)) { throw new ArgumentNullException("survey", "The survey for saving has to have the slug or the title."); } var slugName = string.IsNullOrEmpty(survey.SlugName) ? GenerateSlug(survey.Title, 100) : survey.SlugName; var surveyRow = new SurveyRow { SlugName = slugName, Title = survey.Title, CreatedOn = DateTime.UtcNow, PartitionKey = survey.Tenant, UserDefinedFields = survey.UserDefinedFields }; surveyRow.RowKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", survey.Tenant, surveyRow.SlugName); var questionRows = new List <QuestionRow>(survey.Questions.Count); for (int i = 0; i < survey.Questions.Count; i++) { var question = survey.Questions[i]; var questionRow = new QuestionRow { Text = question.Text, Type = Enum.GetName(typeof(QuestionType), question.Type), PossibleAnswers = question.PossibleAnswers }; questionRow.PartitionKey = surveyRow.RowKey; questionRow.RowKey = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", DateTime.UtcNow.GetFormatedTicks(), i.ToString("D3")); questionRows.Add(questionRow); } //// First add the questions this.questionTable.Add(questionRows); try { //// Then create the container for the answers this.surveyAnswerContainerFactory.Create(surveyRow.PartitionKey, surveyRow.SlugName).EnsureExist(); //// And finally, add the survey //// If this fails, the questions may end up orphan but data will be consistent for the user this.surveyTable.Add(surveyRow); if (this.CacheEnabled) { TenantCacheHelper.AddToCache(survey.Tenant, slugName, survey); } } catch (DataServiceRequestException ex) { TraceHelper.TraceError(ex.TraceInformation()); var dataServiceClientException = ex.InnerException as DataServiceClientException; if (dataServiceClientException != null) { if (dataServiceClientException.StatusCode == 409) { this.questionTable.Delete(questionRows); throw; } } throw; } }
public Tenant GetTenant(string tenant) { Func<Tenant> resolver = () => this.tenantBlobContainer.Get(tenant.ToLowerInvariant()); return this.CacheEnabled ? TenantCacheHelper.GetFromCache(tenant, TenantAccountTag, resolver) : resolver(); }