public async Task <IActionResult> AddThought(InputModels.ThoughtInput input) { if (ModelState.IsValid) { var serviceResponse = await MessageRepository.AddThought(input); return(await ForumViewResult.RedirectFromService(this, serviceResponse, FailureCallback)); } return(await FailureCallback()); async Task <IActionResult> FailureCallback() { return(await Task.Run(() => { return ForumViewResult.RedirectToReferrer(this); })); } }
public async Task <ServiceModels.ServiceResponse> AddThought(InputModels.ThoughtInput input) { var serviceResponse = new ServiceModels.ServiceResponse(); var messageRecord = DbContext.Messages.Find(input.MessageId); if (messageRecord is null) { serviceResponse.Error($@"No message was found with the id '{input.MessageId}'"); } var smileyRecord = await DbContext.Smileys.FindAsync(input.SmileyId); if (messageRecord is null) { serviceResponse.Error($@"No smiley was found with the id '{input.SmileyId}'"); } if (!serviceResponse.Success) { return(serviceResponse); } var existingRecord = await DbContext.MessageThoughts .SingleOrDefaultAsync(mt => mt.MessageId == messageRecord.Id && mt.SmileyId == smileyRecord.Id && mt.UserId == UserContext.ApplicationUser.Id); if (existingRecord is null) { var messageThought = new DataModels.MessageThought { MessageId = messageRecord.Id, SmileyId = smileyRecord.Id, UserId = UserContext.ApplicationUser.Id }; DbContext.MessageThoughts.Add(messageThought); if (messageRecord.PostedById != UserContext.ApplicationUser.Id) { var notification = new DataModels.Notification { MessageId = messageRecord.Id, UserId = messageRecord.PostedById, TargetUserId = UserContext.ApplicationUser.Id, Time = DateTime.Now, Type = Enums.ENotificationType.Thought, Unread = true, }; DbContext.Notifications.Add(notification); } } else { DbContext.Remove(existingRecord); var notification = DbContext.Notifications.FirstOrDefault(item => item.MessageId == existingRecord.MessageId && item.TargetUserId == existingRecord.UserId && item.Type == Enums.ENotificationType.Thought); if (notification != null) { DbContext.Remove(notification); } } DbContext.SaveChanges(); serviceResponse.RedirectPath = UrlHelper.DirectMessage(input.MessageId); return(serviceResponse); }