public BaseResult DeleteBook(long id, string userId) { using (var db = new Entities.LibraryContext()) { var book = db.Books .Where(a => a.Id == id) .FirstOrDefault(); if (book != null) { db.Entry(book).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); return(new BaseResult { Success = true, Message = Resource.DeletedSuccessfully }); } else { return(new BaseResult { Success = true, Message = Resource.ItemNotFound }); } } }
public BaseResult SaveBook(BookViewModel book, string userId) { using (var db = new Entities.LibraryContext()) { if (book.Id == 0)//New { db.Books.Add(new Entities.BooksManagement.Books { Title = book.Title, AuthorName = book.AuthorName, Description = book.Description, EditionLanguage = book.LanguageId, PagesCount = book.PagesCount, SubCategoryId = book.SubCategoryId, PublishingYear = book.Year, CreationDate = DateTime.Now, CreatedBy = userId, ModificationDate = DateTime.Now, ModifiedBy = userId }); } else { var bookModel = db.Books .Where(a => a.Id == book.Id) .FirstOrDefault(); if (bookModel == null) { return new BaseResult { Message = Resource.ItemNotFound } } ; bookModel.Title = book.Title; bookModel.AuthorName = book.AuthorName; bookModel.Description = book.Description; bookModel.EditionLanguage = book.LanguageId; bookModel.PagesCount = book.PagesCount; bookModel.SubCategoryId = book.SubCategoryId; bookModel.PublishingYear = book.Year; bookModel.ModificationDate = DateTime.Now; bookModel.ModifiedBy = userId; db.Entry(bookModel).State = System.Data.Entity.EntityState.Modified; } db.Configuration.ValidateOnSaveEnabled = false; db.SaveChanges(); db.Configuration.ValidateOnSaveEnabled = true; return(new BaseResult { Success = true, Message = Resource.SuccessfullySaved }); } }
public BaseResult SaveCategory(CategoryViewModel category, string userId) { if (category.ParentCategoryId == 0) { category.ParentCategoryId = null; } using (var db = new Entities.LibraryContext()) { if (category.Id == 0)//New { db.Categories.Add(new Entities.BooksManagement.Categories { ArabicName = category.ArabicName, EnglishName = category.EnglishName, CreationDate = DateTime.Now, CreatedBy = userId, ModificationDate = DateTime.Now, ModifiedBy = userId, ParentCategoryId = category.ParentCategoryId > 0 ? category.ParentCategoryId : null }); } else { var categoryModel = db.Categories .Where(a => a.Id == category.Id) .FirstOrDefault(); if (categoryModel == null) { return new BaseResult { Message = Resource.ItemNotFound } } ; categoryModel.ArabicName = category.ArabicName; categoryModel.EnglishName = category.EnglishName; categoryModel.ModifiedBy = userId; categoryModel.ModificationDate = DateTime.Now; categoryModel.ParentCategoryId = category.ParentCategoryId; db.Entry(categoryModel).State = System.Data.Entity.EntityState.Modified; } db.Configuration.ValidateOnSaveEnabled = false; db.SaveChanges(); db.Configuration.ValidateOnSaveEnabled = true; return(new BaseResult { Success = true, Message = Resource.SuccessfullySaved }); } }
public BaseResult DeleteCategory(long id, string userId) { using (var db = new Entities.LibraryContext()) { var category = db.Categories .Where(a => a.Id == id) .FirstOrDefault(); if (category != null) { if (category.Books.Any() || category.SubCategories.Any()) { return new BaseResult { Success = true, Message = "Category cannot be deleted as there're SubCategories/Books attached to it" } } ; db.Entry(category).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); return(new BaseResult { Success = true, Message = Resource.DeletedSuccessfully }); } else { return(new BaseResult { Success = true, Message = Resource.ItemNotFound }); } } }