public void CreateOrder(int userId, Dictionary <int, int> products)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var order = new Order
                {
                    UserId       = userId,
                    CreationTime = DateTime.UtcNow,
                    Products     = products.Keys.Select(id => new OrderProduct
                    {
                        ProductId = id,
                        Quantity  = products[id]
                    }).ToList()
                };

                db.Add(order);
                db.SaveChanges();
            }
        }
Example #2
0
        public void CreateOrder(int userId, IEnumerable <int> productIds)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var order = new Order
                {
                    UserId       = userId,
                    CreationDate = DateTime.UtcNow,
                    Products     = productIds.Select(id => new OrderProduct
                    {
                        ProductId = id
                    }).ToList()
                };

                db.Add(order);

                db.SaveChanges();
            }
        }
Example #3
0
        public IEnumerable <UserOrdersListingViewModel> GetUserOrders(string username)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var userOrders = db.Orders
                                 .Include(o => o.User)
                                 .Where(o => o.User.Username == username)
                                 .Select(o => new UserOrdersListingViewModel
                {
                    Id           = o.Id,
                    CreationDate = o.CreationDate,
                    Sum          = o.OrderProducts.Sum(op => op.Product.Price)
                })
                                 .OrderByDescending(u => u.CreationDate)
                                 .ToArray();

                return(userOrders);
            }
        }
Example #4
0
 public IEnumerable <OrdersListingViewModel> FindOrders(string username)
 {
     using (var context = new ByTheCakeDbContext())
     {
         return(context
                .Orders
                .Where(o => o.User.Username == username)
                .Select(o => new OrdersListingViewModel
         {
             Id = o.Id,
             CreationDate = o.CreationDate,
             TotalSum = o.Products
                        .Where(op => op.OrderId == o.Id)
                        .Select(op => op.Product)
                        .Sum(p => p.Price)
         })
                .ToList());
     }
 }
Example #5
0
        public IHttpResponse FinishOrder(IHttpRequest req)
        {
            var session = req.Session;

            var userId = session.Get <int>(SessionStore.CurrentUserKey);

            var products = session.Get <ShoppingCart>(ShoppingCart.SessionKey).Orders;

            var productIds = products.Select(p => p.Id).ToList();

            using (var db = new ByTheCakeDbContext())
            {
                var order = new Order(userId);
                db.Orders.Add(order);
                db.SaveChanges();

                foreach (var product in products)
                {
                    int prodId     = product.Id;
                    int productQty = productIds.Count(id => id == prodId);

                    if (productQty > 0)
                    {
                        productIds.RemoveAll(id => id == prodId);

                        var productOrder = new ProductOrder(product.Id, order.Id, productQty);

                        db.ProductOrders.Add(productOrder);
                    }
                    if (productIds.Count == 0)
                    {
                        break;
                    }
                }

                db.SaveChanges();
            }

            req.Session.Get <ShoppingCart>(ShoppingCart.SessionKey).Orders.Clear();

            return(this.FileViewResponse(@"shopping\finish-order"));
        }
 public IEnumerable <OrderFromDbViewModel> GetUserOrders(string username)
 {
     using (var db = new ByTheCakeDbContext())
     {
         return(db
                .Users
                .Include(x => x.Orders)
                .ThenInclude(x => x.Products)
                .ThenInclude(x => x.Product)
                .First(u => u.Username == username)
                .Orders
                .Select(o => new OrderFromDbViewModel
         {
             Id = o.Id,
             CreatedOn = o.CreationTime,
             Sum = o.Products.Sum(p => p.Quantity * p.Product.Price)
         })
                .ToList());
     }
 }
        public IEnumerable <ProductListingViewModel> GetAllProducts(string searchProduct = null)
        {
            using (ByTheCakeDbContext database = new ByTheCakeDbContext())
            {
                IQueryable <Product> resultsQuery = database.Products.AsQueryable();

                if (!string.IsNullOrEmpty(searchProduct))
                {
                    resultsQuery = resultsQuery.Where(p => p.Name.ToLower().Contains(searchProduct.ToLower()));
                }

                return(resultsQuery.Select(p => new ProductListingViewModel
                {
                    Id = p.Id,
                    Name = p.Name,
                    Price = p.Price
                })
                       .ToList());
            }
        }
