Пример #1
0
        public async Task <IActionResult> CreateEvent(ControllerModels.Topics.EditEventInput input)
        {
            if (ModelState.IsValid)
            {
                if (input.TopicId >= 0)
                {
                    var topicRecord = DbContext.Topics.FirstOrDefault(item => item.Id == input.TopicId);

                    if (topicRecord is null)
                    {
                        throw new HttpNotFoundError();
                    }

                    if (input.Start is null)
                    {
                        return(RedirectToAction(nameof(Topics.Display), new { id = topicRecord.Id }));
                    }

                    var isOwner = topicRecord.FirstMessagePostedById == UserContext.Id;
                    var isAdmin = UserContext.IsAdmin;

                    if (!isOwner && !isAdmin)
                    {
                        throw new HttpForbiddenError();
                    }

                    var result = await TopicRepository.AddEvent(input);

                    ModelState.AddModelErrors(result.Errors);

                    if (ModelState.IsValid)
                    {
                        var redirectPath = Url.DisplayMessage(result.TopicId, result.MessageId);
                        return(Redirect(redirectPath));
                    }
                }
                else
                {
                    var createTopicViewModel = new ViewModels.Topics.CreateTopicForm {
                        Start          = input.Start,
                        End            = input.End,
                        AllDay         = input.AllDay,
                        SelectedBoards = JsonConvert.DeserializeObject <List <int> >(input.SelectedBoards)
                    };

                    return(View(nameof(Create), createTopicViewModel));
                }
            }

            var editEventViewModel = new ViewModels.Topics.EditEventForm {
                FormAction     = nameof(Topics.CreateEvent),
                FormController = nameof(Topics),
                Start          = input.Start,
                End            = input.End,
                AllDay         = input.AllDay,
                TopicId        = input.TopicId,
                Body           = input.Body,
                SelectedBoards = JsonConvert.SerializeObject(input.SelectedBoards)
            };

            return(View("EditEvent", editEventViewModel));
        }