Exemplo n.º 1
0
        public Customer CreateCustomer(CreateCustomerOptions options)
        {
            if (options == null)
            {
                return(null);
            }

            if (string.IsNullOrWhiteSpace(options.VatNumber) ||
                string.IsNullOrWhiteSpace(options.Email))
            {
                return(null);
            }

            if (options.VatNumber.Length > 9)
            {
                return(null);
            }

            var exists = SearchCustomers(
                new SearchCustomerOptions()
            {
                VatNumber = options.VatNumber
            }).Any();

            if (exists)
            {
                return(null);
            }

            var customer = new Customer()
            {
                VatNumber = options.VatNumber,
                Firstname = options.FirstName,
                Lastname  = options.LastName,
                Email     = options.Email,
                Phone     = options.Phone,
                IsActive  = true
            };

            context_.Add(customer);
            try {
                context_.SaveChanges();
            }catch (Exception ex) {
                return(null);
            }

            return(customer);
        }
Exemplo n.º 2
0
        public Order CreateOrder(int customerId,
                                 ICollection <string> productIds)
        {
            if (customerId <= 0)
            {
                return(null);
            }

            if (productIds == null ||
                productIds.Count == 0)
            {
                return(null);
            }

            var customer = customers_.SearchCustomers(
                new SearchCustomerOptions()
            {
                CustomerId = customerId
            })
                           .Where(c => c.IsActive)
                           .SingleOrDefault();

            if (customer == null)
            {
                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 ex) {
                return(null);
            }

            return(order);
        }
Exemplo n.º 3
0
        public async Task <ApiResult <Order> > CreateOrder(
            int customerId, ICollection <string> productIds)
        {
            if (customerId <= 0)
            {
                return(null);
            }

            if (productIds == null ||
                productIds.Count == 0)
            {
                return(null);
            }

            var customer = customers_.SearchCustomers(
                new SearchCustomerOptions()
            {
                CustomerId = customerId
            })
                           .Where(c => c.IsActive)
                           .SingleOrDefault();

            if (customer == null)
            {
                return(null);
            }

            var products = new List <Product>();

            foreach (var p in productIds)
            {
                var presult = await products_
                              .GetProductByIdAsync(p);

                if (!presult.Success)
                {
                    var ret = presult.ToResult <Order>();

                    return(new ApiResult <Order>(
                               presult.ErrorCode, presult.ErrorText));
                }

                products.Add(presult.Data);
            }

            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 ex) {
                return(null);
            }

            return(new ApiResult <Order>()
            {
                Data = order
            });
        }