Example #8
0
        public ProfileViewModel FindByUsername(string username)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var user            = db.Users.FirstOrDefault(u => u.Username.ToLower() == username.ToLower());
                var userOrdersCount = db.Orders.Where(o => o.UserId == user.Id).Count();

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

                return(new ProfileViewModel()
                {
                    Username = user.Username,
                    RegisteredOn = user.RegistrationDate,
                    OrdersCount = userOrdersCount
                });
            }
        }
Example #9
0
        public IEnumerable <ProductsListViewModel> All(string searchTerm = null)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var products = db.Products.AsQueryable();

                if (!string.IsNullOrEmpty(searchTerm))
                {
                    products = products.Where(p => p.Name.ToLower().Contains(searchTerm.ToLower()));
                }

                return(products
                       .Select(p => new ProductsListViewModel()
                {
                    Name = p.Name,
                    Price = p.Price,
                    Id = p.Id
                }).ToList());
            }
        }
Example #10
0
        public List <OrdersViewModel> GetOrdersById(string username)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var user = db.Users
                           .Where(u => u.Username == username)
                           .FirstOrDefault();

                var orders = db.Orders
                             .Where(o => o.UserId == user.Id)
                             .Select(u => new OrdersViewModel
                {
                    OrderId      = u.Id,
                    CreationDate = u.CreationDate,
                    Sum          = u.Products.Sum(p => p.Product.Price)
                })
                             .ToList();
                return(orders);
            }
        }
        public OrderDetailsViewModel GetOrder(int orderId)
        {
            using (ByTheCakeDbContext database = new ByTheCakeDbContext())
            {
                OrderDetailsViewModel order = database.Orders
                                              .Where(o => o.Id == orderId)
                                              .Select(o => new OrderDetailsViewModel
                {
                    Id                = o.Id,
                    ProductIds        = o.Products.Select(op => op.ProductId).ToList(),
                    ProductNames      = o.Products.Select(op => op.Product.Name).ToList(),
                    ProductPrices     = o.Products.Select(op => op.Product.Price).ToList(),
                    ProductQuantities = o.Products.Select(op => op.Quantity).ToList(),
                    CreationDate      = o.CreationDate
                })
                                              .FirstOrDefault();

                return(order);
            }
        }
Example #12
0
        public IEnumerable <OrderListViewModel> GetOrdersByUser(int userId)
        {
            using (var db = new ByTheCakeDbContext())
            {
                List <Order> ordersByUser = db.Orders.Where(o => o.UserId == userId).ToList();

                if (!ordersByUser.Any())
                {
                    return(null);
                }

                return(ordersByUser
                       .Select(o => new OrderListViewModel()
                {
                    Id = o.Id,
                    CreationDate = o.CreationTime,
                    Price = o.Products.Select(p => p.Product.Price).Sum()
                }));
            }
        }
Example #13
0
        public List <Product> GetProductsByGivenOrderId(int orderId)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var orderProducts = db.OrderProducts
                                    .Where(x => x.OrderId == orderId)
                                    .ToList();

                var products = new List <Product>();

                foreach (var orderProduct in orderProducts)
                {
                    var product = db.Products.SingleOrDefault(x => x.Id == orderProduct.ProductId);

                    products.Add(product);
                }

                return(products);
            }
        }
Example #14
0
    public IEnumerable <ProductListingViewModel> All(string searchTerm = null)
    {
        using (var db = new ByTheCakeDbContext())
        {
            var resultQuery = db.Products.AsQueryable();

            if (!string.IsNullOrEmpty(searchTerm))
            {
                resultQuery = resultQuery.Where(pr => pr.Name.ToLower()
                                                .Contains(searchTerm.ToLower()));
            }
            return(resultQuery.Select(pr => new ProductListingViewModel
            {
                Id = pr.Id,
                Name = pr.Name,
                Price = pr.Price
            })
                   .ToList());
        }
    }
