public ActionResult Editar(int?Id_Producto, int?cantidad_producto) { try { BDContext context = new BDContext(); // obtener el id del usuario que esta logueado var userID = User.Identity.GetUserId(); // obtener el usuario con ese id de base de datos var usuario_BD = context.usuarios.Where(u => u.Usuario_ID.Equals(userID)).Single(); carrito carrito = new carrito { productoId = (int)Id_Producto, userId = usuario_BD.userId, cantidad_producto = (int)cantidad_producto, fecha_modificado = DateTime.UtcNow }; unidad_carrito.genericDAL.Update(carrito); unidad_carrito.Complete(); return(new HttpStatusCodeResult(HttpStatusCode.OK)); } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } }
public ActionResult Agregar(DetallesProductoViewModels productoVM) { BDContext context = new BDContext(); try { // obtener el id del usuario que esta logueado var userID = User.Identity.GetUserId(); // obtener el usuario con ese id de base de datos var usuario_BD = context.usuarios.Where(u => u.Usuario_ID.Equals(userID)).Single(); // obtener productos del carrito del usuario List <sp_obtenerProductosUsuarioCarrito_Result> lista_productos_cliente = context.sp_obtenerProductosUsuarioCarrito(usuario_BD.userId).ToList(); // verificar si el usuario ya tiene ese producto en el carrito foreach (var producto in lista_productos_cliente) { if (producto.productoId == productoVM.Id_Producto) { // usuario ya tiene ese producto en el carrito, preguntar si desea modificar return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } } // crear el producto que se va a agregar al carrito carrito producto_carrito = new carrito { productoId = productoVM.Id_Producto, userId = usuario_BD.userId, cantidad_producto = (int)productoVM.cantidad, fecha_agregado = DateTime.UtcNow, fecha_modificado = DateTime.UtcNow }; unidad_carrito.genericDAL.Add(producto_carrito); unidad_carrito.Complete(); // devolver http 200 return(new HttpStatusCodeResult(HttpStatusCode.OK)); } catch (Exception) { // error, devolver http 500 return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } }
protected void Page_Load(object sender, EventArgs e) { int id = Convert.ToInt32(Session["id"]); String nick = Convert.ToString(Session["user"]); prop = nick; /*****cliente******/ String filename = Server.MapPath("../App_Data/Clientes.txt"); FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(stream); while (reader.Peek() > -1) { Comprador ctemp = new Comprador(); ctemp.Nombre = reader.ReadLine(); ctemp.Nit = Convert.ToInt32(reader.ReadLine()); ctemp.Telefono = Convert.ToInt32(reader.ReadLine()); ctemp.Password = reader.ReadLine(); ctemp.Ciudad = reader.ReadLine(); ctemp.Id = Convert.ToInt32(reader.ReadLine()); ctemp.Credito = Convert.ToInt32(reader.ReadLine()); cliente.Add(ctemp); } reader.Close(); /*****busca la informacion del usuario correcto*****/ for (int i = 0; i < cliente.Count; i++) { if (id == cliente[i].Id) { name.Text = cliente[i].Nombre; nit.Text = Convert.ToString(cliente[i].Nit); phone.Text = Convert.ToString(cliente[i].Telefono); ciudad.Text = cliente[i].Ciudad; credito.Text = Convert.ToString(cliente[i].Credito); } } /*****informacion personal******/ String fname = Server.MapPath("../App_Data/db/carrito.txt"); FileStream cstream = new FileStream(fname, FileMode.Open, FileAccess.Read); StreamReader creader = new StreamReader(cstream); /****enlistar los datos del carrito****/ while (creader.Peek() > -1) { carrito cartemp = new carrito(); cartemp.Nombre = creader.ReadLine(); cartemp.Precio = Convert.ToInt32(creader.ReadLine()); cartemp.Cantidad = Convert.ToInt32(creader.ReadLine()); cartemp.Id = Convert.ToInt32(creader.ReadLine()); cartemp.Total = cartemp.Cantidad * cartemp.Precio; cartemp.Comprador = creader.ReadLine(); carrito.Add(cartemp); } creader.Close(); /*****muestra solo los elemntos del carrito propios*****/ for (int i = 0; i < carrito.Count; i++) { if (name.Text == carrito[i].Comprador && carrito[i].Id < 10000) { carrito cartemp = new carrito(); cartemp.Nombre = carrito[i].Nombre; cartemp.Precio = carrito[i].Precio; cartemp.Cantidad = carrito[i].Cantidad; cartemp.Id = carrito[i].Id; cartemp.Total = carrito[i].Total; cartemp.Comprador = "yo"; mio.Add(cartemp); } } carritoDG.DataSource = mio; carritoDG.DataBind(); /*****muestra solo los elemntos del carrito propios*****/ for (int i = 0; i < carrito.Count; i++) { if (name.Text == carrito[i].Comprador && carrito[i].Id > 9999) { carrito cartemp = new carrito(); cartemp.Nombre = carrito[i].Nombre; cartemp.Precio = carrito[i].Precio; cartemp.Cantidad = carrito[i].Cantidad; cartemp.Id = carrito[i].Id; cartemp.Total = carrito[i].Total; cartemp.Comprador = "yo"; compras.Add(cartemp); } } comprasDG.DataSource = compras; comprasDG.DataBind(); }