public static AuctionDetailedViewModel CreateFromAuction(Auction auction)
        {
            if (auction == null)
            {
                return null;
            }

            AuctionDetailedViewModel result = new AuctionDetailedViewModel
            {
                CurrentPrice = auction.CurrentPrice,
                DateStarted = auction.DateStarted,
                Id = auction.Id,
                Product = ProductViewModel.CreateFromProduct(auction.Product),
                Type = auction.Type,
                Duration = auction.Duration,
                CurrentBuyer = auction.CurrentBuyer
            };

            return result;
        }
        public void IndexMethodShouldReturn1Product()
       {
            Product product = new Product()
            {
                  Id = Guid.NewGuid(), Title = "test", 
                Description = "1234567891011121212512255621dfsdfsd", Price = 10,
                StartingPrice = 5, DateAdded = DateTime.Now, ImageUrl = "test",
                Category = new Category { 
                    Id = Guid.NewGuid(), Name = "dsfdsfsfdsfsfs"
                }                
            };

            Auction auction = new Auction()
            {
                Id = Guid.NewGuid(),
                DateStarted = DateTime.Now,
                Duration = 12,
                Type = AuctionType.Auction,
                Product = product,
                DeliveryDuration = 123,
                CurrentPrice = 123,                
            };

            var list = new List<Auction>();
            list.Add(auction);

            var bugsRepoMock = new Mock<IRepository<Auction>>();
            bugsRepoMock.Setup(x => x.All()).Returns(list.AsQueryable());

            var uofMock = new Mock<IUnitOfWorkData>();
            uofMock.Setup(x => x.Auctions).Returns(bugsRepoMock.Object);

            var controller = new HomeController(uofMock.Object);
            var viewResult = controller.Index() as ViewResult;
            Assert.IsNotNull(viewResult, "Index action returns null.");
            var model = viewResult.Model as IList<ProductViewModel>;

            Assert.IsNotNull(model, "The model is null.");
            Assert.AreEqual(1, model.Count());
            Assert.AreEqual(product.Description, model[0].Description);
       }
        public ActionResult Create(AddProductViewModel product)
        {
            if (product.StartingPrice >= product.Price)
            {
                ModelState.AddModelError("StartingPrice", new ArgumentOutOfRangeException("Starting price should be less than product price."));
            }

            if (ModelState.IsValid)
            {
                var cat = db.Categories.GetById(product.Category.Id);

                string ownerId = User.Identity.GetUserId();

                ApplicationUser owner = this.db.Users.All().FirstOrDefault(user => user.Id == ownerId);

                if (product.ImageUrl == null)
                {
                    product.ImageUrl = "default-product-image.jpg";
                }
                var newProduct = new Product
                {
                    Id = Guid.NewGuid(),
                    ImageUrl = product.ImageUrl,
                    Category = cat,
                    DateAdded = DateTime.Now,
                    CategoryId = product.Category.Id, // (yoan) should it be : cat.id
                    Condition = product.Condition,
                    Description = product.Description,
                    Price = Convert.ToDecimal(product.Price),
                    StartingPrice = Convert.ToDecimal(product.StartingPrice),
                    Title = product.Title,
                    State = ProductState.ForSelling,
                    Owner = owner,
                    OwnerId = ownerId
                };

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

                var newAuction = new Auction
                {
                    Id = Guid.NewGuid(),
                    DateStarted = DateTime.Now,
                    Duration = product.AuctionTime,
                    Product = newProduct,
                    Type = product.AuctionType,
                    DeliveryDuration = product.DeliveryTime,
                    
                };

                if (product.AuctionType == AuctionType.Normal)
                {
                    newAuction.CurrentPrice = product.Price;
                }
                else
                {
                    newAuction.CurrentPrice = product.StartingPrice;
                }

                this.db.Auctions.Add(newAuction);
                this.db.SaveChanges();

                return RedirectToAction("Details", "Products", new { id = newProduct.Id });
            }

            ViewBag.Categories = db.Categories.All().ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

            return View(product);
        }
        public ActionResult Create(AuctionCreateModel model)
        {
            string loggedUserId = User.Identity.GetUserId();

            ApplicationUser currentUser = this.db.Users.GetById(loggedUserId);

            if (ModelState.IsValid)
            {

                Product currentProduct = this.db.Products.All().FirstOrDefault(product => product.Id == model.ProductId);

                Auction newAuction = this.db.Auctions.All().FirstOrDefault(auction => auction.Product.Id == model.ProductId);

                if (newAuction != null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "There is already an auction for this product");
                }

                newAuction = new Auction
                {
                    Id = Guid.NewGuid(),
                    DateStarted = DateTime.Now,
                    Duration = model.Duration,
                    Product = currentProduct,
                    Type = model.Type,
                    CurrentPrice = currentProduct.StartingPrice
                };

                this.db.Auctions.Add(newAuction);
                this.db.SaveChanges();

                return RedirectToAction("Details", "Products", new { id = currentProduct.Id });
            }

            var ownProducts = this.db.Products
                                          .All()
                                          .Where(product => product.Owner.Id == currentUser.Id)
                                          .ToList()
                                          .Select(productEntity => new SelectListItem()
                                          {
                                              Text = productEntity.Title,
                                              Value = productEntity.Id.ToString()
                                          });

            ViewBag.OwnProducts = ownProducts;

            return View();
        }
        private Delivery CreateDelivery(Auction currentAuction, string username)
        {

            ApplicationUser currentUser = this.db.Users.All().FirstOrDefault(user => user.UserName == username);

            if (currentUser == null)
            {
                throw new ArgumentNullException("User not found.");
            }

            Delivery createdDelivery = new Delivery
            {
                Id = Guid.NewGuid(),
                DateShipped = DateTime.Now,
                Duration = currentAuction.DeliveryDuration,
                Receiver = currentUser,
                State = DeliveryState.Shipping
            };

            createdDelivery.Products.Add(currentAuction.Product);
            
            return createdDelivery;
        }