Ejemplo n.º 1
0
        public virtual JsonResult Meetings_Destroy([DataSourceRequest] DataSourceRequest request, MeetingViewModel meeting)
        {
            if (ModelState.IsValid)
            {
                var currentOwnerId = _attendeeService.GetAttendeeIdByEmail(email);
                var meetingOwnerId = _meetingService.GetAll().SingleOrDefault(m => m.MeetingID == meeting.MeetingID).OwnerID;

                if (currentOwnerId == meetingOwnerId)
                {
                    _meetingService.Delete(meeting, ModelState);

                    foreach (var attendeeId in meeting.Attendees.ToList())
                    {
                        var recepient = _attendeeService.GetAttendeeById(attendeeId);
                        var sender    = _attendeeService.GetAttendeeByEmail(email);

                        var message = new MimeMessage();
                        message.From.Add(new MailboxAddress(sender.Name, sender.Email));
                        message.To.Add(new MailboxAddress(recepient.Name, recepient.Email));
                        message.Subject = "Meeting Cancelled.";
                        message.Body    = new TextPart(TextFormat.Html)
                        {
                            Text = $"Hi {recepient.Name}, " +
                                   $"{sender.Name} cancelled the meeting of {new MeetingRooms().GetRooms().SingleOrDefault(m => m.RoomID == meeting.RoomID).RoomName} meeting room."
                        };

                        using (var client = new SmtpClient())
                        {
                            client.Connect("smtp.office365.com", 587, false);
                            client.Authenticate(sender.Email, service.Decrypt(sender.Password, sender.Salt));
                            client.Send(message);
                            client.Disconnect(true);
                        }
                    }
                }
            }

            return(Json(new[] { meeting }.ToDataSourceResult(request, ModelState)));
        }