Esempio n. 1
0
        public Category ArchiveCategory(IRepository repository, int id, Question questionToEdit)
        {
            var oldVersion = repository.OfType<Category>().Queryable.Where(a => a.Id == id).Single();
            var newVersion = new Category(oldVersion.Survey);

            newVersion.Rank = oldVersion.Rank;
            newVersion.LastUpdate = DateTime.Now;
            newVersion.CreateDate = newVersion.LastUpdate;

            foreach (var categoryGoal in oldVersion.CategoryGoals)
            {
                var categoryGoalToDuplicate = new CategoryGoal();
                Mapper.Map(categoryGoal, categoryGoalToDuplicate);
                newVersion.AddCategoryGoal(categoryGoalToDuplicate);
            }
            foreach (var question in oldVersion.Questions)
            {
                if (question.Id == questionToEdit.Id)
                {
                    //Ignore
                }
                else
                {
                    var questionToDuplicate = new Question(oldVersion.Survey);
                    questionToDuplicate.Order = question.Order;
                    questionToDuplicate.PrimaryPhoto = question.PrimaryPhoto;
                    foreach (var response in question.Responses)
                    {
                        var newResponse = new Response(); //If I don't do this, the old responses are *moved* here, not copied
                        Mapper.Map(response, newResponse);
                        questionToDuplicate.AddResponse(newResponse);
                    }
                    foreach (var photo in question.Photos) //This should work, but it needs to be tested.
                    {
                        var newPhoto = repository.OfType<Photo>().Queryable.Single(a => a.Id == photo.Id);
                        questionToDuplicate.Photos.Add(newPhoto);
                    }
                    Mapper.Map(question, questionToDuplicate);
                    newVersion.AddQuestions(questionToDuplicate);
                }

            }

            newVersion.IsActive = oldVersion.IsActive;
            newVersion.Name = oldVersion.Name;
            newVersion.Affirmation = oldVersion.Affirmation;
            newVersion.Encouragement = oldVersion.Encouragement;
            newVersion.DoNotUseForCalculations = oldVersion.DoNotUseForCalculations;
            newVersion.PreviousVersion = oldVersion;

            //*******************  SAVE
            repository.OfType<Category>().EnsurePersistent(newVersion);
            //*******************  SAVE

            oldVersion = repository.OfType<Category>().GetNullableById(id);
            oldVersion.IsCurrentVersion = false;

            //*******************  SAVE
            repository.OfType<Category>().EnsurePersistent(oldVersion);
            //*******************  SAVE

            return newVersion;
        }