Example #15
0
        public IHttpResponse OrderDetails(IHttpRequest req)
        {
            int orderId = int.Parse(req.UrlParameters["id"]);

            string tableContent = "";
            string date         = "";

            using (var db = new ByTheCakeDbContext())
            {
                var orderDate = db.Orders
                                .Where(o => o.Id == orderId)
                                .Select(o => o.OrderDate)
                                .First()
                                .ToString("dd-MM-yyyy");

                var productList = db.ProductOrders
                                  .Where(po => po.OrderId == orderId)
                                  .Select(po => new
                {
                    po.ProductId,
                    po.Product.Name,
                    po.Product.Price,
                    po.ProductQty
                })
                                  .ToList();

                foreach (var product in productList)
                {
                    tableContent += $"<tr><td><a href=\"/details/{product.ProductId}\"> {product.Name} </a></td>" +
                                    $"<td> {product.Price} </td>" +
                                    $"<td> {product.ProductQty}</td></tr>";
                }

                date = $"<p></p><p><em>Created on {orderDate}</em></p>";
            }

            this.ViewData["table"] = tableContent;
            this.ViewData["date"]  = date;

            return(this.FileViewResponse(@"shopping\order-details"));
        }
Example #16
0
        public bool Create(string username, string password)
        {
            using (var db = new ByTheCakeDbContext())
            {
                if (db.Users.Any(u => u.Username == username))
                {
                    return(false);
                }

                var user = new User
                {
                    Username         = username,
                    Password         = password,
                    RegistrationDate = DateTime.UtcNow
                };

                db.Add(user);
                db.SaveChanges();
                return(true);
            }
        }
        public IEnumerable <ProductFromOrderViewModel> GetOrderProducts(int orderId)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var order = db
                            .Orders
                            .Include(x => x.Products)
                            .ThenInclude(x => x.Product)
                            .FirstOrDefault(o => o.Id == orderId);

                return(order?.Products
                       .Select(p => new ProductFromOrderViewModel
                {
                    Id = p.Product.Id,
                    Name = p.Product.Name,
                    Price = p.Product.Price,
                    Quantity = p.Quantity
                })
                       .ToList());
            }
        }
Example #18
0
        public IEnumerable <ProductListingViewModel> All(string searchTerm = null)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var products = db.Products.AsQueryable();

                if (searchTerm != null)
                {
                    products = products.Where(p => p.Name.ToLower().Contains(searchTerm));
                }

                return(products
                       .Select(pr => new ProductListingViewModel
                {
                    Id = pr.Id,
                    Name = pr.Name,
                    Price = pr.Price
                })
                       .ToList());
            }
        }
Example #19
0
 public OrderDetailsViewModel GetOrderDetails(int id)
 {
     using (var db = new ByTheCakeDbContext())
     {
         return(db.Orders
                .Where(o => o.Id == id)
                .Select(e => new OrderDetailsViewModel
         {
             Id = e.Id,
             CreatedOn = e.CreationDate,
             Products = e.Products
                        .Select(p => new ProductViewModel
             {
                 Id = p.ProductId,
                 Name = p.Product.Name,
                 Price = p.Product.Price
             }).ToList()
                        .ToList()
         }).FirstOrDefault());
     }
 }
Example #20
0
        public bool Find(string username, string password)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var user = db.Users.SingleOrDefault(x => x.Username == username);

                if (user == null)
                {
                    return(false);
                }

                bool validPassword = BCrypt.Net.BCrypt.Verify(password, user.Password);

                if (!validPassword)
                {
                    return(false);
                }

                return(true);
            }
        }
        public ProductDetailsViewModel Find(int id)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var resultProduct = db
                                    .Products
                                    .FirstOrDefault(p => p.Id == id);

                if (resultProduct != null)
                {
                    return(new ProductDetailsViewModel
                    {
                        Name = resultProduct.Name,
                        ImageUrl = resultProduct.ImageUrl,
                        Price = resultProduct.Price
                    });
                }

                return(null);
            }
        }
        public void CreateOrder(int userId, IDictionary <int, int> productIdWithQuantity)
        {
            using (ByTheCakeDbContext database = new ByTheCakeDbContext())
            {
                Order order = new Order
                {
                    UserId       = userId,
                    CreationDate = DateTime.UtcNow,
                    Products     = productIdWithQuantity
                                   .Select(pq => new OrderProduct
                    {
                        ProductId = pq.Key,
                        Quantity  = pq.Value
                    })
                                   .ToList()
                };

                database.Orders.Add(order);
                database.SaveChanges();
            }
        }
        public ProfileViewModel Profile(string username)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var profile = db
                              .Users
                              .FirstOrDefault(u => u.Username == username);

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

                return(new ProfileViewModel
                {
                    Username = profile.Username,
                    RegistrationDate = DateTime.UtcNow,
                    OrdersCount = profile.Orders.Count
                });
            }
        }
