Ejemplo n.º 1
0
        public ActionResult AddCoachRequest()
        {
            String     result = "Coach permission has been requested.";
            DBAccessor dba    = new DBAccessor();

            // Add request to the DB
            string email     = User.Identity.Name;
            Person requestee = dba.GetPersonInformation(email);

            if (requestee != null)
            {
                if (dba.AddNewRequest(requestee.ID, RequestType.COACH_PERMISSION))
                {
                    // Send email that a request was added
                    try {
                        // Form an email
                        MailMessage newMessage  = new MailMessage();
                        SmtpClient  mailService = new SmtpClient();

                        //set the addresses
                        newMessage.From = new MailAddress(AppConstants.EMAIL_ADMIN);
                        newMessage.To.Add(AppConstants.EMAIL_ADMIN);

                        //set the content
                        newMessage.Subject = "Coach Permission Requested";
                        newMessage.Body    = requestee.firstName + " " + requestee.lastName + " has requested coach access (email: " + requestee.email + ").";

                        //send the message
                        mailService.UseDefaultCredentials = false;
                        mailService.DeliveryMethod        = SmtpDeliveryMethod.Network;
                        mailService.Host        = AppConstants.EMAIL_SMTP_ADDRESS;
                        mailService.Credentials = new NetworkCredential(AppConstants.EMAIL_SMTP_USERNAME, AppConstants.EMAIL_SMTP_PASSWORD);
                        mailService.Send(newMessage);
                    }
                    catch (Exception) {
                        result = "Error notifying the site administrator.";
                    }
                }
                else
                {
                    result = "Couldn't add a request to the database.";
                }
            }
            else
            {
                result = "Couldn't find the user in the database.";
            }

            return(Json(
                       new { message = result },
                       JsonRequestBehavior.AllowGet
                       ));
        }