public int AddProduct(int cartId, int productId, int qty) { using (var db = new Entities()) { var product = db.Products.Single(p => p.Id == productId); // Find lineitem with this product. var lineItem = product.LineItems.SingleOrDefault(li => li.CartId == cartId); if (lineItem != null) { lineItem.Qty += qty; } else { lineItem = new LineItem { CartId = cartId, ProductId = productId, Qty = qty, Price = product.Price }; db.LineItems.Add(lineItem); } db.SaveChanges(); return lineItem.Id; } }
public void UpdateShippingCategory(int id, string name) { using (var db = new Entities()) { var category = db.ShippingCategories.Single(c => c.Id == id); category.Name = name; db.Entry(category).State = EntityState.Modified; db.SaveChanges(); } }
public Cart CreateCart() { using (var db = new Entities()) { var cart = new Cart(); db.Carts.Add(cart); db.SaveChanges(); return cart; } }
/// <summary> /// Creates a new page. /// </summary> /// <param name="info"></param> public void CreatePage(NewPageResponse info) { using (var db = new Entities()) { var page = Mapper.Map<Page>(info); page.CreatedAt = DateTime.Now; db.Pages.Add(page); db.SaveChanges(); } }
/// <summary> /// Update product. /// </summary> /// <param name="request"></param> public void UpdateProduct(int id, UpdateProductRequest request) { if (request == null) throw new ArgumentNullException("request"); using(var db = new Llprk.DataAccess.Models.Entities()) { var product = _GetProduct(db, id); Mapper.Map(request, product); db.Entry(product).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } }
/// <summary> /// Update page with given infos. /// </summary> /// <param name="info"></param> public void UpdatePage(EditPageResponse info) { if (info == null) throw new ArgumentNullException("info"); using (var db = new Llprk.DataAccess.Models.Entities()) { var page = _GetPage(db, info.Id); Mapper.Map(info, page); db.Entry(page).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } }
/// <summary> /// Update product. /// </summary> /// <param name="request"></param> public void UpdateProduct(int id, UpdateProductRequest request) { if (request == null) { throw new ArgumentNullException("request"); } using (var db = new Llprk.DataAccess.Models.Entities()) { var product = _GetProduct(db, id); Mapper.Map(request, product); db.Entry(product).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } }
public ShippingCategory CreateShippingCategory(string name) { using (var db = new Entities()) { var cat = new ShippingCategory { Name = name }; db.ShippingCategories.Add(cat); db.SaveChanges(); return cat; } }
public void DeleteShippingCost(int id) { try { using (var db = new Entities()) { var cat = db.ShippingCategories.Single(c => c.Id == id); db.ShippingCategories.Remove(cat); db.SaveChanges(); } } catch (DbUpdateException) { throw new ApplicationException("Shipping category is in use. Cannot delete it."); } }
/// <summary> /// Update page with given infos. /// </summary> /// <param name="info"></param> public void UpdatePage(EditPageResponse info) { if (info == null) { throw new ArgumentNullException("info"); } using (var db = new Llprk.DataAccess.Models.Entities()) { var page = _GetPage(db, info.Id); Mapper.Map(info, page); db.Entry(page).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } }
public void Execute() { using (var db = new Entities()) { // Welche Bestellungen müssen den geändert werden? var ordersToUpdate = db.Orders .Include(o => o.OrderLines) .Include("OrderLines.Product") .Include("OrderLines.Product.ShippingCategory") .Where(o => o.SubTotalPrice == 0 || o.ShippingCosts == 0) .ToList(); var shopService = new ShopService(); // Für die gewählten Bestellungen die Preise berechnen... foreach (var order in ordersToUpdate) { order.SubTotalPrice = shopService.CalculateSubTotalPrice(order); order.ShippingCosts = shopService.CalculateShippingCosts(order); } // ...und speichern. db.SaveChanges(); } }
/// <summary> /// Update the tax for a single country. /// </summary> /// <param name="country"></param> /// <param name="percent"></param> public void UpdateCountryTax(string country, int percent) { using (var db = new Entities()) { var tax = db.Taxes.SingleOrDefault(t => t.CountryId == country); if (tax == null) { tax = new Tax { CountryId = country, Percent = percent }; db.Taxes.Add(tax); } else { tax.Percent = percent; } db.SaveChanges(); } }
/// <summary> /// Markiert einen Auftrag als bezahlt. /// </summary> /// <param name="order"></param> public void PayOrder(Entities db, Order order, string mailBody=null) { if (order == null) { throw new ArgumentNullException("order"); } order.PaidAt = DateTime.Now; // TODO: Vielleicht hat der Kunde schon vorher gezahlt? // Wenn kein Bestätigungs-Mail-Body angegeben wurde, // müssen wir den selber erstellen. var body = mailBody == null ? Nustache.Core.Render.StringToString(db.Parameters.First().MailMessagePaid, order) : mailBody; // Email an den Kunden schicken. MailService.SendMailToCustomer(order.Email, "Wir haben Deine Bezahlung erhalten", body); db.SaveChanges(); }
public void UpdateLineItemQty(int cartId, UpdateLineItemQtyRequest[] updates) { using (var db = new Entities()) { foreach (var update in updates) { var lineItem = db.LineItems.Single(l => l.Id == update.Id); if (update.Qty == 0) { db.LineItems.Remove(lineItem); } else { lineItem.Qty = update.Qty; db.Entry(lineItem).State = System.Data.Entity.EntityState.Modified; } } db.SaveChanges(); } }
/// <summary> /// Markiert einen Auftrag als verschickt. /// </summary> /// <param name="order"></param> public void ShipOrder(Entities db, Order order, string mailBody) { if (order == null) { throw new ArgumentNullException("order"); } order.ShippedAt = DateTime.Now; // Email an den Kunden schicken. MailService.SendMailToCustomer(order.Email, "Deine Bestellung wurde verschickt", mailBody); db.SaveChanges(); }
public void UpdateShippingCosts(UpdateShippingCostsRequest data) { using (var db = new Entities()) { foreach (var country in data.Countries) { foreach (var cost in country.ShippingCosts) { var shippingCost = db.ShippingCosts.SingleOrDefault(sc => sc.CountryId == country.Id && sc.ShippingCategoryId == cost.ShippingCategoryId); if (shippingCost != null) { shippingCost.Amount = cost.Amount; shippingCost.AdditionalAmount = cost.AdditionalAmount; db.Entry(shippingCost).State = EntityState.Modified; } else { shippingCost = new ShippingCost { CountryId = country.Id, ShippingCategoryId = cost.ShippingCategoryId, Amount = cost.Amount, AdditionalAmount = cost.AdditionalAmount }; db.ShippingCosts.Add(shippingCost); } } } db.SaveChanges(); } }
/// <summary> /// Place a new order from an existing cart. /// </summary> /// <param name="cartId"></param> /// <param name="request"></param> /// <returns></returns> public Order CreateOrder(int cartId, CreateOrderRequest request) { // Validate request. var vc = new ValidationContext(request); Validator.ValidateObject(request, vc, true); lock (_orderLock) // Only one order at a time. { using (var tx = new TransactionScope()) { using (var db = new Entities()) { // Get cart. var cart = db.Carts .Include(i => i.LineItems.Select(l => l.Product)) .Single(c => c.Id == cartId); if (cart.OrderId.HasValue) { throw new ApplicationException(string.Format( "Dieser Warenkorb ({0} wurde bereits bestellt!", cartId)); } // Calcualte costs and taxes. var shippingCosts = _ShippingService.CalculateShippingCosts(cartId, request.DeliveryAddress.CountryId); var tax = _TaxService.TaxForCountry(cartId, request.DeliveryAddress.CountryId); var da = request.DeliveryAddress; // Create new Order. var order = new Order { // Adressen Address1 = da.Address1, Address2 = da.Address2, City = da.City, CountryId = da.CountryId, Email = da.Email, Firstname = da.Firstname, Name = da.Lastname, Salutation = da.Salutation, Zip = da.Zip, ShippingCosts = shippingCosts, Tax = tax, SubTotalPrice = cart.Subtotal, Total = cart.Subtotal + shippingCosts + tax, CreatedAt = DateTime.UtcNow }; // Order speicher. db.Orders.Add(order); db.SaveChanges(); // Alle Lineitems der Order zuweisen. foreach (var lineItem in cart.LineItems) { if (lineItem.Product == null) { throw new ApplicationException(string.Format( "Das Produkt mit der Id {0} ist nicht verfügbar!", lineItem.ProductId)); } if (lineItem.Product.Available < lineItem.Qty) { throw new ApplicationException(string.Format( "Von {0} ist/sind nur noch {1} Stück auf Lager. Bitte Menge anpassen.", lineItem.Product.Name, lineItem.Product.Available)); } lineItem.Product.Available -= lineItem.Qty; lineItem.OrderId = order.Id; } // Mark cart as ordered. cart.OrderId = order.Id; // Bestätigungsmail verschicken. //var mailBody = Nustache.Core.Render.StringToString(db.Parameters.First().MailMessageOrdered, order); //MailService.SendMailToCustomer(order.Email, "Deine Bestellung bei lillypark.com", mailBody); // Commit! tx.Complete(); return order; } } } }
/// <summary> /// Bestellung abgeben. /// </summary> /// <param name="order"></param> /// <param name="productIdsAndQtys"></param> public void PlaceOrder(Entities db, Order order, IDictionary<int, int> productIdsAndQtys) { using (var transaction = new TransactionScope()) { // Bestellung mit Datum versehen und in DB speichern. order.CreatedAt = DateTime.Now; // Die Bestellung muss erstmal ohne Bestellzeilen gespeichert werden, // damit wir eine BestellID aus der DB haben. db.Orders.Add(order); db.SaveChanges(); foreach (var id in productIdsAndQtys.Keys) { var product = db.Products .Include(p => p.ShippingCategory) // Brauchen wird für die Versandkostenberechnung. .FirstOrDefault(x => x.Id == id); var qty = productIdsAndQtys[id]; if (product == null) { throw new AppException(string.Format( "Das Produkt mit der Id {0} ist nicht verfügbar!", id)); } // Nicht mehr genug vom Produkt auf Lager? if (product.Available < qty) { throw new AppException(string.Format( "Von {0} ist/sind nur noch {1} Stück auf Lager. Bitte Menge anpassen.", product.Name, product.Available)); } // Menge abziehen. product.Available -= qty; // Produkte zur Bestellung hinzufügen. order.OrderLines.Add(new OrderLine() { OrderId = order.Id, ProductId = id, Product = product, Qty = qty }); } // ...speichern. db.SaveChanges(); // Preis berechnen order.SubTotalPrice = CalculateSubTotalPrice(order); // Versandkosten berechnen order.ShippingCosts = CalculateShippingCosts(order); // Validierung. var vc = new ValidationContext(order, null, null); Validator.ValidateObject(order, vc, true); // ...speichern. db.SaveChanges(); // Bestätigungsmail verschicken. var mailBody = Nustache.Core.Render.StringToString(db.Parameters.First().MailMessageOrdered, order); MailService.SendMailToCustomer(order.Email, "Deine Bestellung bei lillypark.com", mailBody); transaction.Complete(); } // Benachrichtgung an den Shopinhaber, dass eine neue // Bestellung eingegangen ist. // INFO: Wenn das Verschicken fehlschlägt, soll die Bestellung dennoch angenommen werden. var ownerMailBody = string.Format(@" Hi Lilly/Hoonie, guckst Du <a href=""http://lillypark.com/orders/details/{0}"">hier</a>. Liebe Grüße, Dein WebShop", order.Id); MailService.SendMailToOwner("Eine neue Bestellung ist eingegangen.", ownerMailBody); }
private void setShippingCosts(Entities db, string countryId, int shippingCategoryId, decimal amount, decimal additionalAmount) { var costs = db.ShippingCosts.SingleOrDefault(sc => sc.CountryId == countryId && sc.ShippingCategoryId == shippingCategoryId); if (costs == null) { costs = new ShippingCost { CountryId = countryId, ShippingCategoryId = shippingCategoryId }; } costs.Amount = amount; costs.AdditionalAmount = additionalAmount; db.SaveChanges(); }