Engage.Survey.Entities.SurveyRepository.DeleteSurvey C# (CSharp) Method

DeleteSurvey() public method

Deletes the specified survey.
public DeleteSurvey ( int surveyId, int moduleId ) : void
surveyId int The ID of the survey to delete.
moduleId int The ID of the module in which to find the survey.
return void
        public void DeleteSurvey(int surveyId, int moduleId)
        {
            var survey = this.LoadSurvey(surveyId, moduleId);

            this.Context.Surveys.DeleteOnSubmit(survey);
            this.Context.Sections.DeleteAllOnSubmit(survey.Sections);
            var questions = survey.Sections.SelectMany(section => section.Questions);
            this.Context.Questions.DeleteAllOnSubmit(questions);
            var answers = questions.SelectMany(question => question.Answers);
            this.Context.Answers.DeleteAllOnSubmit(answers);
            var responses = this.Context.Responses.Where(response => response.SurveyId == surveyId);
            this.Context.Responses.DeleteAllOnSubmit(responses);
            var responseHeaders = responses.Select(response => response.ResponseHeader);
            this.Context.ResponseHeaders.DeleteAllOnSubmit(responseHeaders);

            this.Context.SubmitChanges();
        }

Usage Example

        public void DeleteSurvey(int surveyId)
        {
            var surveyRepository = new SurveyRepository();
            var moduleId = surveyRepository.GetModuleIdForSurvey(surveyId);
            if (!this.CanEditModule(moduleId))
            {
                this.DenyAccess();
            }

            surveyRepository.DeleteSurvey(surveyId, moduleId);
        }