public void SaveTenant(Tenant tenant)
        {
            this.tenantBlobContainer.Save(tenant.Name.ToLowerInvariant(), tenant);

            if (this.CacheEnabled)
            {
                TenantCacheHelper.AddToCache(tenant.Name, TenantAccountTag, tenant);
            }
        }
Esempio n. 2
0
        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
            };

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