public async Task <IActionResult> Create([FromForm] ProductInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(BadRequest());
            }

            var product = inputModel.ToEntity();

            if (null != inputModel.Photo)
            {
                string imagePath = await _fs.Create(inputModel.Photo.OpenReadStream(),
                                                    FileTypes.ProductPhoto,
                                                    _fs.CreateDefaultFileName(inputModel.Photo.FileName));

                product.Photo = imagePath;
            }

            product = _store.Create(product);

            var outputModel = ProductOutputModel.FromEntity(product);

            return(CreatedAtRoute("ViewProduct",
                                  new { id = outputModel.Id }, outputModel));
        }
Beispiel #2
0
        public IActionResult Create([FromForm] ProductInputModel inputModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(inputModel));
            }

            var product = inputModel.ToEntity();


            product = _store.Create(product);

            var outputModel = ProductOutputModel.FromEntity(product);

            return(RedirectToAction(nameof(Index)));
        }
Beispiel #3
0
        public IActionResult Create([FromBody] ProductInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(BadRequest());
            }

            var product = inputModel.ToEntity();


            product = _store.Create(product);

            var outputModel = ProductOutputModel.FromEntity(product);

            var result = CreatedAtRoute("ViewProduct",
                                        new { id = outputModel.Id }, outputModel);

            return(result);
        }
Beispiel #4
0
        public IActionResult Edit(int id, [FromForm] ProductInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(View());
            }
            if (!ModelState.IsValid)
            {
                return(View(inputModel.ToEntity()));
            }

            int productId = id;

            try
            {
                var product = _store.GetById(productId);
                if (null == product)
                {
                    return(NotFound());
                }

                product.Name        = inputModel.Name;
                product.Description = inputModel.Description;
                product.Price       = inputModel.Price;


                _store.Update(product);
                productId = product.Id;
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_store.Exists(productId))
                {
                    return(NotFound());
                }

                throw;
            }

            return(RedirectToAction(nameof(Index)));
        }