public async Task <ControllerModels.Topics.EditEventResult> AddEvent(ControllerModels.Topics.EditEventInput input) { var result = new ControllerModels.Topics.EditEventResult(); var topicRecord = DbContext.Topics.Find(input.TopicId); if (topicRecord is null) { throw new HttpNotFoundError(); } if (input.Start is null) { throw new ArgumentException("Parameter cannot be null.", nameof(input.Start)); } if (input.End is null) { throw new ArgumentException("Parameter cannot be null.", nameof(input.End)); } result.TopicId = topicRecord.Id; result.MessageId = topicRecord.FirstMessageId; var eventRecord = new DataModels.Event { TopicId = input.TopicId, Start = (DateTime)input.Start, End = (DateTime)input.End, AllDay = input.AllDay }; DbContext.Add(eventRecord); await DbContext.SaveChangesAsync(); // TODO: Send topic-updated result to hub if topic ID != null. return(result); }
public async Task <IActionResult> EditEvent(ControllerModels.Topics.EditEventInput input) { if (ModelState.IsValid) { var topicRecord = DbContext.Topics.Find(input.TopicId); var eventRecord = DbContext.Events.FirstOrDefault(item => item.TopicId == input.TopicId); if (topicRecord is null || eventRecord is null) { throw new HttpNotFoundError(); } var isOwner = topicRecord.FirstMessagePostedById == UserContext.Id; var isAdmin = UserContext.IsAdmin; if (!isOwner && !isAdmin) { throw new HttpForbiddenError(); } if (input.Start is null || input.End is null) { DbContext.Remove(eventRecord); } else { eventRecord.Start = (DateTime)input.Start; eventRecord.End = (DateTime)input.End; eventRecord.AllDay = input.AllDay; } await DbContext.SaveChangesAsync(); if (ModelState.IsValid) { return(RedirectToAction(nameof(Display), new { id = input.TopicId })); } }
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)); }