/// <summary> /// Creates a product according to options and adds it to the ProductList /// </summary> /// <param name="options"></param> /// <returns></returns> public bool AddProduct(AddProductOptions options) { if (options == null) { return(false); } var check = GetProductById(options.Id); if (check != null) { return(false); } if (string.IsNullOrWhiteSpace(options.Name)) { return(false); } if (options.Price <= 0) { return(false); } if (options.Category == Model.ProductCategory.Invalid) { return(false); } var product = new Product() { Id = options.Id, Name = options.Name, Price = options.Price, Category = options.Category }; context_.Add(product); var success = false; try { success = context_.SaveChanges() > 0; } catch (Exception e) { } return(success); }
public Order Customer_Order_Success() { var p = new Product() { Id = $"213{DateTime.Now.Millisecond}", Category = ProductCategory.Cameras, Name = "lalilulelo", Price = 123.44M }; var p2 = new Product() { Id = $"214{DateTime.Now.Millisecond}", Category = ProductCategory.Computers, Name = "lalilulelo1", Price = 133.44M }; context_.Add(p2); var customer = new Customer() { VatNumber = $"{CodeGenerator.CreateRandom()}", Email = $"{CodeGenerator.CreateRandom()}" }; var order = new Order() { DeliveryAddress = "Kleeman" }; var op = new OrderProduct() { ProductId = p2.Id }; order.Products.Add(op); customer.Orders.Add(order); context_.Add(customer); context_.SaveChanges(); return(order); }
/// <summary> /// Creates a customer and adds him at the customer list /// </summary> /// <param name="options"></param> /// <returns></returns> public Customer AddCustomer(AddCustomerOptions options) { if (options == null) { return(null); } if (string.IsNullOrWhiteSpace(options.Email) || string.IsNullOrWhiteSpace(options.VatNumber)) { return(null); } if (options.VatNumber.Length > 9) { return(null); } var exists = SearchCustomer( new SearchCustomerOptions() { VatNumber = options.VatNumber }).Any(); if (exists) { return(null); } Customer customer = new Customer { VatNumber = options.VatNumber, Email = options.Email, Firstname = options.FirstName, Lastname = options.LastName, Phone = options.Phone, Active = true, Created = DateTime.UtcNow }; context_.Add(customer); try { context_.SaveChanges(); } catch { return(null); } return(customer); }
public Order CreateOrder(int customerId, ICollection <string> productIds) { if (customerId <= 0) { return(null); } if (productIds == null) { return(null); } var customer = customers_.SearchCustomer( new SearchCustomerOptions() { Id = customerId }) .Where(c => c.Active) .SingleOrDefault(); if (customer == null || productIds.Count == 0) { return(null); } var products = context_ .Set <Product>() .Where(p => productIds.Contains(p.Id)) .ToList(); if (products.Count != productIds.Count) { return(null); } var order = new Order() { Customer = customer }; foreach (var p in products) { order.Products.Add( new OrderProduct() { ProductId = p.Id } ); } context_.Add(order); try { context_.SaveChanges(); } catch (Exception) { return(null); } return(order); }