public Respuesta update([FromUri] int id, [FromBody] Promocion promocion_data) { Respuesta respuesta = new Respuesta(); respuesta.resultado = 0; if (id < 1 || promocion_data == null) { respuesta.mensaje = "No se recibieron datos."; return(respuesta); } respuesta.mensaje = Tools.VerifyToken(promocion_data.sesion, 29); if (respuesta.mensaje != "1") { return(respuesta); } try { using (MacEntities db = new MacEntities()) { var estatus = true; if (promocion_data.estatus == "Inactiva") { } tbl_promociones promocion = db.tbl_promociones.Find(id); if (promocion == null) { respuesta.mensaje = "No se encontro la promoción"; return(respuesta); } promocion.nombre = promocion_data.nombre; promocion.descripcion = promocion_data.descripcion; promocion.precio = promocion_data.precio; promocion.imagen = promocion_data.imagen; if (promocion_data.estatus == "Inactiva") { promocion.fecha_anulacion = DateTime.Now; estatus = false; } promocion.estatus = estatus; promocion.fecha_desde = Tools.GetDateTimeObject(promocion_data.fecha_desde); promocion.fecha_fin = Tools.GetDateTimeObject(promocion_data.fecha_fin); db.Entry(promocion).State = System.Data.Entity.EntityState.Modified; if (db.SaveChanges() < 1) { respuesta.mensaje = "Error, no se pudo actualizar la promoción"; } else { respuesta.mensaje = "La promoción fue actualizada exitosamente."; respuesta.resultado = 1; } } } catch (Exception ex) { respuesta.mensaje = "Error, intente mas tarde."; //respuesta.data = ex; } finally { } return(respuesta); }//Update
public Respuesta suspend([FromUri] int id, [FromBody] Promocion promocion_data) { Respuesta respuesta = new Respuesta(); respuesta.resultado = 0; respuesta.mensaje = Tools.VerifyToken(promocion_data.sesion, 29); if (respuesta.mensaje != "1") { return(respuesta); } try { using (MacEntities db = new MacEntities()) { var id_usuario_anulacion = (from usuario in db.tbl_usuarios where usuario.usuario == promocion_data.sesion.usuario && usuario.activo == true select usuario.id).FirstOrDefault(); if (id_usuario_anulacion == 0) { respuesta.mensaje = "El usuario no pudo ser encontrado."; return(respuesta); } tbl_promociones promocion = db.tbl_promociones.Find(id); if (promocion == null) { respuesta.mensaje = "La prmocion no pudo ser encontrada."; return(respuesta); } promocion.id_usuario_anulacion = id_usuario_anulacion; promocion.motivo_anulacion = promocion_data.motivo_anulacion; promocion.fecha_anulacion = DateTime.Now; promocion.estatus = false; db.Entry(promocion).State = System.Data.Entity.EntityState.Modified; if (db.SaveChanges() < 1) { respuesta.mensaje = "Error, no se pudo suspender la promoción"; } else { respuesta.mensaje = "La promoción fue suspendida exitosamente."; respuesta.resultado = 1; } } } catch (Exception ex) { respuesta.mensaje = "Error, intente mas tarde."; } finally { } return(respuesta); }//Update
public Respuesta store2() { Respuesta respuesta = new Respuesta() { resultado = 0 }; /*Token sesion = new Token() { * usuario = HttpContext.Current.Request.Form["u"], * token = HttpContext.Current.Request.Form["t"], * }; * respuesta.mensaje = Tools.VerifyToken(sesion, 29); * if (respuesta.mensaje != "1") { return respuesta; }*/ //FTP FILE UPLOAD if (HttpContext.Current.Request.Files.AllKeys.Any()) { // Get the uploaded image from the Files collection var httpPostedFile = HttpContext.Current.Request.Files["image"]; if (httpPostedFile != null) { // Validate the uploaded image(optional) //You could modify the following code and get the postedfile inputstream, then insert them into database. // Get the complete file path var fileSavePath = Path.Combine( HttpContext.Current.Server.MapPath("~/files"), httpPostedFile.FileName); // Save the uploaded file to "files" folder httpPostedFile.SaveAs(fileSavePath); FtpWebRequest request = (FtpWebRequest)WebRequest. Create("ftp://vocablo.edu.mx/promociones" + httpPostedFile.FileName); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential("vocablo", "v0c4bl0ftp"); /// byte[] fileContents; byte[] fileContents = File.ReadAllBytes(fileSavePath); /* using (StreamReader sourceStream = * new StreamReader(fileSavePath)) * { fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); }*/ request.ContentLength = fileContents.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(fileContents, 0, fileContents.Length); } try { // Check if file exists with its full path if (File.Exists(fileSavePath)) { // If file found, delete it File.Delete(fileSavePath); } else { respuesta.mensaje = "Archivo no encontrado."; return(respuesta); } } catch (IOException ioExp) { respuesta.mensaje = "hubo un error"; respuesta.data = ioExp; return(respuesta); } } else { respuesta.mensaje = "Imagen no encontrada."; return(respuesta); } } //FTP FILE UPLOAD END try { using (MacEntities db = new MacEntities()) { tbl_promociones promocion = new tbl_promociones(); promocion.nombre = HttpContext.Current.Request.Form["nombre"]; promocion.descripcion = HttpContext.Current.Request.Form["descripcion"]; promocion.precio = Decimal.Parse(HttpContext.Current.Request.Form["precio"]); promocion.fecha_desde = Tools.GetDateTimeObject( HttpContext.Current.Request.Form["fecha_desde"]); promocion.fecha_fin = Tools.GetDateTimeObject( HttpContext.Current.Request.Form["fecha_fin"]); promocion.imagen = HttpContext.Current.Request.Files["image"].FileName; promocion.estatus = true; db.tbl_promociones.Add(promocion); if (db.SaveChanges() < 1) { respuesta.mensaje = "Error, no se pudo agregar la promoción, intente más tarde."; } else { respuesta.resultado = 1; respuesta.mensaje = "La promoción fue agregada exitosamente."; } }//using } catch (DbEntityValidationException ex) { respuesta.mensaje = "Errores econtrados."; List <Error> error_list = new List <Error>(); foreach (var errors in ex.EntityValidationErrors) { foreach (var error in errors.ValidationErrors) { // get the error message error_list.Add(new Error(error.ErrorMessage)); } //inner foreach } //foreach respuesta.data = error_list; } //DbEntityValidationException ex catch (Exception ex) { respuesta.mensaje = "Error, intente mas tarde."; respuesta.data = ex; } finally { } return(respuesta); }///store2
public Respuesta store([FromBody] Promocion promocion_data) { Respuesta respuesta = new Respuesta() { resultado = 0 }; if (promocion_data == null) { respuesta.mensaje = "No se recibieron datos"; return(respuesta); } respuesta.mensaje = Tools.VerifyToken(promocion_data.sesion, 29); if (respuesta.mensaje != "1") { return(respuesta); } try { using (MacEntities db = new MacEntities()) { tbl_promociones promocion = new tbl_promociones(); promocion.nombre = promocion_data.nombre; promocion.descripcion = promocion_data.descripcion; promocion.precio = promocion_data.precio; promocion.imagen = promocion_data.imagen; promocion.fecha_desde = Tools.GetDateTimeObject(promocion_data.fecha_desde); promocion.fecha_fin = Tools.GetDateTimeObject(promocion_data.fecha_fin); db.tbl_promociones.Add(promocion); if (db.SaveChanges() < 1) { respuesta.mensaje = "Error, no se pudo agregar la promoción, intente más tarde."; } else { respuesta.resultado = 1; respuesta.mensaje = "La promoción fue agregada exitosamente."; } }//using } catch (DbEntityValidationException ex) { respuesta.mensaje = "Errores econtrados."; List <Error> error_list = new List <Error>(); foreach (var errors in ex.EntityValidationErrors) { foreach (var error in errors.ValidationErrors) { // get the error message error_list.Add(new Error(error.ErrorMessage)); } //inner foreach } //foreach respuesta.data = error_list; } //DbEntityValidationException ex catch (Exception ex) { respuesta.mensaje = "Error, intente mas tarde."; respuesta.data = ex; } finally { } return(respuesta); }///store