public static string add_Perfil(Perfil d) { return "{" + '"' + "IdPerfil" + '"' + ": " + d.IdPerfil.ToString() + ',' + '"' + "Nombre" + '"' + ": " + '"' + d.Nombre + '"' + ',' + '"' + "Activo" + '"' + ": " + (d.Activo ? "true" : "false") + ',' + '"' + "FechaCreacion" + '"' + ": " + '"' + Utils.dateToJson(d.FechaCreacion) + '"' + "}"; }
public ActionResult Create(string nombre, int[] selectedPerfiles, bool estado = false, int tienda = 0) { //Declaraciones Generales para los request ASCIIEncoding encoding = new ASCIIEncoding(); HttpWebRequest webrequest; HttpWebResponse webresponse; byte[] data; Stream newStream; ResponseBD u = new ResponseBD(); //Select Perfiles List<Perfil> p = new List<Perfil>(); encoding = new ASCIIEncoding(); data = encoding.GetBytes(""); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectAll_Perfil); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); p = (List<Perfil>)js.Deserialize(objText, typeof(List<Perfil>)); } //Obtener Perfiles seleccionados List<Perfil> sp = (List<Perfil>)Session["selectedPerfiles"]; if (selectedPerfiles == null) { //Guardar Contraseña int idContrasena = 1; //Guardar Usuario Usuario usu = new Usuario() { IdUsuario = 0, Nombre = nombre, IdContrasena = idContrasena, Activo = estado, FechaCreacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture), UltimaModificacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture), }; data = encoding.GetBytes(JsonSerializer.add_Usuario(usu)); Debug.WriteLine(JsonSerializer.add_Usuario(usu)); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_Usuario); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); u = (ResponseBD)js.Deserialize(objText, typeof(ResponseBD)); } if (u.Flujo.Equals(Constantes.OK)) { int idUsuario = Int32.Parse(u.Mensaje); int i = 0; foreach (var per in sp) { PerfilXUsuario pxu = new PerfilXUsuario() { IdUsuario = idUsuario, IdPerfil = per.IdPerfil, IdPerfilXUsuario = 0, FechaAsignacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture) }; data = encoding.GetBytes(JsonSerializer.add_PerfilXUsuario(pxu)); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_PerfilXUsuario); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); u = (ResponseBD)js.Deserialize(objText, typeof(ResponseBD)); } if (u.Flujo.Equals("ERROR")) { ViewBag.Mensaje = u.Mensaje; ViewBag.Productos = sp; return View(p); } i++; } return RedirectToAction("Index"); } //Select Tienda data = encoding.GetBytes(""); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Combo_Tienda); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); ViewBag.Tienda = (List<Tienda>)js.Deserialize(objText, typeof(List<Tienda>)); } ViewBag.Mensaje = u.Mensaje; ViewBag.Perfiles = p; return View(sp); } foreach (var item in selectedPerfiles) { Perfil prd = new Perfil(); data = encoding.GetBytes(JsonSerializer.selectById_Perfil(item)); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectById_Perfil); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); prd = (Perfil)js.Deserialize(objText, typeof(Perfil)); } sp.Add(prd); } foreach (var i in sp) { p.RemoveAll(s => s.IdPerfil == i.IdPerfil); } ViewBag.Perfiles = p; Session["selectedPerfiles"] = sp; //Select Tienda data = encoding.GetBytes(""); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectAll_Tienda); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); ViewBag.Tienda = (List<Tienda>)js.Deserialize(objText, typeof(List<Tienda>)); } return View(sp); }
public ActionResult Create(string nombre, int[] selectedRoles, bool estado= false) { //Declaraciones Generales para los request ASCIIEncoding encoding = new ASCIIEncoding(); HttpWebRequest webrequest; HttpWebResponse webresponse; byte[] data; Stream newStream; ResponseBD u = new ResponseBD(); //Seleccciono Roles List<Rol> p = new List<Rol>(); data = encoding.GetBytes(""); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectAll_Rol); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); p = (List<Rol>)js.Deserialize(objText, typeof(List<Rol>)); } //Obtener Roles seleccionados List<Rol> sp = (List<Rol>)Session["selectedRoles"]; if (selectedRoles == null) { //Guardar Perfil Perfil per = new Perfil() { IdPerfil = 0, Nombre = nombre, Activo = estado, FechaCreacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture), UltimaModificacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture), }; data = encoding.GetBytes(JsonSerializer.add_Perfil(per)); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_Perfil); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); u = (ResponseBD)js.Deserialize(objText, typeof(ResponseBD)); } if (u.Flujo.Equals(Constantes.OK)) { int idPerfil = Int32.Parse(u.Mensaje); //Agregar Productos seleccionados int i = 0; foreach (var rol in sp) { RolXPerfil rxp = new RolXPerfil() { IdRol = rol.IdRol, IdPerfil = idPerfil, IdRolXPerfil = 0, FechaAsignacion = DateTime.ParseExact(DateTime.Now.ToString("dd/MM/yyyy"), "dd/MM/yyyy", CultureInfo.InvariantCulture) }; data = encoding.GetBytes(JsonSerializer.add_RolXPerfil(rxp)); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.Add_RolXPerfil); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); u = (ResponseBD)js.Deserialize(objText, typeof(ResponseBD)); } if (u.Flujo.Equals("ERROR")) { ViewBag.Mensaje = u.Mensaje; ViewBag.Productos = sp; return View(p); } i++; } return RedirectToAction("Index", "Usuario"); } else { ViewBag.Mensaje = u.Mensaje; ViewBag.Perfiles = p; return View(sp); } } foreach (var item in selectedRoles) { Rol rol = new Rol(); data = encoding.GetBytes(JsonSerializer.selectById_Rol(item)); webrequest = (HttpWebRequest)WebRequest.Create(Constantes.SelectById_Rol); webrequest.Method = Constantes.PostMethod; webrequest.ContentType = Constantes.ContentType; webrequest.ContentLength = data.Length; newStream = webrequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); webresponse = (HttpWebResponse)webrequest.GetResponse(); using (var reader = new StreamReader(webresponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objText = reader.ReadToEnd(); rol = (Rol)js.Deserialize(objText, typeof(Rol)); } sp.Add(rol); } foreach (var i in sp) { p.RemoveAll(s => s.IdRol == i.IdRol); } ViewBag.Roles = p; Session["selectedProducts"] = sp; return View(sp); }