public ActionResult Create(MarketEntryFormViewModel viewModel)
        {
            var userId        = User.Identity.GetUserId();
            var activeAuction = unitOfWork.Auctions.GetActiveAuction();

            var finalProducts    = new List <FinalProduct>();
            var products         = new List <Product>();
            var productQualities = new List <ProductQuality>();

            PopulateLists(finalProducts, products, productQualities);

            if (!ModelState.IsValid)
            {
                viewModel.Products         = User.IsInRole(RoleName.Buyer) ? products : new List <Product>();
                viewModel.ProductQualities = User.IsInRole(RoleName.Buyer) ? productQualities : new List <ProductQuality>();
                viewModel.FinalProducts    = User.IsInRole(RoleName.Seller) ? finalProducts : new List <FinalProduct>();
                viewModel.IsBuyerForm      = User.IsInRole(RoleName.Buyer);

                return(View(ViewName.MarketEntryForm, viewModel));
            }

            if (User.IsInRole(RoleName.Buyer))
            {
                activeAuction.AddBuyerMarketEntry(viewModel, userId);
            }
            else if (User.IsInRole(RoleName.Seller))
            {
                activeAuction.AddSellerMarketEntry(viewModel, userId);
            }

            unitOfWork.Complete();

            return(RedirectToAction(ViewName.Market, "Home"));
        }
        public ActionResult New()
        {
            var finalProducts    = new List <FinalProduct>();
            var products         = new List <Product>();
            var productQualities = new List <ProductQuality>();

            PopulateLists(finalProducts, products, productQualities);

            var viewModel = new MarketEntryFormViewModel()
            {
                DeliveryDate           = DateTime.Now,
                Products               = User.IsInRole(RoleName.Buyer) ? products : new List <Product>(),
                ProductQualities       = User.IsInRole(RoleName.Buyer) ? productQualities : new List <ProductQuality>(),
                FinalProducts          = User.IsInRole(RoleName.Seller) ? finalProducts : new List <FinalProduct>(),
                IsBuyerForm            = User.IsInRole(RoleName.Buyer),
                FinalProductsAvailable = User.IsInRole(RoleName.Buyer),
                Heading = User.IsInRole(RoleName.Buyer) ? "Φόρμα Ζήτησης" : "Φόρμα Προσφοράς"
            };

            if (User.IsInRole(RoleName.Seller) && viewModel.FinalProducts.Any())
            {
                viewModel.FinalProductsAvailable = true;
            }

            return(View(ViewName.MarketEntryForm, viewModel));
        }
        // Add New Seller Market Entry to Auction
        public void AddSellerMarketEntry(MarketEntryFormViewModel viewModel, string userId)
        {
            var marketEntry = MarketEntry.CreateSellerEntry(userId, EntryType.Supply,
                                                            Id, viewModel.FinalProductId, viewModel.EntryQuantity,
                                                            viewModel.EntryPrice, viewModel.DeliveryDate);

            MarketEntries.Add(marketEntry);
        }
        // Add New Buyer Market Entry to Auction
        public void AddBuyerMarketEntry(MarketEntryFormViewModel viewModel, string userId)
        {
            var marketEntry = MarketEntry.CreateBuyerEntry(userId, EntryType.Demand,
                                                           Id, viewModel.ProductId, viewModel.ProductQualityId,
                                                           viewModel.EntryQuantity, viewModel.EntryPrice, viewModel.DeliveryDate,
                                                           viewModel.DeliveryLocation);

            MarketEntries.Add(marketEntry);
        }
        // Update Market Entry
        public void Modify(MarketEntryFormViewModel viewModel)
        {
            EntryQuantity = viewModel.EntryQuantity;
            EntryPrice    = viewModel.EntryPrice;
            DeliveryDate  = viewModel.DeliveryDate;
            SetFinalProduct(viewModel.FinalProductId);
            DeliveryLocation = viewModel.DeliveryLocation;

            if (EntryType == EntryType.Demand)
            {
                FinalProduct.Modify(viewModel.ProductId, viewModel.ProductQualityId);
            }
        }
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var marketEntry = unitOfWork.MarketEntries
                              .GetMarketEntryWithFinalProductAndAuction(id);

            if (marketEntry == null)
            {
                return(HttpNotFound());
            }

            // Check for users to be able to edit only their market entries of active auction only
            if (marketEntry.Auction.IsCompleted && marketEntry.UserId != User.Identity.GetUserId())
            {
                return(new HttpUnauthorizedResult());
            }

            var finalProducts    = new List <FinalProduct>();
            var products         = new List <Product>();
            var productQualities = new List <ProductQuality>();

            PopulateLists(finalProducts, products, productQualities);

            var viewModel = new MarketEntryFormViewModel()
            {
                Id                     = marketEntry.Id,
                DeliveryDate           = marketEntry.DeliveryDate,
                EntryQuantity          = marketEntry.EntryQuantity,
                EntryPrice             = marketEntry.EntryPrice,
                DeliveryLocation       = marketEntry.DeliveryLocation,
                FinalProductId         = marketEntry.FinalProductId,
                ProductId              = marketEntry.FinalProduct.ProductId,
                ProductQualityId       = marketEntry.FinalProduct.QualityId,
                Products               = User.IsInRole(RoleName.Buyer) ? products : new List <Product>(),
                ProductQualities       = User.IsInRole(RoleName.Buyer) ? productQualities : new List <ProductQuality>(),
                FinalProducts          = User.IsInRole(RoleName.Seller) ? finalProducts : new List <FinalProduct>(),
                IsBuyerForm            = User.IsInRole(RoleName.Buyer),
                FinalProductsAvailable = true,
                Heading                = "Επεξεργασία Καταχώρισης"
            };

            return(View(ViewName.MarketEntryForm, viewModel));
        }
        public ActionResult Update(MarketEntryFormViewModel viewModel)
        {
            var finalProducts    = new List <FinalProduct>();
            var products         = new List <Product>();
            var productQualities = new List <ProductQuality>();

            PopulateLists(finalProducts, products, productQualities);

            if (!ModelState.IsValid)
            {
                viewModel.Products         = User.IsInRole(RoleName.Buyer) ? products : new List <Product>();
                viewModel.ProductQualities = User.IsInRole(RoleName.Buyer) ? productQualities : new List <ProductQuality>();
                viewModel.FinalProducts    = User.IsInRole(RoleName.Seller) ? finalProducts : new List <FinalProduct>();
                viewModel.IsBuyerForm      = User.IsInRole(RoleName.Buyer);

                return(View(ViewName.MarketEntryForm, viewModel));
            }

            var marketEntry = unitOfWork.MarketEntries
                              .GetMarketEntryWithFinalProduct(viewModel.Id);

            if (marketEntry == null)
            {
                return(HttpNotFound());
            }

            if (marketEntry.UserId != User.Identity.GetUserId())
            {
                return(new HttpUnauthorizedResult());
            }

            marketEntry.Modify(viewModel);

            unitOfWork.Complete();

            return(RedirectToAction(ViewName.Market, "Home"));
        }