示例#1
0
        // GET: Anuncios/Edit/5
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Anuncio anuncio = await db.Anuncios.FindAsync(id);

            Producto producto = await db.Productos.FindAsync(anuncio.ProductoId);

            ProductoAnuncio productoAnuncio = new ProductoAnuncio();

            productoAnuncio.Cantidad       = anuncio.Cantidad;
            productoAnuncio.Descripcion    = anuncio.Descripcion;
            productoAnuncio.Especificacion = producto.Especificacion;
            productoAnuncio.Etiqueta       = anuncio.Etiqueta;
            productoAnuncio.Nombre         = producto.Nombre;
            productoAnuncio.Precio         = anuncio.NuevoPrecio;
            productoAnuncio.Tipo           = producto.Tipo;
            productoAnuncio.ProductoId     = producto.ProductoId;
            productoAnuncio.AnuncioId      = anuncio.AnuncioId;
            if (anuncio == null)
            {
                return(HttpNotFound());
            }
            // ViewBag.ProductoId = new SelectList(db.Productos, "ProductoId", "Nombre", anuncio.ProductoId);
            // ViewBag.VendedorId = new SelectList(db.Vendedores, "VendedorId", "Nombre", anuncio.VendedorId);
            return(View(productoAnuncio));
        }
示例#2
0
        public ActionResult Details([Bind(Include = "Nombre,Tipo,Especificacion,Precio,Fecha,Cantidad,Calificacion,Etiqueta,Descripcion")] ProductoAnuncio productoanuncio, int cantidad)
        {
            Anuncio   anuncio   = db.Anuncios.Find(productoanuncio.AnuncioId);
            Comprador comprador = QuerysGeneric.GetComprador(User.Identity.Name, db.Compradores);

            if (anuncio.Cantidad < cantidad)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                anuncio.Cantidad = anuncio.Cantidad - cantidad;
                db.Carritos.Add(new Carrito {
                    Comprador = comprador, CompradorId = comprador.Id, Anuncio = anuncio, AnuncioId = anuncio.AnuncioId, Cantidad = cantidad
                });
                db.SaveChanges();
            }
            return(RedirectToAction("Index", "Carritos"));
        }
示例#3
0
        public async Task <ActionResult> Edit([Bind(Include = "ProductoId,AnuncioId,Nombre,Tipo,Especificacion,Precio,Fecha,Cantidad,Calificacion,Etiqueta,Descripcion")] ProductoAnuncio productoanuncio)
        {
            Anuncio anuncio = await db.Anuncios.FindAsync(productoanuncio.AnuncioId);

            anuncio.Cantidad    = productoanuncio.Cantidad;
            anuncio.Descripcion = productoanuncio.Descripcion;
            anuncio.Etiqueta    = productoanuncio.Etiqueta;
            anuncio.NuevoPrecio = productoanuncio.Precio;
            anuncio.Fecha       = DateTime.Now;
            anuncio.Vendedor    = QuerysGeneric.GetVendedor(User.Identity.Name, db.Vendedores);
            anuncio.VendedorId  = anuncio.Vendedor.Id;
            Producto producto = new Producto();

            producto.Nombre = productoanuncio.Nombre;
            producto.Tipo   = productoanuncio.Tipo;

            HttpPostedFileBase file = Request.Files["ImageData"];

            //anuncio.Image = ConvertToBytes(file);
            if (ModelState.IsValid)
            {
                List <Producto> productos = db.Productos.Where(m => (m.Nombre == producto.Nombre && m.Tipo == producto.Tipo)).ToList();
                if (productos.Count != 0)
                {
                    anuncio.Producto   = productos[0];
                    anuncio.ProductoId = anuncio.Producto.ProductoId;
                }
                else
                {
                    db.Productos.Add(producto);
                    anuncio.Producto   = producto;
                    anuncio.ProductoId = producto.ProductoId;
                }
                db.Entry(anuncio).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            // ViewBag.ProductoId = new SelectList(db.Productos, "ProductoId", "Nombre", anuncio.ProductoId);
            // ViewBag.VendedorId = new SelectList(db.Vendedores, "VendedorId", "Nombre", anuncio.VendedorId);
            return(View(anuncio));
        }
示例#4
0
        public async Task <ActionResult> Create([Bind(Include = "Nombre,Tipo,Especificacion,Precio,Fecha,Cantidad,Calificacion,Etiqueta,Descripcion")]
                                                ProductoAnuncio productoanuncio, HttpPostedFileBase upload)
        {
            Anuncio anuncio = new Anuncio();

            anuncio.Cantidad    = productoanuncio.Cantidad;
            anuncio.Descripcion = productoanuncio.Descripcion;
            anuncio.Etiqueta    = productoanuncio.Etiqueta;
            anuncio.Precio      = productoanuncio.Precio;
            anuncio.NuevoPrecio = anuncio.Precio;
            anuncio.Fecha       = DateTime.Now;
            anuncio.Vendedor    = QuerysGeneric.GetVendedor(User.Identity.Name, db.Vendedores);
            anuncio.VendedorId  = anuncio.Vendedor.Id;
            Producto producto = new Producto();

            producto.Nombre = productoanuncio.Nombre;
            producto.Tipo   = productoanuncio.Tipo;
            string fileContent     = string.Empty;
            string fileContentType = string.Empty;


            try
            {
                if (ModelState.IsValid)
                {
                    if (upload != null && upload.ContentLength > 0)
                    {
                        var avatar = new File
                        {
                            FileName    = System.IO.Path.GetFileName(upload.FileName),
                            FileType    = FileType.Avatar,
                            ContentType = upload.ContentType
                        };
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            avatar.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        anuncio.Files = new List <File> {
                            avatar
                        };
                    }
                    List <Producto> productos = db.Productos.Where(m => (m.Nombre == producto.Nombre && m.Tipo == producto.Tipo)).ToList();
                    if (productos.Count != 0)
                    {
                        anuncio.Producto   = productos[0];
                        anuncio.ProductoId = anuncio.Producto.ProductoId;
                    }
                    else
                    {
                        db.Productos.Add(producto);
                        anuncio.Producto   = producto;
                        anuncio.ProductoId = producto.ProductoId;
                    }
                    db.Anuncios.Add(anuncio);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return(View(anuncio));
        }