public void TestCreateTimetablesDocx() { Course course; using (var db = new MedSimDbContext()) { var testId = Guid.Parse("0237b617-022c-4bed-8ce3-48356a86e63f");//"f1afbbbb-b72f-43f4-8b36-7837fe8d1b80" course = CreateDocxTimetable.GetCourseIncludes(db).First(c => c.Id == testId); using (var stream = CreateDocxTimetable.CreateTimetableDocx(course, templ, true)) { using (var fileStream = new FileStream("testCourseFacultyTimetable.docx", FileMode.Create)) { stream.WriteTo(fileStream); } } using (var stream = CreateDocxTimetable.CreateTimetableDocx(course, templ, false)) { using (var fileStream = new FileStream("testCourseParticipantTimetable.docx", FileMode.Create)) { stream.WriteTo(fileStream); } } } }
public static DbQuery <Course> GetCourseIncludes(MedSimDbContext repo) { return(CreateDocxTimetable.GetCourseIncludes(repo) .Include("CourseParticipants.Department.Institution.Culture") .Include("Room") .Include("FacultyMeetingRoom") .Include("CourseFormat.CourseType.CandidatePrereadings")); }
public async Task <HttpResponseMessage> GetTimetableForCourse([FromUri] DowloadFileSetModel model) { var validation = await ValidateInput(model); if (validation != null) { return(validation); } var courses = GetPermittedEntity(CreateDocxTimetable.GetCourseIncludes(Repo)); var course = await courses.FirstAsync(c => c.Id == model.EntitySetId); //todo - check if a faculty member, otherwise download participant timetable return(StreamToResponse( CreateDocxTimetable.CreateTimetableDocx(course, WebApiConfig.DefaultTimetableTemplatePath, true), CreateDocxTimetable.TimetableName(course))); }
/// <summary> /// /// </summary> /// <param name="course"></param> /// <returns>a lookup true = successfully emailed, false = email failed</returns> public static async Task <EmailResult <CourseParticipant> > SendCourseEmail(Course course, ApplicationUserManager userManager, DateTime?originalDate = null) { var map = ((Expression <Func <CourseParticipant, CourseParticipantDto> >) new CourseParticipantMaps().MapToDto).Compile(); var faculty = course.CourseParticipants.Where(cp => map(cp).IsEmailed == originalDate.HasValue) .ToLookup(cp => cp.IsFaculty); var attachments = new List <Attachment>(); string timetableName = CreateDocxTimetable.TimetableName(course); var participantTimetable = CreateDocxTimetable.CreateTimetableDocx(course, WebApiConfig.DefaultTimetableTemplatePath, false); if (course.CourseFormat.CourseType.SendCandidateTimetable) { attachments.Add(new Attachment(Stream.Synchronized(participantTimetable), OpenXmlDocxExtensions.DocxMimeType) { Name = timetableName }); } attachments.AddRange(GetInviteReadings(course)); var success = new ConcurrentBag <CourseParticipant>(); var fail = new ConcurrentBag <CourseParticipant>(); using (var parallelEmails = new ParallelSmtpEmails(disposeMsgOnComplete: false)) { List <MailMessage> messages = new List <MailMessage>(course.CourseParticipants.Count); var sendMail = new Func <CourseParticipant, Task>(async cp => { var mail = new MailMessage(); messages.Add(mail); mail.To.AddParticipants(cp.Participant); string token = await userManager.GenerateUserTokenAsync(InvitePurpose(cp.CourseId), cp.ParticipantId); var confirmEmail = new CourseInvite { CourseParticipant = cp, OldStart = originalDate, Token = token }; mail.CreateHtmlBody(confirmEmail); foreach (var a in attachments) { //a.ContentStream.Position = 0; mail.Attachments.Add(a); } parallelEmails.Send(mail, s => { if (s == null) { success.Add(cp); } else { fail.Add(cp); } }); }); foreach (var f in faculty[false]) { await sendMail(f); } MemoryStream facultyTimetable = null; if (faculty[true].Any()) { attachments = new List <Attachment>(); facultyTimetable = CreateDocxTimetable.CreateTimetableDocx(course, WebApiConfig.DefaultTimetableTemplatePath, true); attachments.Add(new Attachment(Stream.Synchronized(facultyTimetable), OpenXmlDocxExtensions.DocxMimeType) { Name = timetableName }); attachments.AddRange(GetFilePaths(course) .Select(fp => new Attachment(Stream.Synchronized(fp.Value.ToStream()), System.Net.Mime.MediaTypeNames.Application.Zip) { Name = fp.Key })); foreach (var f in faculty[true]) { await sendMail(f); } } await parallelEmails.SendingComplete(); messages.ForEach(m => m.Dispose()); participantTimetable.Dispose(); facultyTimetable?.Dispose(); } return(new EmailResult <CourseParticipant> { SuccessRecipients = success.ToArray(), FailRecipients = fail.ToArray() }); }