Пример #1
0
        public IActionResult Upsert(ToyViewModel toyViewModel)
        {
            if (toyViewModel == null)
            {
                return(RedirectToAction(nameof(Index), "Toys"));
            }

            if (!ModelState.IsValid)
            {
                IEnumerable <Shop> shopList = _db.Shops.ToList();
                toyViewModel = new ToyViewModel()
                {
                    Toy   = new Toy(),
                    Shops = shopList.Select(I => new SelectListItem()
                    {
                        Text  = I.Location,
                        Value = I.Id.ToString()
                    })
                };
                return(View(toyViewModel));
            }

            if (toyViewModel.Toy.Id == 0)
            {
                _db.Toys.Add(toyViewModel.Toy);
            }
            else
            {
                _db.Toys.Update(toyViewModel.Toy);
            }

            _db.SaveChanges();
            return(RedirectToAction(nameof(Index)));
        }
Пример #2
0
        public ActionResult Create(ToyViewModel toyVM)//[Bind(Include = "ToyID,ForAge,Price,TypeOfToyID,ImageUrl,Avaibility,YearOfManufactire")]
        {
            var toy = new Toy
            {
                NameOfToy         = toyVM.Toy.NameOfToy,
                Price             = toyVM.Toy.Price,
                ForAge            = toyVM.Toy.ForAge,
                ToyDescription    = toyVM.Toy.ToyDescription,
                Avaibility        = toyVM.Toy.Avaibility,
                ImageUrl          = toyVM.Toy.ImageUrl,
                YearOfManufactire = toyVM.Toy.YearOfManufactire,
                //ToyID=toyVM.Toy.ToyID,
                TypeOfToyID = toyVM.Toy.TypeOfToyID,
                TypeOfToy   = toyVM.Toy.TypeOfToy
            };

            if (ModelState.IsValid)
            {
                db.Toys.Add(toy);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            //var model = new ToyViewModel
            //{
            //    Toy = toy,
            //    TypeOfToys = db.ToysType.ToList()
            //};

            // ViewBag.TypeOfToyID = new SelectList(db.ToysType, "TypeOfToyID", "Name", toy.TypeOfToyID);

            toyVM.TypeOfToys = db.ToysType.ToList();
            return(View(toyVM));
        }
Пример #3
0
        [ValidateInput(false)]                       //if there is description
        public ActionResult Edit(ToyViewModel toyVM) //[Bind(Include = "ToyID,ForAge,Price,TypeOfToyID,ImageUrl,Avaibility,YearOfManufactire")]
        {
            var toy = new Toy
            {
                ToyID             = toyVM.Toy.ToyID,
                NameOfToy         = toyVM.Toy.NameOfToy,
                Price             = toyVM.Toy.Price,
                ToyDescription    = toyVM.Toy.ToyDescription,
                ForAge            = toyVM.Toy.ForAge,
                Avaibility        = toyVM.Toy.Avaibility,
                ImageUrl          = toyVM.Toy.ImageUrl,
                YearOfManufactire = toyVM.Toy.YearOfManufactire,
                TypeOfToyID       = toyVM.Toy.TypeOfToyID,
                TypeOfToy         = toyVM.Toy.TypeOfToy
            };

            if (ModelState.IsValid)
            {
                db.Entry(toy).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            toyVM.TypeOfToys = db.ToysType.ToList();
            // ViewBag.TypeOfToyID = new SelectList(db.ToysType, "TypeOfToyID", "Name", toy.TypeOfToyID);
            return(View(toy));// return View(toyVM);
        }
Пример #4
0
        public async Task <IActionResult> PutToy(int id, ToyViewModel toyModel)
        {
            if (id != toyModel.Id)
            {
                return(BadRequest());
            }

            var toy = new Toy
            {
                Id    = id,
                Name  = toyModel.Name,
                Price = Currency.Parse(toyModel.Price)
            };

            _context.Entry(toy).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ToyExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
        }
Пример #5
0
        public IActionResult Upsert(int?id)
        {
            IEnumerable <Shop> shopList     = _db.Shops.ToList();
            ToyViewModel       toyViewModel = new ToyViewModel()
            {
                Toy   = new Toy(),
                Shops = shopList.Select(I => new SelectListItem
                {
                    Text  = I.Location,
                    Value = I.Id.ToString()
                })
            };

            if (id == null)
            {
                return(View(toyViewModel));
            }

            toyViewModel.Toy = _db.Toys.Find(id);
            if (toyViewModel.Toy == null)
            {
                return(NotFound());
            }

            return(View(toyViewModel));
        }
Пример #6
0
        public IActionResult Create(ToyViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.Toy.Producer = _producerRepository.GetProducerByID(model.Producer);
                model.Toy.Category = _categoryRepository.GetCategoryById(model.Category);

                _toyRepository.AddToy(model.Toy);

                if (model.Images != null)
                {
                    var helper = new ImageHelper(_environment);

                    foreach (var image in model.Images)
                    {
                        var imageName = helper.AddImage(image);
                        var gallery   = new Gallery()
                        {
                            FileName = imageName, Toy = model.Toy
                        };

                        _galleryRepository.AddImage(gallery);
                    }
                }

                _toyRepository.Save();
                return(RedirectToAction("Index"));
            }
            else
            {
                model.Categories = FormHelper.GetFormCategories(_context.Categories.ToArray());
                model.Producers  = FormHelper.GetFormProducers(_context.Producers.ToArray());
            }
            return(View(model));
        }
Пример #7
0
 public ToyView()
 {
     InitializeComponent();
     _toyViewModel      = new ToyViewModel();
     DataContext        = _toyViewModel;
     CbActive.IsChecked = true;
     ImageDefault(_imageDefault);
 }
Пример #8
0
        // GET: Toys/Create
        public ActionResult Create()
        {
            // ViewBag.TypeOfToyID = new SelectList(db.ToysType, "TypeOfToyID", "Name");
            var typeoftoy = db.ToysType.ToList();
            var model     = new ToyViewModel
            {
                TypeOfToys = typeoftoy
            };

            return(View(model));
        }
Пример #9
0
        public async Task AddNewToy(ToyViewModel model)
        {
            try
            {
                await _context.Toys.AddAsync(_mapper.Map <Toy>(model));

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #10
0
        public async Task <IActionResult> PostLendTableEntry(ToyViewModel toyModel)
        {
            var toy = new Toy
            {
                Name  = toyModel.Name,
                Price = Currency.Parse(toyModel.Price)
            };

            _context.Toy.Add(toy);
            await _context.SaveChangesAsync();

            return(Ok());
        }
Пример #11
0
        // GET: Item/Create
        public IActionResult Create()
        {
            var producers  = FormHelper.GetFormProducers(_context.Producers.ToArray());
            var categories = FormHelper.GetFormCategories(_context.Categories.ToArray());

            var vm = new ToyViewModel()
            {
                Producers  = producers,
                Categories = categories,
                Conditions = FormHelper.GetFormCondition()
            };

            return(View(vm));
        }
Пример #12
0
        public async Task <ActionResult <ToyViewModel> > GetToy(int id)
        {
            var toy = await _context.Toy.FindAsync(id);

            if (toy == null)
            {
                return(NotFound());
            }

            var result = new ToyViewModel
            {
                Id    = toy.Id,
                Name  = toy.Name,
                Price = Currency.Format(toy.Price)
            };

            return(Ok(result));
        }
Пример #13
0
        public async Task <IActionResult> AddToy([FromBody] ToyViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                await _toyService.AddNewToy(model);

                return(Ok("New toy was added!"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Пример #14
0
        // GET: Toys/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Toy toy = db.Toys.Find(id);

            if (toy == null)
            {
                return(HttpNotFound());
            }
            var model = new ToyViewModel
            {
                Toy        = toy,
                TypeOfToys = db.ToysType.ToList()
            };

            return(View(model));
        }
Пример #15
0
        // GET: Toys/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Toy toy = db.Toys.Find(id);

            if (toy == null)
            {
                return(HttpNotFound());
            }
            var model = new ToyViewModel
            {
                Toy        = toy,
                TypeOfToys = db.ToysType.ToList()
            };

            // ViewBag.TypeOfToyID = new SelectList(db.ToysType, "TypeOfToyID", "Name", toy.TypeOfToyID);
            return(View(model));
        }
Пример #16
0
        // GET: Item/Edit/5
        public IActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var _toy = _toyRepository.GetToyById(id.Value);

            var model = new ToyViewModel()
            {
                Toy        = _toy,
                Producers  = FormHelper.GetFormProducers(_producerRepository.GetProducers()),
                Categories = FormHelper.GetFormCategories(_categoryRepository.GetCategories()),
                Producer   = _toy.Producer.Id,
                Category   = _toy.Category.Id,
                Conditions = FormHelper.GetFormCondition(),
                Condition  = (int)_toy.Condition
            };

            return(View(model));
        }
Пример #17
0
        public IActionResult Edit(int id, ToyViewModel model)
        {
            if (id != model.Toy.ToyID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                model.Toy.Producer  = _producerRepository.GetProducerByID(model.Producer);
                model.Toy.Category  = _categoryRepository.GetCategoryById(model.Category);
                model.Toy.Condition = (Condition)model.Condition;

                _context.Update(model.Toy);

                if (model.Images != null)
                {
                    var helper = new ImageHelper(_environment);

                    foreach (var image in model.Images)
                    {
                        var imageName = helper.AddImage(image);
                        var gallery   = new Gallery()
                        {
                            FileName = imageName, Toy = model.Toy
                        };

                        _galleryRepository.AddImage(gallery);
                    }
                }

                _toyRepository.Save();

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }