public ActionResult Create(Order order) { if (ModelState.IsValid) { try { var existingOrder = db.Orders.Count(o => o.OrderDate == order.OrderDate); if (existingOrder > 0) { throw new Exception("An order with the same date already exists"); } db.Orders.Add(order); db.SaveChanges(); SendEmail(order); return RedirectToAction("Index"); } catch (SqlException sqlex) { ModelState.AddModelError("OrderDate", "Woah, there, cowboy, SQL says - " + sqlex.GetBaseException().GetType().ToString() + " - " + sqlex.GetBaseException().Message); return View(); } catch (Exception ex) { ModelState.AddModelError("OrderDate", "Woah, there, cowboy - " + ex.GetBaseException().GetType().ToString() + " - " + ex.GetBaseException().Message); return View(); } } return View(order); }
private void SendEmail(Order order) { //var client = new RestClient(); //client.BaseUrl = "https://api.mailgun.net/v2"; //client.Authenticator = new HttpBasicAuthenticator("api", "key-46@qffnszp0xztzms0"); //var request = new RestRequest(); //request.AddParameter("domain", "pigbob.mailgun.org", ParameterType.UrlSegment); //request.Resource = "{domain}/messages"; //request.AddParameter("from", "Pigbob <*****@*****.**>"); //request.AddParameter("subject", "New Pig Order!"); //var messageBody = "<html>Hello, a fellow PigBobber has created a new Pig Run for <strong>" + order.OrderDate.ToLongDateString() + "</strong><br/><br/>" + // "Please click <a href=\"http://pigbob.azurewebsites.net/Sandwiches/Index/" + order.OrderID + "\">here</a> to place your order."; //request.AddParameter("html", messageBody); //request.Method = Method.POST; //request.AddParameter("to", "*****@*****.**"); //var result = client.Execute(request); MailMessage message = new MailMessage(); message.From = new MailAddress("*****@*****.**"); message.Subject = "New Pig Order!"; message.IsBodyHtml = true; message.Body = "Hello, a fellow PigBobber has created a new Pig Run for <strong>" + order.OrderDate.ToLongDateString() + "</strong><br/><br/>" + "Please click <a href=\"http://pigbob.azurewebsites.net/Sandwiches/Index/" + order.OrderID + "\">here</a> to place your order."; var members = Membership.GetAllUsers(); foreach (MembershipUser member in members) { message.To.Add(member.Email); } //message.To.Add("*****@*****.**"); SmtpClient smtp = new SmtpClient(); smtp.Send(message); }
public ActionResult Edit(Order order) { if (ModelState.IsValid) { db.Entry(order).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(order); }