Example #24
0
        public IEnumerable <ProductListingViewModel> List(string searchTerm = null)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var results = db.Products.ToArray();

                if (!String.IsNullOrEmpty(searchTerm))
                {
                    results = results
                              .Where(p => p.Name.ToLower().Contains(searchTerm.ToLower()))
                              .ToArray();
                }

                return(results.Select(p => new ProductListingViewModel
                {
                    Id = p.Id,
                    Name = p.Name,
                    Price = p.Price
                })
                       .ToList());
            }
        }
Example #25
0
        public bool Add(ProductViewModel cake)
        {
            using (var db = new ByTheCakeDbContext())
            {
                if (db.Products.Any(p => p.Name == cake.Name))
                {
                    return(false);
                }

                var product = new Product()
                {
                    Name     = cake.Name,
                    Price    = cake.Price,
                    ImageUrl = cake.ImageUrl,
                };

                db.Products.Add(product);
                db.SaveChanges();

                return(true);
            }
        }
        public IEnumerable <ProductListingViewModel> Find(int id)
        {
            using (var db = new ByTheCakeDbContext())
            {
                if (!db.Orders.Any(o => o.Id == id))
                {
                    throw new  InvalidOperationException("No such order!");
                }

                var product = db.ProductsOrders
                              .Where(po => po.OrderId == id)
                              .Select(p => new ProductListingViewModel()
                {
                    Id    = p.ProductId.ToString(),
                    Name  = p.Product.Name,
                    Price = p.Product.Price
                })
                              .ToArray();

                return(product);
            }
        }
Example #27
0
        public DetailsViewModel Details(int orderId)
        {
            using (var db = new ByTheCakeDbContext())
            {
                var details = db.Orders
                              .Where(o => o.Id == orderId)
                              .Select(o => new DetailsViewModel
                {
                    Id       = o.Id,
                    Products = o.Products.Select(p => new ProductViewModel
                    {
                        Id    = p.ProductId,
                        Name  = p.Product.Name,
                        Price = p.Product.Price
                    }).ToList(),
                    CreatedOn = o.CreationDate
                })
                              .FirstOrDefault();

                return(details);
            }
        }
Example #28
0
        public bool Success()
        {
            using (var db = new ByTheCakeDbContext())
            {
                if (db.Users.Any(x => x.Username == username))
                {
                    return(false);
                }

                var user = new User();
                user.Username = username;

                password                = BCrypt.Net.BCrypt.HashPassword(password);
                user.Password           = password;
                user.DateOfRegistration = DateTime.UtcNow;

                db.Add(user);
                db.SaveChanges();

                return(true);
            }
        }
Example #29
0
 public OrderDetailsByIdViewModel Find(int id)
 {
     using (var db = new ByTheCakeDbContext())
     {
         return(db.Orders
                .Where(o => o.Id == id)
                .Select(o => new OrderDetailsByIdViewModel
         {
             Id = o.Id,
             CreatedOn = o.CreationDate,
             Products = o.Products
                        .Where(p => p.OrderId == o.Id)
                        .Select(p => new OrderProductsDetailsViewModel
             {
                 Id = p.ProductId,
                 Name = p.Product.Name,
                 Price = p.Product.Price
             })
         })
                .FirstOrDefault());
     }
 }
Example #30
0
        public IHttpResponse Login(IHttpRequest req)
        {
            const string formNameKey     = "username";
            const string formPasswordKey = "password";
            var          formData        = req.FormData;

            if (!formData.ContainsKey(formNameKey) || !formData.ContainsKey(formPasswordKey))
            {
                return(new BadRequestResponse());
            }

            string username = req.FormData[formNameKey];
            string password = req.FormData[formPasswordKey];

            if (HasWrongFields(username, password))
            {
                return(this.FileViewResponse(@"account\login"));
            }

            string passwordHash = PasswordUtilities.GetHash(password);
            int    userId       = 0;

            using (var db = new ByTheCakeDbContext())
            {
                User user = db.Users.FirstOrDefault(u => u.Username == username);
                if (user == null || user.PasswordHash != passwordHash)
                {
                    UserOrPasswordError();
                    return(this.FileViewResponse(@"account\login"));
                }
                userId = user.Id;
            }

            req.Session.Add(SessionStore.CurrentUserKey, userId);
            req.Session.Add(ShoppingCart.SessionKey, new ShoppingCart());

            return(new RedirectResponse("/"));
        }