private void crearJsonOrder() { orderDta mnu = new orderDta(); mnu.order = new List <orders>(); orders order = new orders(); order.mesa = "1"; order.orden = "ejemplo"; order.fecha_hora = DateTime.Now.ToString(); mnu.order.Add(order); //Obtengo el directorio para el archivo var service = HttpContext.RequestServices.GetService(typeof(Microsoft.AspNetCore.Hosting.IHostingEnvironment)) as Microsoft.AspNetCore.Hosting.IHostingEnvironment; string folderName = "json/"; string webRootPath = service.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); //Serializo el objeto a json string objeto = JsonConvert.SerializeObject(mnu); //Escribo en disco el archivo System.IO.File.WriteAllText(newPath + @"/orders.json", objeto); }
public void OnGet() { // Cargo el json en la entidad de ordenes var service = HttpContext.RequestServices.GetService(typeof(Microsoft.AspNetCore.Hosting.IHostingEnvironment)) as Microsoft.AspNetCore.Hosting.IHostingEnvironment; string folderName = "json/"; string webRootPath = service.WebRootPath; string Path = System.IO.Path.Combine(webRootPath, folderName); // Cargo el json en la entidad orderDta od = JsonConvert.DeserializeObject <orderDta>(System.IO.File.ReadAllText(Path + @"/orders.json")); // cargo el objeto json en el objeto del modelo ordersD = od; }
private bool guardarPedido(string[] pedidos, string id, string side) { try{ string pedido = ""; orderDta orders = new orderDta(); // Abro el objeto json e inserto el nuevo pedido var service = HttpContext.RequestServices.GetService(typeof(Microsoft.AspNetCore.Hosting.IHostingEnvironment)) as Microsoft.AspNetCore.Hosting.IHostingEnvironment; string folderName = "json/"; string webRootPath = service.WebRootPath; string Path = System.IO.Path.Combine(webRootPath, folderName); // Cargo el json en la entidad orderDta od = JsonConvert.DeserializeObject <orderDta>(System.IO.File.ReadAllText(Path + @"/orders.json")); orders = od; orders order = new orders(); order.mesa = id; order.fecha_hora = DateTime.Now.ToString(); order.entregado = false; order.side = side; foreach (string m in pedidos) { if (!m.Trim().Equals("")) { pedido += m.Trim() + "<br>"; } } order.orden = pedido; od.order.Add(order); // vuelvo a guardar el objeto en el documento json. string objeto = JsonConvert.SerializeObject(od); System.IO.File.WriteAllText(Path + @"/orders.json", objeto); // Uso el inmemory para guardar el pedido de esa mesa y que no vuelvan a hacerlo hasta 3000 seg despues. var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(300)); memoryCache.Set("mesa_" + id + side, pedido, cacheEntryOptions); return(true); } catch (Exception ex) { return(false); } }
private bool existePedido(string id, string side) { orderDta orders = new orderDta(); // Abro el objeto json e inserto el nuevo pedido var service = HttpContext.RequestServices.GetService(typeof(Microsoft.AspNetCore.Hosting.IHostingEnvironment)) as Microsoft.AspNetCore.Hosting.IHostingEnvironment; string folderName = "json/"; string webRootPath = service.WebRootPath; string Path = System.IO.Path.Combine(webRootPath, folderName); // Cargo el json en la entidad orderDta od = JsonConvert.DeserializeObject <orderDta>(System.IO.File.ReadAllText(Path + @"/orders.json")); var o = od.order.Where(x => (x.mesa == id && x.side == side)); return(o.Count() > 0); }
public void OnPost() { string mesa = Request.Form[nameof(idMesa)]; string side = Request.Form[nameof(idSide)]; // Cargo el json en la entidad de ordenes var service = HttpContext.RequestServices.GetService(typeof(Microsoft.AspNetCore.Hosting.IHostingEnvironment)) as Microsoft.AspNetCore.Hosting.IHostingEnvironment; string folderName = "json/"; string webRootPath = service.WebRootPath; string Path = System.IO.Path.Combine(webRootPath, folderName); // Cargo el json en la entidad orderDta od = JsonConvert.DeserializeObject <orderDta>(System.IO.File.ReadAllText(Path + @"/orders.json")); // Elimino el pedido de la mesa enviada od.order.RemoveAll(x => (x.mesa == mesa && x.side == side)); // Vuelvo a guardar el objeto serializado string objeto = JsonConvert.SerializeObject(od); System.IO.File.WriteAllText(Path + @"/orders.json", objeto); ordersD = od; }