示例#1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var session = await _context.GetSessionAsync(SessionID);

            if (session == null)
            {
                return(RedirectToPage("/Error"));
            }

            var fileUpload = new FileUpload
            {
                Category = "Agenda",
                Document = FileUpload
            };

            var document = new Document();

            document = await MemoryStreamExtention.FileToDocumentAsync(fileUpload, document);

            var agenda = new Agenda
            {
                CreationDate = DateTime.Now,
                Name         = Name,
                SessionID    = SessionID,
                Session      = session,
                Document     = document
            };

            var success = await _context.AddAgendaAsync(agenda);

            if (success)
            {
                var protege = await _context.GetProtegeByIDAsync(session.Course.Pair.ProtegeID);

                var mentor = await _context.GetMentorByIDAsync(session.Course.Pair.MentorID);

                var client = await _context.GetClientByIDAsync(session.Course.Pair.ClientID);

                var subject = $"{protege?.AppUser?.Email} uploaded Agenda {agenda.Name}";
                var message = "<h1>Agenda Uploaded</h1>" +
                              $"<p>The Assigment <strong>{agenda.Name}</strong> was submitted by <strong>{protege?.AppUser?.Email}</strong> at <strong>{agenda.CreationDate}</strong></p>";

                if (protege != null)
                {
                    await _emailSender.SendEmailAsync(protege.AppUser.Email, subject, message);
                }
                if (mentor != null)
                {
                    await _emailSender.SendEmailAsync(mentor.AppUser.Email, subject, message);
                }
                if (client != null)
                {
                    await _emailSender.SendEmailAsync(client.AppUser.Email, subject, message);
                }

                return(RedirectToPage("/Sessions/Details", new { id = session.SessionID }));
            }

            return(RedirectToPage("./Index"));
        }