Пример #1
0
        public async Task <IActionResult> Create(ProductoCategoriaVM productoCategoriaVM, int?idVendedor, List <IFormFile> Imagen1, List <IFormFile> Imagen2, List <IFormFile> Imagen3)
        {
            Producto producto = new Producto()
            {
                Titulo       = productoCategoriaVM.Producto.Titulo,
                Descripcion  = productoCategoriaVM.Producto.Descripcion,
                Condiciones  = productoCategoriaVM.Producto.Condiciones,
                FechaValidez = productoCategoriaVM.Producto.FechaValidez
            };
            await _productosService.CreateProductoPost(producto);

            var img1 = await _imagenesProductosService.AgregarImagenesBlob(Imagen1);

            var img2 = await _imagenesProductosService.AgregarImagenesBlob(Imagen2);

            var img3 = await _imagenesProductosService.AgregarImagenesBlob(Imagen3);

            ImagenesProducto imagenes = new ImagenesProducto()
            {
                Imagen1    = img1,
                Imagen2    = img2,
                Imagen3    = img3,
                ProductoId = producto.Id
            };

            ProductoCategoria newProdCat = new ProductoCategoria()
            {
                CategoriaId = productoCategoriaVM.Categoria.Id,
                ProductoId  = producto.Id
            };
            ProductoVendedor productoVendedor = new ProductoVendedor();

            productoVendedor.ProductoId = producto.Id;

            int aux = -1;

            if (idVendedor == null)
            {
                string   idUsuario = _userManager.GetUserId(User);
                Vendedor vendedor  = await _vendedoresService.GetVendedorByIdentityUserId(idUsuario);

                aux = vendedor.Id;
            }
            else if (idVendedor != null)
            {
                string aux2 = idVendedor.ToString();
                aux = int.Parse(aux2);
            }
            productoVendedor.VendedorId = aux;

            if (ModelState.IsValid)
            {
                await _imagenesProductosService.CreateImagenesProducto(imagenes);

                await _productosVendedoresService.CreateProductoVendedor(productoVendedor);

                await _productoCategoriasService.CreateProductoCategoriaPost(newProdCat);

                return(RedirectToAction("Create", "OpcionesProductos", new { productoId = producto.Id }));
            }

            _memoryCache.Remove("ProductosForIndex2");
            return(View(producto));
        }