public bool PaidAdSearchCriteria(Ad a, AdQueryModel query) { bool categoryBool = query.Category != null ? (query.Category.Id == a.CategoryId) : true; bool cityBool = query.City != null ? (query.City.Id == a.CityId) : true; return(categoryBool && cityBool && a.IsPaid.Value); }
private IEnumerable <AdModel> GetPaidAds(AdQueryModel data, SearchHelper helper) { IEnumerable <AdModel> adModels = new List <AdModel>(); try { if (data != null) { //take random two paid ads var ads = unitOfWork.AdRepository.Get(a => helper.PaidAdSearchCriteria(a, data)).OrderBy(arg => Guid.NewGuid()).Take(2); foreach (Ad ad in ads) { ad.ViewCount++; if (ad.ViewCount == Config.PaidAdViewCount) { HandleExpiredPaidAds(ad); } unitOfWork.AdRepository.Update(ad); } unitOfWork.Save(); adModels = ads.Select(x => Mapper.Map <AdModel>(x)); } return(adModels); } catch { return(adModels); } }
public bool AdSearchCriteria(Ad a, AdQueryModel query) { bool categoryBool = query.Category != null ? (query.Category.Id == a.CategoryId) : true; bool cityBool = query.City != null ? (query.City.Id == a.CityId) : true; bool keywordBool = !string.IsNullOrWhiteSpace(query.Keyword) ? EvaluateKeywords(query.Keyword, a.Name, a.Description) : true; return(categoryBool && cityBool && keywordBool); }
public HttpResponseMessage PostAds(AdQueryModel data, string sort = "date", string dir = "desc", string items = Util.ITEMNUMBER, string pageno = Util.PAGENUMBER) { AdsResult result = new AdsResult(); IEnumerable <AdModel> adModel; SearchHelper helper = new SearchHelper(); int noOfItems = Util.parseQStringNumber(items); int pageNumber = Util.parseQStringNumber(pageno, true); SortBy sortBy = Util.parseQstringSortBy(sort); SortDirection sortDir = Util.parseQstringSortDirection(dir); try { if (data == null) { adModel = unitOfWork.AdRepository.Get().Select(x => Mapper.Map <AdModel>(x)); } else { adModel = unitOfWork.AdRepository.Get(a => helper.AdSearchCriteria(a, data)).Select(x => Mapper.Map <AdModel>(x)); } if (adModel != null) { //picking up total number of records before we do sorting and get results only for one page result.numberOfResults = adModel.Count(); adModel = (helper.SortResults(sortBy, sortDir, noOfItems, pageNumber, adModel)); result.adModel = AddImagePaths(adModel); result.paidAds = AddImagePaths(GetPaidAds(data, helper)); return(OK(result)); } else { return(NotFound("Error occured. Could not query database.")); } } catch (ObjectNotFoundException) { return(NotFound("Ad not found.")); } catch (Exception ex) { return(InternalServerError("Error occured. Could not query database.", ex)); } }