/// <summary> /// Updates the latest draft of a message for a given author. /// </summary> private async Task <Message> UpdateDraftAsync( Conversation conversation, int authorId, string messageContents, bool send, IList <Attachment> attachments = null) { var message = conversation.Messages .SingleOrDefault(m => m.AuthorId == authorId && !m.Sent.HasValue); bool existingMessage = message != null; if (message == null) { message = new Message { ConversationId = conversation.Id, AuthorId = authorId, Author = await _dbContext.Users .Where(u => u.Id == authorId) .SingleAsync() }; } message.Contents = _htmlSanitizer.SanitizeHtml(messageContents); if (attachments != null) { message.Attachments = attachments; } if (send) { message.Sent = _timeProvider.UtcNow; } if (existingMessage) { _dbContext.Update(message); } else { _dbContext.Add(message); } return(message); }
/// <summary> /// Publish a new announcement. /// </summary> public async Task <bool> PostAnnouncementAsync( string classroomName, int userId, Announcement announcement, Func <DateTime, string> formatDateTime, IModelErrorCollection modelErrors) { var classroom = await _dbContext.Classrooms .Where(c => c.Name == classroomName) .Include(c => c.Sections) .SingleAsync(); var sectionIds = announcement.Sections ?.Select(s => s.SectionId) ?.ToList() ?? new List <int>(); if (!_validator.ValidateAnnouncement(classroom, announcement, modelErrors)) { return(false); } announcement.ClassroomId = classroom.Id; announcement.UserId = userId; announcement.Contents = _htmlSanitizer.SanitizeHtml(announcement.Contents); announcement.DatePosted = _timeProvider.UtcNow; _dbContext.Announcements.Add(announcement); await _dbContext.SaveChangesAsync(); await SendAnnouncementEmailAsync ( announcement, classroom, sectionIds, formatDateTime, emailAdmins : true ); return(true); }