public ActionResult BooksCreate([DataSourceRequest]DataSourceRequest request, BookViewModel book) { if (ModelState.IsValid) { var db = new LibraryDbContext(); Book newBook = new Book() { Description = book.Description, Author = book.Author, ISBN = book.ISBN, Title = book.Title, Website = book.Website }; var category = db.Categories.FirstOrDefault(x => x.Name == book.CategoryName); category = CreateOrGetCategory(book, db, newBook, category); newBook.Id = db.Books.Add(newBook).Id; db.SaveChanges(); book.Id = newBook.Id; } return Json(new[] { book }.ToDataSourceResult(request, ModelState)); }
// // GET: /Admin/Books/ public ActionResult Index() { var db = new LibraryDbContext(); var found = db.Books.ToList(); return View(found); }
public IEnumerable<Book> ListViewSearchResults_GetData([QueryString] string q) { var db = new LibraryDbContext(); if (string.IsNullOrEmpty(q)) { return db.Books .Include("Category") .OrderBy(b => b.Title) .ThenBy(b => b.Author) .ToList(); } else { return db.Books .Include("Category") .Where(b => b.Title.ToLower().Contains(q.ToLower()) || b.Author.ToLower().Contains(q.ToLower()) || b.Category.Name.ToLower().Contains(q.ToLower())) .OrderBy(b => b.Title) .ThenBy(b => b.Author) .ToList(); } }
// The id parameter name should match the DataKeyNames value set on the control public void BooksListView_DeleteItem(int Id) { var db = new LibraryDbContext(); var selected = db.Books.Find(Id); db.Books.Remove(selected); db.SaveChanges(); }
public LibrarySampleData(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager, LibraryDbContext context, IRandomUserMeService randomUserMeService, IRandomTextService randomTextService) { _userManager = userManager; _roleManager = roleManager; _context = context; _randomUserMeService = randomUserMeService; _randomTextService = randomTextService; }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); Database.SetInitializer(new MigrateDatabaseToLatestVersion<LibraryDbContext, Configuration>()); var db = new LibraryDbContext(); db.Database.Initialize(true); }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewCategories_DeleteItem(int id) { var db = new LibraryDbContext(); Category item = db.Categories.FirstOrDefault(c => c.Id == id); if (item != null) { db.Categories.Remove(item); db.SaveChanges(); } }
public void CategoriesListView_InsertItem() { var item = new Category(); TryUpdateModel(item); if (ModelState.IsValid) { var db = new LibraryDbContext(); db.Categories.Add(item); db.SaveChanges(); } }
public void BooksListView_InsertItem() { var item = new Book(); TryUpdateModel(item); if (ModelState.IsValid) { var db = new LibraryDbContext(); db.Books.Add(item); db.SaveChanges(); } }
public ActionResult CategoriesRead([DataSourceRequest]DataSourceRequest request) { var db = new LibraryDbContext(); IQueryable<Category> found = db.Categories; var viewModelCategories = found.ConvertToCategoryViewModel().ToList(); DataSourceResult result = viewModelCategories.ToDataSourceResult(request); return Json(result); }
private bool ShouldRemoveItem(string menuText) { var dbContext = new LibraryDbContext(); var user = Context.User.Identity.Name; //var user = dbContext.Users.Find(userId); if (menuText == "Logout" && string.IsNullOrEmpty(user)) { return true; } if (menuText == "Books" && string.IsNullOrEmpty(user)) { return true; } if (menuText == "Categories" && string.IsNullOrEmpty(user)) { return true; } //if (user != null) //{ // if (menuText == "Admin" && user.Roles.Count == 1) // { // return true; // } // if (menuText == "Logs" && user.Roles.Count == 1) // { // return true; // } // if (menuText == "My tickets" && user.Roles.Count == 2) // { // return true; // } //} if (menuText == "Login" && user != null) { return true; } if (menuText == "Register" && user != null) { return true; } return false; }
public AccountController( UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, IEmailSender emailSender, ISmsSender smsSender, LibraryDbContext applicationDbContext) { _userManager = userManager; _signInManager = signInManager; _emailSender = emailSender; _smsSender = smsSender; _applicationDbContext = applicationDbContext; }
// The id parameter name should match the DataKeyNames value set on the control public void BooksListView_UpdateItem(int Id) { var db = new LibraryDbContext(); Book item = db.Books.Find(Id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", Id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); } }
private static Category CreateOrGetCategory(BookViewModel book, LibraryDbContext db, Book newBook, Category category) { if (category == null) { category = db.Categories.Add(new Category() { Name = book.CategoryName }); category.Books = new HashSet<Book>(); category.Books.Add(newBook); } else { newBook.Category = category; } return category; }
public ActionResult CategoriesUpdate([DataSourceRequest]DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); var newCategory = new Category() { Id = category.Id, Name = category.Name }; db.Categories.Attach(newCategory); db.Entry(newCategory).State = EntityState.Modified; db.SaveChanges(); } return Json(new[] { category }.ToDataSourceResult(request, ModelState)); }
public ActionResult CategoriesCreate([DataSourceRequest]DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); Category newCategory = new Category() { Name = category.Name }; newCategory.Id = db.Categories.Add(newCategory).Id; db.SaveChanges(); category.Id = newCategory.Id; } return Json(new[] { category }.ToDataSourceResult(request, ModelState)); }
public ActionResult CategoriesDelete([DataSourceRequest]DataSourceRequest request, CategoryViewModel category) { if (ModelState.IsValid) { var db = new LibraryDbContext(); var toDelete = new Category() { Id = category.Id, Name = category.Name }; db.Categories.Attach(toDelete); db.Categories.Remove(toDelete); db.SaveChanges(); } return Json(new[] { category }.ToDataSourceResult(request, ModelState)); }
public ActionResult BooksRead([DataSourceRequest]DataSourceRequest request) { var db = new LibraryDbContext(); IQueryable<Book> found = db.Books; var viewModelBooks = found.ConvertToBookViewModel().ToList(); foreach (var item in viewModelBooks) { if (item.Description != null && item.Description.Length > 20) { item.Description = item.Description.Substring(0, 20); } } DataSourceResult result = viewModelBooks.ToDataSourceResult(request); return Json(result); }
// The id parameter name should match the DataKeyNames value set on the control public void GridViewCategories_UpdateItem(int id) { var db = new LibraryDbContext(); Category item = db.Categories.FirstOrDefault(c => c.Id == id); // Load the item here, e.g. item = MyDataLayer.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category modified"); db.SaveChanges(); } }
public IEnumerable<Category> Categories_GetData() { var db = new LibraryDbContext(); return db.Categories.OrderBy(b => b.Name).ToList(); }
public LibraryRepository(LibraryDbContext dbContext) { _dbContext = dbContext; }
public Books() { this.dbContext = new LibraryDbContext(); }
// The return type can be changed to IEnumerable, however to support // paging and sorting, the following parameters must be added: // int maximumRows // int startRowIndex // out int totalRowCount // string sortByExpression public IQueryable<Book> BooksListView_GetData() { var db = new LibraryDbContext(); return db.Books.OrderBy(b => b.Id).Include(b => b.Category); }
public AuthorRepo(LibraryDbContext context) : base(context) { _context = context; }
// The return type can be changed to IEnumerable, however to support // paging and sorting, the following parameters must be added: // int maximumRows // int startRowIndex // out int totalRowCount // string sortByExpression public IQueryable<Category> CategoriesListView_GetData() { var db = new LibraryDbContext(); return db.Categories.OrderBy(c => c.Id); }
// The id parameter should match the DataKeyNames value set on the control // or be decorated with a value provider attribute, e.g. [QueryString]int id public WebFormsTemplate.Models.Book FormViewBook_GetItem([QueryString] int? id) { var db = new LibraryDbContext(); return db.Books.FirstOrDefault(b => b.Id == id); }
// The return type can be changed to IEnumerable, however to support // paging and sorting, the following parameters must be added: // int maximumRows // int startRowIndex // out int totalRowCount // string sortByExpression public IQueryable<Category> ListViewCategories_GetData() { var db = new LibraryDbContext(); return db.Categories.Include("Books"); }
// The id parameter should match the DataKeyNames value set on the control // or be decorated with a value provider attribute, e.g. [QueryString]int id public Book BookFormView_GetItem([QueryString("id")]int? id) { var db = new LibraryDbContext(); return db.Books.FirstOrDefault(b => b.Id == id); }
public BookDetails() { this.dbContext = new LibraryDbContext(); }
public EditBooks() { this.dbContext = new LibraryDbContext(); }
// The following code creates the database and schema if they don't exist. // This is a temporary workaround since deploying database through EF migrations is // not yet supported in this release. // Please see this http://go.microsoft.com/fwlink/?LinkID=615859 for more information on how to do deploy the database // when publishing your application. private static void EnsureDatabaseCreated(LibraryDbContext context) { if (!_databaseChecked) { _databaseChecked = true; context.Database.Migrate(); } }
public LibrarySampleData(LibraryDbContext context) { _context = context; }
public InvoiceRepository(LibraryDbContext dbContext) => _dbContext = dbContext;
public LibraryViewComponent(LibraryDbContext libraryContext) { _libraryContext = libraryContext; }
public CategoriesController(LibraryDbContext context) { _db = context; }