public ContentResult AddUser([FromBody] User user) { var success = false; User checkUser = new User(); Random number = new Random(); int IdNumber = number.Next(10, 100); checkUser = users.FirstOrDefault(u => u.Id == user.Id); if (checkUser == null && !users.Any(u => u.Id == IdNumber)) { user.Id = IdNumber; users.Add(user); string jsonString = JsonConvert.SerializeObject(users); json.Write("users.json", "./", jsonString); success = true; } else { success = false; } return(new ContentResult() { Content = JsonConvert.SerializeObject(new { success }), ContentType = "application/json" }); }
protected void EditProductButtonClick(object sender, EventArgs e) { int id = int.TryParse(ProductIdField.Value, out int result) ? result : 0; JSONReadWrite readWrite = new JSONReadWrite(); string jsonString = readWrite.Read("products.json", "Data"); List <Product> products = JsonConvert.DeserializeObject <List <Product> >(jsonString); Product product = products.Where(p => p.ProductId == id).FirstOrDefault(); if (product == null) { Response.Redirect("/Views/Products/ProductsPage.aspx"); } product.Title = ProductNameTextBox.Text; product.Description = ProductDescriptionTextBox.Text; product.Category = products.Select(s => s.Category).Where(c => c.CategoryName == CategoryDropDownList.SelectedValue).FirstOrDefault(); product.Supplier = products.Select(s => s.Supplier).Where(s => s.SupplierName == SupplierDropDownList.SelectedValue).FirstOrDefault(); product.Manufacturer = products.Select(s => s.Manufacturer).Where(m => m.ManufacturerName == ManufacturerDropDownList.SelectedValue).FirstOrDefault(); product.Price = decimal.TryParse(PriceTextBox.Text, out decimal priceResult) ? priceResult : 0; int index = products.FindIndex(f => f.ProductId == id); products[index] = product; string jsonWriteString = JsonConvert.SerializeObject(products); readWrite.Write("products.json", "Data", jsonWriteString); Response.Redirect("/Views/Products/ProductsPage.aspx"); }
public IActionResult Index(Reservation reservationModel) { List <Reservation> reservation = new List <Reservation>(); JSONReadWrite readWrite = new JSONReadWrite(); reservation = JsonConvert.DeserializeObject <List <Reservation> >(readWrite.Read("people.json", "data")); Reservation person = reservation.FirstOrDefault(x => x.Id == reservationModel.Id); if (person == null) { reservation.Add(reservationModel); } else { int index = reservation.FindIndex(x => x.Id == reservationModel.Id); reservation[index] = reservationModel; } string jSONString = JsonConvert.SerializeObject(reservation); readWrite.Write("people.json", "data", jSONString); return(Ok()); }
// public IActionResult Post(int id,string name,string surname, List<string> shoes,bool hungry,int age) public IActionResult PostPeople(Person personModel) { List <Person> people = new List <Person>(); JSONReadWrite readWrite = new JSONReadWrite(); people = JsonConvert.DeserializeObject <List <Person> >(readWrite.Read("people.json", "data")); // Person personModel = new Person { Id = id, Name = name, Surname = surname, Shoes = shoes, Hungry = hungry, Age = age }; Person person = people.FirstOrDefault(x => x.Id == personModel.Id); if (person == null) { people.Add(personModel); } else { int index = people.FindIndex(x => x.Id == personModel.Id); people[index] = personModel; } string jSONString = JsonConvert.SerializeObject(people); readWrite.Write("people.json", "data", jSONString); return(Ok()); }
public IActionResult Delete(int id) { List <HomeModel> employee = new List <HomeModel>(); JSONReadWrite readWrite = new JSONReadWrite(); employee = JsonConvert.DeserializeObject <List <HomeModel> >(readWrite.Read("employee.json", "data")); int index = employee.FindIndex(x => x.Id == id); employee.RemoveAt(index); string jSONString = JsonConvert.SerializeObject(employee); readWrite.Write("employee.json", "data", jSONString); return(RedirectToAction("Index", "Home")); }
public IActionResult Delete(int id) { List <Reservation> reservation = new List <Reservation>(); JSONReadWrite readWrite = new JSONReadWrite(); reservation = JsonConvert.DeserializeObject <List <Reservation> >(readWrite.Read("people.json", "data")); int index = reservation.FindIndex(x => x.Id == id); reservation.RemoveAt(index); string jSONString = JsonConvert.SerializeObject(reservation); readWrite.Write("people.json", "data", jSONString); return(RedirectToAction("index", "Person")); }
public IActionResult Add(HomeModel personModel) { List <HomeModel> employee = new List <HomeModel>(); JSONReadWrite readWrite = new JSONReadWrite(); employee = JsonConvert.DeserializeObject <List <HomeModel> >(readWrite.Read("employee.json", "data")); HomeModel person = employee.FirstOrDefault(x => x.Id == personModel.Id); if (person == null) { employee.Add(personModel); } else { ViewData["Message"] = "Modified data of employee."; int index = employee.FindIndex(x => x.Id == personModel.Id); employee[index] = personModel; } string jSONString = JsonConvert.SerializeObject(employee); readWrite.Write("employee.json", "data", jSONString); return(RedirectToAction("Index", "Home")); }
protected void AddProductButtonClick(object sender, EventArgs e) { JSONReadWrite readWrite = new JSONReadWrite(); string jsonString = readWrite.Read("products.json", "Data"); List <Product> products = JsonConvert.DeserializeObject <List <Product> >(jsonString); Product product = new Product(); product.ProductId = products.Count + 1; product.Title = ProductNameTextBox.Text; product.Description = ProductDescriptionTextBox.Text; product.Category = products.Select(s => s.Category).Where(c => c.CategoryName == CategoryDropDownList.SelectedValue).FirstOrDefault(); product.Supplier = products.Select(s => s.Supplier).Where(s => s.SupplierName == SupplierDropDownList.SelectedValue).FirstOrDefault(); product.Manufacturer = products.Select(s => s.Manufacturer).Where(m => m.ManufacturerName == ManufacturerDropDownList.SelectedValue).FirstOrDefault(); product.Price = decimal.TryParse(PriceTextBox.Text, out decimal result) ? result : 0; products.Add(product); string jsonWriteString = JsonConvert.SerializeObject(products); readWrite.Write("products.json", "Data", jsonWriteString); Response.Redirect("/Views/Products/ProductsPage.aspx"); }