public CrumbController(ICrumbsRepository crumbsRepository)
 {
     _crumbsRepository = crumbsRepository;
 }
Exemplo n.º 2
0
        public static bool ProcessOrder(ICrumbsRepository crumbsRepository, Invoice invoice, string serverPath, string email, bool updateCopies)
        {
            List<MidiFile> files = new List<MidiFile>();
            User user = (crumbsRepository as IUserRepository).FindUserByID(invoice.UserID);
            Mailing mail = crumbsRepository.AllMailings.SingleOrDefault(item => item.Name == "Send_MIDI");

            if (user.UserID == 20)
            {
                User newUser = (crumbsRepository as IUserRepository).FindUserByEmail(email);
                if (newUser == null)
                {
                    newUser = new User()
                    {
                        Type = 3,
                        UserKey = email,
                        UserName = email,
                        Email = email,
                        CreateDate = DateTime.Now,
                        LastLoginDate = DateTime.Now
                    };
                    (crumbsRepository as IUserRepository).InsertOrUpdateUser(newUser);
                    crumbsRepository.Save();
                }
                user = newUser;
            }

            if (user == null || mail == null)
                return false;

            string userEmail = user.Email;
            if (string.IsNullOrEmpty(userEmail))
                userEmail = email;

            if (string.IsNullOrEmpty(userEmail))
                return false;

            SmtpClient client = null;
            try
            {
                bool useGMailForOrders = false;
                string s = ConfigurationManager.AppSettings["UseGMailForOrders"];
                if (string.IsNullOrEmpty(s) == false)
                {
                    useGMailForOrders = Convert.ToBoolean(s);
                }

                if (useGMailForOrders == true)
                {
                    client = new SmtpClient("smtp.gmail.com", 587);
                    client.EnableSsl = true;
                    client.Timeout = 10000;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = false;
                    client.Credentials = new NetworkCredential("*****@*****.**", "Crumbs123");
                }
                else
                {
                    client = new SmtpClient("m02.internetmailserver.net", 587);
                    client.EnableSsl = false;
                    client.Credentials = new NetworkCredential("*****@*****.**", "Crumbs123", "");
                }

                MailAddress from = new MailAddress("*****@*****.**", "PianoCrumbs.com");
                MailAddress to = new MailAddress(userEmail, "");
                MailMessage message = new MailMessage(from, to);

                message.Subject = mail.Subject;
                message.IsBodyHtml = true;
                message.CC.Add(new MailAddress("*****@*****.**", "PianoCrumbs.com"));

                if (invoice.MidiID.HasValue)
                {
                    MidiFile file = crumbsRepository.FindMidiFile(invoice.MidiID.Value);
                    if (file != null)
                    {
                        string path = MidiFileHelper.getMidiPath(file);
                        string FullPath = serverPath + path;

                        Attachment attachment = new Attachment(FullPath);
                        attachment.Name = file.FileName;
                        message.Attachments.Add(attachment);

                        files.Add(file);
                    }
                }
                else if (invoice.PackageID.HasValue)
                {
                    Package package = crumbsRepository.FindPackage(invoice.PackageID.Value);
                    if (package != null)
                    {
                        foreach (MidiFile file in package.MidiFiles)
                        {
                            string path = MidiFileHelper.getMidiPath(file);
                            string FullPath = serverPath + path;

                            Attachment attachment = new Attachment(FullPath);
                            attachment.Name = file.FileName;
                            message.Attachments.Add(attachment);

                            files.Add(file);
                        }
                    }
                }
                message.Body = string.Format(mail.Text, user.UserName, "", "", "");

                client.Send(message);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    invoice.Comment = ex.InnerException.Message + " | " + DateTime.Now.ToString("s");
                }
                else
                {
                    invoice.Comment = ex.Message + " | " + DateTime.Now.ToString("s");
                }
                crumbsRepository.InsertOrUpdateInvoice(invoice);
                crumbsRepository.Save();

                return false;
            }

            // Create new SentMail record
            SentMail sent = new SentMail();
            sent.UserID = user.UserID;
            sent.MailID = mail.ID;
            sent.Date = DateTime.Now;
            crumbsRepository.InsertOrUpdateSentMail(sent);

            if (user.Role == null && updateCopies == true)
            {
                // Update MidiFile record - substruct one copy
                foreach (MidiFile file in files)
                {
                    file.CopiesLeft -= 1;
                    crumbsRepository.InsertOrUpdateMidiFile(file);
                }
            }

            // Update Invoice with SentMail ID
            invoice.Comment = userEmail + " | " + DateTime.Now.ToString("s");
            invoice.UserID = user.UserID;
            crumbsRepository.InsertOrUpdateInvoice(invoice);

            crumbsRepository.Save();
            return true;
        }