예제 #1
0
        public async Task <IActionResult> XhrEdit(ControllerModels.Messages.EditInput input)
        {
            var message = await DbContext.Messages.FirstOrDefaultAsync(m => m.Id == input.Id);

            if (message is null || message.Deleted)
            {
                throw new HttpNotFoundError();
            }

            if (message.PostedById != UserContext.ApplicationUser.Id && !UserContext.IsAdmin)
            {
                throw new HttpForbiddenError();
            }

            if (ModelState.IsValid)
            {
                var result = await MessageRepository.EditMessage(input);

                ModelState.AddModelErrors(result.Errors);

                if (ModelState.IsValid)
                {
                    await ForumHub.Clients.All.SendAsync("updated-message", new HubModels.Message {
                        TopicId   = result.TopicId,
                        MessageId = result.MessageId
                    });

                    return(Ok());
                }
            }

            var errors = ModelState.Keys.Where(k => ModelState[k].Errors.Count > 0).Select(k => new { propertyName = k, errorMessage = ModelState[k].Errors[0].ErrorMessage });

            return(new JsonResult(errors));
        }
예제 #2
0
        public async Task <ControllerModels.Messages.EditResult> EditMessage(ControllerModels.Messages.EditInput input)
        {
            var result = new ControllerModels.Messages.EditResult();

            if (input.Id == 0)
            {
                throw new HttpBadRequestError();
            }

            var record = DbContext.Messages.FirstOrDefault(m => m.Id == input.Id);

            if (record is null || record.Deleted)
            {
                result.Errors.Add(nameof(input.Id), $"No record found with the ID '{input.Id}'.");
            }

            result.MessageId = record.Id;
            result.TopicId   = record.TopicId;

            var processedMessage = await ProcessMessageInput(input.Body);

            foreach (var error in processedMessage.Errors)
            {
                result.Errors.Add(error.Key, error.Value);
            }

            if (!(result.Errors.Any()))
            {
                await UpdateMessageRecord(processedMessage, record);

                var topic = DbContext.Topics.Find(record.TopicId);

                if (record.Id == topic.FirstMessageId)
                {
                    topic.FirstMessageShortPreview = record.ShortPreview;
                    DbContext.Update(topic);
                }

                if (record.Id == topic.LastMessageId)
                {
                    topic.LastMessageShortPreview = record.ShortPreview;
                    DbContext.Update(topic);
                }

                await DbContext.SaveChangesAsync();
            }

            return(result);
        }
예제 #3
0
        public async Task <IActionResult> Edit(ControllerModels.Messages.EditInput input)
        {
            var message = await DbContext.Messages.FirstOrDefaultAsync(m => m.Id == input.Id);

            if (message is null || message.Deleted)
            {
                throw new HttpNotFoundError();
            }

            if (message.PostedById != UserContext.ApplicationUser.Id && !UserContext.IsAdmin)
            {
                throw new HttpForbiddenError();
            }

            if (ModelState.IsValid)
            {
                var result = await MessageRepository.EditMessage(input);

                ModelState.AddModelErrors(result.Errors);

                if (ModelState.IsValid)
                {
                    await ForumHub.Clients.All.SendAsync("updated-message", new HubModels.Message {
                        TopicId   = result.TopicId,
                        MessageId = result.MessageId
                    });

                    var redirectPath = Url.DisplayMessage(result.TopicId, result.MessageId);
                    return(Redirect(redirectPath));
                }
            }

            var viewModel = new ViewModels.Messages.EditMessageForm {
                Id        = input.Id.ToString(),
                Body      = input.Body,
                ElementId = $"edit-message-{input.Id}"
            };

            return(await ForumViewResult.ViewResult(this, viewModel));
        }