示例#1
0
        public async Task <IActionResult> AddNew(ToyPostData toyPost)
        {
            if (!ModelState.IsValid)
            {
                AddError("Model not valid");
                return(View(toyPost));
            }

            toyPost.Seller = await UserRoles.GetUser(User, _userManager);

            toyPost.Category = await _context.Categories.FirstAsync(c => c.ID == toyPost.categoryID);

            _context.Toys.Add(new Toy(toyPost));

            await _context.SaveChangesAsync();

            AddInfo($"Toy {toyPost.Name} added");

            return(RedirectToAction("Index", "Home"));
        }
示例#2
0
        public async Task <IActionResult> EditToy(ToyPostData toy)
        {
            var toyFromDb = await _context.Toys
                            .Include(p => p.Seller)
                            .FirstOrDefaultAsync(p => p.ID == toy.ID);

            if (toyFromDb == null)
            {
                AddError($"Toy with ID {toy.ID} Doesn't exist");
                return(RedirectToAction("Index", "Home"));
            }

            var toyCategory = await _context.Categories.FirstOrDefaultAsync(c => c.ID == toy.categoryID);

            if (toyCategory == null)
            {
                AddError($"Category with ID {toy.categoryID} Doesn't exist");
                return(RedirectToAction("Index", "Home"));
            }

            if (toyFromDb.Seller.Id != User.GetUserId() &&
                !User.IsAdmin())
            {
                AddError($"Only the seller of the item or an admin can update a toy's details");
                return(RedirectToAction("Index", "Home"));
            }

            toyFromDb.Description = toy.Description;
            toyFromDb.ImageUrl    = toy.ImageUrl;
            toyFromDb.Name        = toy.Name;
            toyFromDb.Price       = toy.Price;
            toyFromDb.Available   = toy.Available;
            toyFromDb.Category    = toyCategory;

            await _context.SaveChangesAsync();

            AddInfo($"Toy {toy.Name} succefully updated");

            return(RedirectToAction("Index", "Home"));
        }
示例#3
0
 public ToyPostData(ToyPostData data) : this((Toy)data)
 {
     this.categoryID = data.categoryID;
 }