Exemple #1
0
        public Order CreateOrder()
        {
            var order = new Order()
            {
                Customer = new Customer
                {
                    FirstName = _session.Get <string>(Constants.FIRST_NAME_KEY),
                    LastName  = _session.Get <string>(Constants.LAST_NAME_KEY)
                },
                Date       = DateTime.Now,
                TotalPrice = ShoppingCart.GetTotalPrice()
            };

            var shoppingCartProductIds = ShoppingCart.GetProductIds();
            var products = _candyStoreRepository.GetAll <Product>()
                           .Where(x => shoppingCartProductIds.Contains(x.ProductID));

            var orderDetails = new List <OrderDetails>();

            foreach (var product in products)
            {
                var orderDetail = new OrderDetails
                {
                    Order           = order,
                    Product         = product,
                    ProductQuantity = ShoppingCart.GetProductQuantity(product)
                };

                orderDetails.Add(orderDetail);
            }

            _candyStoreRepository.InsertRange(orderDetails);

            var productsInDb = from product in ShoppingCart.GetAllProducts()
                               join dbProduct in _candyStoreRepository.GetAll <Product>()
                               on product.ProductID equals dbProduct.ProductID
                               select dbProduct;

            foreach (var product in ShoppingCart.GetAllProductsWithQuantity())
            {
                var productInDb = productsInDb.FirstOrDefault(x => x.ProductID.Equals(product.Key.ProductID));
                productInDb.Count -= product.Value;
            }

            _candyStoreRepository.UpdateRange(productsInDb);

            _session.Clear();

            return(order);
        }
Exemple #2
0
        public OperationValidationResult LoginAdministrator(string identificationNumberAsString)
        {
            var result = new OperationValidationResult
            {
                Valid = true
            };

            int identificationNumber;
            var parsed = int.TryParse(identificationNumberAsString, out identificationNumber);

            if (!parsed)
            {
                result.Valid = false;
                result.AddErrorMessage("Enter a correct whole number value.");

                return(result);
            }

            var user = _candyStoreRepository.GetAll <Employee>()
                       .FirstOrDefault(u => u.IdentificationNumber == identificationNumber);

            if (user == null)
            {
                result.Valid = false;
                result.AddErrorMessage("There is no such employee");
            }

            return(result);
        }
Exemple #3
0
        public string GetReceiptText()
        {
            _stringBuilderFacade.AppendLine("Candy Store Receipt");
            _stringBuilderFacade.AppendLine("\n");
            _stringBuilderFacade.AppendLine($"Order: {View.OrderId.ToString()}");

            var purchasedProducts = (from order in _candyStoreRepository.GetAll <Order>().Where(x => x.OrderID == View.OrderId)
                                     join orderDetail in _candyStoreRepository.GetAll <OrderDetails>()
                                     on order.OrderID equals orderDetail.OrderId
                                     into joinedOrderDetails
                                     from joinedOrderDetail in joinedOrderDetails
                                     join product in _candyStoreRepository.GetAll <Product>()
                                     on joinedOrderDetail.ProductId equals product.ProductID
                                     select new
            {
                Product = product,
                Quantity = joinedOrderDetail.ProductQuantity
            })
                                    .ToDictionary(x => x.Product, x => x.Quantity);

            foreach (var item in purchasedProducts)
            {
                string currentLine = $"{item.Key.Name}  ${item.Key.Price:f2} x {item.Value}    ${item.Value * item.Key.Price:f2}";
                _stringBuilderFacade.AppendLine(currentLine);
            }

            var orderInDb = _candyStoreRepository.GetAll <Order>()
                            .FirstOrDefault(x => x.OrderID == View.OrderId);

            _stringBuilderFacade.AppendLine($"\nTotal price: {orderInDb.TotalPrice.ToString("0.00" + "$")}");

            _stringBuilderFacade.AppendLine($"\nCustomer: {orderInDb.Customer.FirstName} {orderInDb.Customer.LastName}");
            _stringBuilderFacade.AppendLine($"\nDate: {_dateTimeFacade.GetCurrentTime()}");

            return(_stringBuilderFacade.ToString());
        }
        public OperationValidationResult AddNewProduct(string productPriceString, string productName, string categoryName, byte[] image)
        {
            var result = new OperationValidationResult
            {
                Valid = true
            };

            var isParsed = double.TryParse(productPriceString, out double productPrice);

            if (!isParsed || productPrice < 0)
            {
                result.Valid = false;
                result.AddErrorMessage("Price must be a positive number.");
                return(result);
            }

            if (image == null)
            {
                result.Valid = false;
                result.AddErrorMessage("Select an image for the product.");
                return(result);
            }

            if (string.IsNullOrEmpty(productName) || string.IsNullOrEmpty(categoryName))
            {
                result.Valid = false;
                result.AddErrorMessage("Type in a valid name for product and category.");
                return(result);
            }

            try
            {
                var product = new Product
                {
                    Name  = productName,
                    Price = productPrice
                };
                var category = _candyStoreRepository.GetAll <Category>().FirstOrDefault(c => c.Name == categoryName);
                product.Category     = category;
                product.ProductImage = image;

                _candyStoreRepository.Insert(product);
            }
            catch (Exception ex)
            {
                result.Valid = false;
                result.AddErrorMessage(ex.Message);
                return(result);
            }

            return(result);
        }
Exemple #5
0
 public IList <Category> GetAllCategories()
 {
     return(_candyStoreRepository.GetAll <Category>()
            .ToList());
 }
 public Category GetCategoryById(int categoryId)
 {
     return(_candyStoreRepository.GetAll <Category>()
            .FirstOrDefault(x => x.CategoryID == categoryId));
 }