public BooksForGridResult GetBooks() { using (var db = new Entities.LibraryContext()) { var result = new BooksForGridResult { aaData = db.Books .Select(a => new BookForGrid { Author = a.AuthorName, Category = a.Categories.Category.EnglishName, Description = a.Description, Language = a.Languages.EnglishName, PublishingYear = a.PublishingYear, SubCategory = a.Categories.EnglishName, Title = a.Title, CreatedBy = a.CreatedByUser.DisplayName, CreatedOn = a.CreationDate.ToString(), ModifiedBy = a.ModifiedByUser.DisplayName, ModifiedOn = a.ModificationDate.HasValue ? a.ModificationDate.Value.ToString() : "", Id = a.Id }).ToList(), Success = true }; var serialNo = 1; result.aaData.ForEach(a => a.SerialNo = serialNo++); return(result); } }
public SearchBooksResult GetBooksForTiles(TilesSearchModel searchModel) { using (var db = new Entities.LibraryContext()) { var query = db.Books.AsQueryable(); if (searchModel.SubCategoryId > 0) { query = query.Where(a => a.SubCategoryId == searchModel.SubCategoryId); } if (searchModel.ParentCategoryId > 0) { query = query.Where(a => a.Categories.ParentCategoryId == searchModel.ParentCategoryId); } if (!string.IsNullOrEmpty(searchModel.Title)) { query = query.Where(a => a.Title.ToLower().Contains(searchModel.Title.ToLower())); } var result = new SearchBooksResult { Success = true, Books = query .Select(a => new BookForSearchViewModel { description = a.Description, img_title = a.Title, tags = new List <string> { a.Categories.EnglishName }, title = a.Title }).ToList() }; return(result); } }
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 }); } }
/// <summary> /// Add Log /// </summary> /// <param name="error"></param> /// <returns></returns> public void AddLog(ErrorViewModel error) { using (var db = new Entities.LibraryContext()) { db.ErrorLogs.Add(new Entities.Shared.ErrorLogs { ClassName = error.ClassName, Date = error.Date, Paramaters = JsonConvert.SerializeObject(error.Parameters), Message = JsonConvert.SerializeObject(error.Message), MethodName = error.MethodName, UserId = error.UserId }); } }
public IdNameResult GetLanguages() { using (var db = new Entities.LibraryContext()) { return(new IdNameResult { Success = true, Entities = db.Languages .Where(a => a.IsActive) .Select(a => new IdNameViewModel { Name = a.EnglishName, Id = a.Id }).OrderBy(a => a.Name).ToList() }); } }
public IdNameResult GetParentCategories(string userId) { using (var db = new Entities.LibraryContext()) { return(new IdNameResult { Success = true, Entities = db.Categories .Where(a => !a.ParentCategoryId.HasValue || a.ParentCategoryId.Value == 0) .Select(a => new IdNameViewModel { Name = a.EnglishName, Id = a.Id }).OrderBy(a => a.Name).ToList() }); } }
public CategoryForEditResult GetCategory(long id, string userId) { using (var db = new Entities.LibraryContext()) { return(new CategoryForEditResult { Success = true, Category = db.Categories .Where(a => a.Id == id) .Select(a => new CategoryViewModel { ArabicName = a.ArabicName, EnglishName = a.EnglishName, Id = a.Id, ParentCategoryId = a.ParentCategoryId }).FirstOrDefault() }); } }
public CategoriesForGridResult GetCategories(DataTablesParam param, string userId) { using (var db = new Entities.LibraryContext()) { var query = db.Categories.AsQueryable(); var totalCount = query.Count(); //Sorting query = SortList(query, param.iSortCol, param.iSortDir); //Searching query = SearchList(query, param.search); //Paging if (param.iDisplayLength > 0) { query = query.Skip(param.iDisplayStart).Take(param.iDisplayLength); } var data = query.Select(a => new CategoryForGrid { ArabicName = a.ArabicName, BooksCount = a.Books.Count, CreatedBy = a.CreatedByUser.DisplayName, CreatedOn = a.CreationDate.ToString(), EnglishName = a.EnglishName, Id = a.Id, ModifiedBy = a.ModifiedByUser.DisplayName, ModifiedOn = a.ModificationDate.ToString() }).ToList(); var serialNo = 1; data.ForEach(a => a.SerialNo = serialNo++); var result = new CategoriesForGridResult { Success = true, iTotalRecords = totalCount, iTotalDisplayRecords = data.Count, aaData = data }; return(result); } }
public IdNameResult GetSubCategories(long ParentCategoryId, string userId) { using (var db = new Entities.LibraryContext()) { var query = db.Categories.AsQueryable(); if (ParentCategoryId > 0) { query = query.Where(a => a.ParentCategoryId.Value == ParentCategoryId); } return(new IdNameResult { Success = true, Entities = query .Where(a => a.ParentCategoryId.HasValue && a.ParentCategoryId.Value > 0) .Select(a => new IdNameViewModel { Name = a.EnglishName, Id = a.Id }).OrderBy(a => a.Name).ToList() }); } }
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 }); } } }
public BookForEditResult GetBook(long id, string userId) { using (var db = new Entities.LibraryContext()) { return(new BookForEditResult { Success = true, Book = db.Books .Where(a => a.Id == id) .Select(a => new BookViewModel { Id = a.Id, Year = a.PublishingYear, SubCategoryId = a.SubCategoryId, AuthorName = a.AuthorName, PagesCount = a.PagesCount, Description = a.Description, LanguageId = a.EditionLanguage, ParentCategoryId = a.Categories.ParentCategoryId.Value, Title = a.Title }).FirstOrDefault() }); } }