public IActionResult Create([Bind("SellerName,Items,AverageReview")] SellerViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var seller = new Domain.Model.Seller
                    {
                        Id    = viewModel.SellerId,
                        Name  = viewModel.SellerName,
                        Items = RepoItem.GetItemsBySellerName(viewModel.SellerName)
                                .FindAll(p => p.SellerId == (RepoSell.GetSellersByName(viewModel.SellerName)
                                                             .First(p => p.Name == viewModel.SellerName).Id)),
                        Reviews = RepoRev.GetReviewBySellerName(viewModel.SellerName)
                                  .FindAll(p => p.SellerId == (RepoSell.GetSellersByName(viewModel.SellerName)
                                                               .First(p => p.Name == viewModel.SellerName).Id))
                    };

                    RepoSell.AddSeller(seller);
                    RepoSell.Save();

                    return(RedirectToAction(nameof(Index)));
                }
                return(View(viewModel));
            }
            catch
            {
                return(View(viewModel));
            }
        }
        public IActionResult Edit([FromRoute] int id, [Bind("SellerName,Items,AverageReview")] SellerViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Domain.Model.Seller seller = RepoSell.GetSellerById(id);
                    seller.Name  = viewModel.SellerName;
                    seller.Items = RepoItem.GetItemsBySellerName(viewModel.SellerName)
                                   .FindAll(p => p.SellerId == (RepoSell.GetSellersByName(viewModel.SellerName)
                                                                .First(p => p.Name == viewModel.SellerName).Id));
                    seller.Reviews = RepoRev.GetReviewBySellerName(viewModel.SellerName)
                                     .FindAll(p => p.SellerId == (RepoSell.GetSellersByName(viewModel.SellerName)
                                                                  .First(p => p.Name == viewModel.SellerName).Id));
                    RepoSell.UpdateSeller(seller);
                    RepoSell.Save();

                    return(RedirectToAction(nameof(Index)));
                }
                return(View(viewModel));
            }
            catch (Exception)
            {
                return(View(viewModel));
            }
        }
Ejemplo n.º 3
0
 public static Context.Seller UnMapSeller(Domain.Model.Seller seller)
 {
     return(new Context.Seller
     {
         SellerId = seller.Id,
         SellerName = seller.Name,
     });
 }
        public void UpdateSeller(Domain.Model.Seller inputSeller)
        {
            _logger.LogInformation($"Updating seller with ID {inputSeller.Id}");
            Context.Seller currentEntity = _dbContext.Sellers.Find(inputSeller.Id);
            Context.Seller newEntity     = Mapper.UnMapSeller(inputSeller);

            _dbContext.Entry(currentEntity).CurrentValues.SetValues(newEntity);
        }
        // GET: Items/Delete/5
        public IActionResult Delete(int id)
        {
            Domain.Model.Seller seller = RepoSell.GetSellerById(id);
            var viewModel = new SellerViewModel
            {
                SellerId      = seller.Id,
                SellerName    = seller.Name,
                Items         = seller.Items.Count,
                AverageReview = seller.Rating
            };

            return(View(viewModel));
        }
        public void AddSeller(Domain.Model.Seller inputSeller)
        {
            if (inputSeller.Id != 0)
            {
                _logger.LogWarning($"Seller to be added has an ID ({inputSeller.Id}) already: ignoring.");
            }

            _logger.LogInformation("Adding seller");

            Context.Seller entity = Mapper.UnMapSeller(inputSeller);
            entity.SellerId = 0;
            _dbContext.Add(entity);
        }
        // GET: Items/Edit/5
        public IActionResult Edit(int id)
        {
            // we pass the current values into the Edit view
            // so that the input fields can be pre-populated instead of blank
            // (important for good UX)
            Domain.Model.Seller seller = RepoSell.GetSellerById(id);
            var viewModel = new SellerViewModel
            {
                SellerId      = seller.Id,
                SellerName    = seller.Name,
                Items         = seller.Items.Count,
                AverageReview = seller.Rating
            };

            return(View(viewModel));
        }