Esempio n. 1
0
        public async Task <IActionResult> Edit(ProducViewModel view)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // aqui le estoy diciendo que me guarde la imagen original
                    var path = view.ImageUrl;

                    // imagefile es la nueva foto.. si el usuario selecciono una nueva foto
                    if (view.ImageFile != null && view.ImageFile.Length > 0)
                    {
                        var guid = Guid.NewGuid().ToString();
                        var file = $"{guid}.jpg";

                        path = Path.Combine(
                            Directory.GetCurrentDirectory(),
                            "wwwroot\\images\\Products",
                            file); //aqui le estoy concatenando el nombre a la ruta para saber donde esta la imagen

                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            await view.ImageFile.CopyToAsync(stream);
                        }
                        // aqui le estoy diciendo que tome la ruta relativa segun el ambiente
                        //donde lo estoy ejecutando

                        path = $"~/images/Products/{file}";
                    }

                    // estoy conviritnedo el producto a una view para poder mandarlo con la ruta
                    var product = this.ToProduct(view, path);


                    // TODO: Pending to change to: this.User.Identity.Name
                    product.User = await this.userHelper.GetUserByEmailAsync("this.User.Identity.Name");

                    await this.productRepository.UpdateAsync(product);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!await this.productRepository.ExistAsync(view.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            return(View(view));
        }
Esempio n. 2
0
 private Product ToProduct(ProducViewModel view, string path)
 {
     return(new Product
     {
         Id = view.Id,
         ImageUrl = path,
         IsAvailabe = view.IsAvailabe,
         LastPurchase = view.LastPurchase,
         LastSale = view.LastSale,
         Name = view.Name,
         Price = view.Price,
         Stock = view.Stock,
         User = view.User
     });
 }
Esempio n. 3
0
        public async Task <IActionResult> Create(ProducViewModel view)
        {
            if (ModelState.IsValid)
            {
                var path = string.Empty;

                if (view.ImageFile != null && view.ImageFile.Length > 0)
                {
                    var guid = Guid.NewGuid().ToString();
                    var file = $"{guid}.jpg";

                    path = Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "wwwroot\\images\\Products",
                        file); //aqui le estoy concatenando el nombre a la ruta para saber donde esta la imagen

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await view.ImageFile.CopyToAsync(stream);
                    }
                    // aqui le estoy diciendo que tome la ruta relativa segun el ambiente
                    //donde lo estoy ejecutando

                    path = $"~/images/Products/{file}";
                }

                // estoy conviritnedo el producto a una view para poder mandarlo con la ruta
                var product = this.ToProduct(view, path);

                // TODO: Pending to change to: this.User.Identity.Name CAMBIO HECHO!
                product.User = await this.userHelper.GetUserByEmailAsync("this.User.Identity.Name");

                await this.productRepository.CreateAsync(product);

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

            return(View(view));
        }