/// <summary> /// Search product with paging /// </summary> /// <param name="websiteId"></param> /// <param name="searchModel"></param> /// <returns></returns> public async Task <PaginationResponse <List <ProductModel> > > SearchAsync(int websiteId, SearchModel <ProductSearchModel> searchModel) { var repository = this.unitOfWork.GetRepository <Product>(); var queryString = searchModel.Data.Name?.ToUpper() ?? string.Empty; var(productSearchModel, pagingContext) = searchModel.Extract(); Expression <Func <Product, bool> > predicate = x => x.WebsiteId == websiteId && (x.Category.Name.ToUpper().Contains(queryString) || x.Description.ToUpper().Contains(queryString) || x.MetaData.ToUpper().Contains(queryString) || x.Name.ToUpper().Contains(queryString) || x.ProductCode.ToUpper().Contains(queryString) || x.SpecificData.ToUpper().Contains(queryString) ) && (productSearchModel.MinPrice == null || x.Price >= productSearchModel.MinPrice) && (productSearchModel.MaxPrice == null || x.Price <= productSearchModel.MaxPrice) && (productSearchModel.CategoryIds == null || productSearchModel.CategoryIds.Length == 0 || productSearchModel.CategoryIds.Contains(x.CategoryId)); var includes = new[] { nameof(ProductImage) }; var products = await repository.GetAllAsync(predicate, pagingContext, includes); var totalRecord = await repository.CountAsync(predicate); return(new PaginationResponse <List <ProductModel> >( mapper.Map <List <ProductModel> >(products), totalRecord, searchModel.NumberPerPage)); }
/// <summary> /// Search website /// </summary> /// <param name="searchModel"></param> /// <returns></returns> public async Task <PaginationResponse <List <WebsiteModel> > > SearchAsync(SearchModel <WebsiteSearchModel> searchModel) { var websiteRepository = unitOfWork.GetRepository <Website>(); //Extract search model generate website search model and paging context var(websiteSearchModel, pagingContext) = searchModel.Extract(); Expression <Func <Website, bool> > predicate = x => (websiteSearchModel.Name == null || x.Name.ToLower().Contains(websiteSearchModel.Name.ToLower())); var websites = await websiteRepository.GetAllAsync(predicate, pagingContext, new[] { "Company" }); var totalRecord = await websiteRepository.CountAsync(predicate); return(new PaginationResponse <List <WebsiteModel> >( mapper.Map <List <WebsiteModel> >(websites), totalRecord, searchModel.NumberPerPage)); }
/// <summary> /// Search categories /// </summary> /// <param name="websiteId">site id</param> /// <param name="searchModel">category model search</param> /// <returns>response list category</returns> public async Task <PaginationResponse <List <CategoryModel> > > SearchAsync(int websiteId, SearchModel <CategorySearchModel> searchModel) { var categoryRepository = this.unitOfWork.GetRepository <Category>(); //Extract search model generate category search model and paging context var(categorySearchModel, pagingContext) = searchModel.Extract(); Expression <Func <Category, bool> > predicate = x => (searchModel.Data.Name == null || x.Name.ToLower().Contains(categorySearchModel.Name.ToLower()) && x.WebsiteId == websiteId); var categories = await categoryRepository.GetAllAsync(predicate, pagingContext); var totalRecord = await categoryRepository.CountAsync(predicate); return(new PaginationResponse <List <CategoryModel> >( mapper.Map <List <CategoryModel> >(categories), totalRecord, searchModel.NumberPerPage)); }
/// <summary> /// Search company /// </summary> /// <param name="searchModel"></param> /// <returns></returns> public async Task <PaginationResponse <List <CompanyModel> > > SearchAsync(SearchModel <CompanySearchModel> searchModel) { var companyRepository = unitOfWork.GetRepository <Company>(); //Extract search model generate company search model and paging context var(companySearchModel, pagingContext) = searchModel.Extract(); Expression <Func <Company, bool> > predicate = x => (companySearchModel.Name == null || x.Name.ToLower().Contains(companySearchModel.Name.ToLower())) && (companySearchModel.Address == null || x.Address.ToLower().Contains(companySearchModel.Address.ToLower())) && (companySearchModel.Phone == null || x.Phone.Contains(companySearchModel.Phone)) && (companySearchModel.Status == null || x.Status == companySearchModel.Status); var companies = await companyRepository.GetAllAsync(predicate, pagingContext); var totalRecord = await companyRepository.CountAsync(predicate); return(new PaginationResponse <List <CompanyModel> >( mapper.Map <List <CompanyModel> >(companies), totalRecord, searchModel.NumberPerPage)); }
/// <summary> /// Search users /// </summary> /// <param name="websiteId"></param> /// <param name="searchModel"></param> /// <returns></returns> public async Task <PaginationResponse <List <UserModel> > > SearchAsync(int?websiteId, SearchModel <UserSearchModel> searchModel) { var roleId = websiteId == null ? (int)Role.Administrator : (int)Role.User; var userRepository = unitOfWork.GetRepository <User>(); //Extract search model generate user search model and paging context var(userSearchModel, pagingContext) = searchModel.Extract(); Expression <Func <User, bool> > predicate = x => (userSearchModel.DisplayName == null || (x.LastName + " " + x.FirstName).Contains(userSearchModel.DisplayName.Trim().ToLower())) && x.RoleId == roleId && (roleId == (int)Role.Administrator || x.WebsiteId == websiteId); var users = await userRepository.GetAllAsync(predicate, pagingContext); var totalRecord = await userRepository.CountAsync(predicate); return(new PaginationResponse <List <UserModel> >( mapper.Map <List <UserModel> >(users), totalRecord, searchModel.NumberPerPage)); }
/// <summary> /// Search orders /// </summary> /// <param name="websiteId"></param> /// <param name="searchModel"></param> /// <returns></returns> public async Task <PaginationResponse <List <OrderModel> > > SearchAsync(int?websiteId, SearchModel <OrderSearchModel> searchModel) { var roleId = websiteId == null ? (int)Role.Administrator : (int)Role.User; var orderRepository = unitOfWork.GetRepository <Order>(); //Extract search model generate order search model and paging context var(orderSearchModel, pagingContext) = searchModel.Extract(); Expression <Func <Order, bool> > predicate = x => (orderSearchModel.UserDisplayName == null || x.User.FirstName.ToLower().Contains(orderSearchModel.UserDisplayName.ToLower()) || x.User.LastName.ToLower().Contains(orderSearchModel.UserDisplayName.ToLower())) && (!orderSearchModel.Id.HasValue || x.Id == orderSearchModel.Id) && (roleId == (int)Role.Administrator || x.WebsiteId == websiteId); var includes = new[] { nameof(User), nameof(OrderDetail) }; var orders = await orderRepository.GetAllAsync(predicate, pagingContext, includes); var totalRecord = await orderRepository.CountAsync(predicate); return(new PaginationResponse <List <OrderModel> >( mapper.Map <List <OrderModel> >(orders), totalRecord, searchModel.NumberPerPage)); }