public ActionResult UpdateToOrders(string type, string id, string mode) { try { ViewBag.Type = type; ViewBag.Mode = mode; if (type == "buy") { ViewBag.Element = manager.GetContext().BuyOrders.Find(Convert.ToInt32(id)); } else { ViewBag.Element = manager.GetContext().ReadingOrders.Find(Convert.ToInt32(id)); } Book book = manager.GetContext().Lib.Find(ViewBag.Element.Id_book); ViewBag.Id_empl = manager.GetContext().Employees.Find(ViewBag.Element.Id_employee).Id; ViewBag.Employee = manager.GetContext().Employees.Find(ViewBag.Element.Id_employee).FIO; ViewBag.Books = manager.GetContext().Lib; List <string> names = new List <string>(); foreach (var em in manager.GetContext().Employees) { names.Add(em.FIO); } ViewBag.ListEmployees = names; ViewBag.BookName = book.Book_title; ViewBag.Short_desc = book.Short_description; if (mode == "delete") { if (type == "buy") { var order = manager.GetContext().BuyOrders.Find(Convert.ToInt32(Request.Params["id"])); var innerCount = manager.GetContext().ReadingOrders.Where(s => s.Id_book == order.Id_book); if (innerCount.Count() > 0) { foreach (var el in innerCount) { DeleteHelper.FromReadingOrder(el.Id); } } DeleteHelper.FromBuyOrder(order.Id); DeleteHelper.FromBook(manager.GetContext().Lib.Find(order.Id_book).Id); } else { DeleteHelper.FromReadingOrder(Convert.ToInt32(id)); } string message = "Запись удалена"; Response.Redirect("/Home/Orders?message=" + message + "&type=" + type); } return(View()); } catch (Exception ex) { string message = "Не удалось обновить запись"; Response.Redirect("/Home/Orders?message=" + message + "&type=" + type); return(View()); } }
public void AddBook(string id, string type, string name, string desc) { string message = ""; if (type == "add") { try { DBManager manager = new DBManager(); var book = manager.GetContext().Lib.Find(Convert.ToInt32(id)); UpdateHelper.FromBook(Convert.ToInt32(id), book.Book_title, book.Short_description, Convert.ToString(book.Number_copies + 1), book.How_many_times.ToString()); message = "Запись успешно обновлена"; } catch (Exception ex) { message = "Не удалось обновить запись. Попробуйте еще раз"; } } else if (type == "new") { try { DBManager manager = new DBManager(); Book book = new Book { Book_title = name, Short_description = desc, Number_copies = 1, How_many_times = 0 }; InsertHelper.FromBook(name, desc, Convert.ToString(1), Convert.ToString(0)); int idBook = manager.GetContext().Lib.First(s => s.Book_title == name).Id; DBManager man2 = new DBManager(); var order = man2.GetContext().BuyOrders.Where(s => s.Id_book == idBook); if (order.Count() > 0) { foreach (var el in order) { DeleteHelper.FromBuyOrder(el.Id); } } message = "Запись успешно обновлена"; } catch (Exception ex) { message = "Не удалось обновить запись. Попробуйте еще раз"; } } else { message = "Не удалось обновить запись. Попробуйте еще раз"; } Response.Redirect("/Home/Add?message=" + message); }