public async Task Execute(PangulDbContext db, UpdateQuestion command)
        {
            command.Validate();

            // Get existing question
            var question = await _getQuestion.Execute(db, new GetQuestion()
            {
                UserContext     = command.UserContext,
                LightWeightOnly = false,
                QuestionId      = command.QuestionId,
                RowVersion      = command.RowVersion,
            });

            if (question == null)
            {
                throw new PangulCommandFailedException(CommandFailureType.MissingData, $"No such question ({command.QuestionId}, {command.RowVersion})");
            }

            // Verify user has permission
            await _internalUserPermissionService.RequireWriteAccessFor(question, command.UserContext);

            // Get tag lists
            var tags     = command.Tags ?? new string[0];
            var newTags  = tags.Where(i => question.Tags.All(j => j.Tag != i) && !string.IsNullOrWhiteSpace(i));
            var dropTags = question.Tags.Where(i => tags.All(j => j != i.Tag));

            // Update properties
            command.ApplyTo(question);

            // Drop old tags
            foreach (var tag in dropTags)
            {
                db.QuestionTag.Remove(tag);
            }

            // Create tags for those not present
            await db.QuestionTag.AddRangeAsync(newTags.Select(t => new QuestionTag
            {
                Tag        = t,
                QuestionId = question.QuestionId
            }));
        }
Example #2
0
        public async Task PurgeExistingAnswer(PangulDbContext db, UserContext user, string answerId, DataBackupConfiguration backupConfiguration)
        {
            var answer = await _answerService.GetAnswer(db, user, answerId);

            if (answer == null)
            {
                throw new PangulCommandFailedException(CommandFailureType.MissingData, $"No such answer ({answerId})");
            }

            await _internalUserPermissionService.RequireWriteAccessFor(answer, user);

            _backupService.Backup(answer, backupConfiguration);

            await _purgeAnswer.Execute(db, new PurgeAnswerData()
            {
                Answer      = answer,
                UserContext = user
            });
        }
Example #3
0
        public async Task Execute(PangulDbContext db, UpdateAnswer command)
        {
            command.Validate();

            // Fetch the answer
            var answer = await _getAnswer.Execute(db, new GetAnswer()
            {
                UserContext = command.UserContext,
                AnswerId    = command.AnswerId,
                RowVersion  = command.RowVersion
            });

            if (answer == null)
            {
                throw new PangulCommandFailedException(CommandFailureType.MissingData, $"No such answer ({command.AnswerId}, {command.RowVersion})");
            }

            // Verify user has permission
            await _permissionService.RequireWriteAccessFor(answer, command.UserContext);

            // Update the answer
            answer.Body = command.NewBody;
        }