public IHttpActionResult GetHouses(HouseSearchRequest search) { //throw new NotImplementedException("To do"); // #01 get the result from projection HouseSearchResponse responseObject = HouseProjections.GetHouseList(search); // #02 send back 200 OK with response return(Ok(responseObject)); }
public static HouseSearchResponse GetHouseList(HouseSearchRequest filter) { using (DatabaseContext context = new DatabaseContext()) { HouseSearchResponse houseSearchResponse = new HouseSearchResponse(); #if DEBUG context.Database.Log = delegate(string s) { Debug.WriteLine(s); }; #endif // #01 get IQueryable with filter applied, not paging yet var result = context.Houses; if (!string.IsNullOrEmpty(filter.City)) { result.Where(x => x.Address.City.Contains(filter.City)).Where(x => x.Rent.Equals(filter.TypeRent)); } if (!string.IsNullOrEmpty(filter.Street)) { result.Where(x => x.Address.Address2.Contains(filter.Street)).Where(x => x.Rent.Equals(filter.TypeRent)); } if (!string.IsNullOrEmpty(filter.Address)) { result.Where(x => x.Address.Address1.Contains(filter.Address)).Where(x => x.Rent.Equals(filter.TypeRent)); } if (!string.IsNullOrEmpty(filter.ZipCode)) { result.Where(x => x.Address.ZipCode.Contains(filter.ZipCode)).Where(x => x.Rent.Equals(filter.TypeRent)); } if (!(filter.Price < 0)) { result.Where(x => x.Price.Equals(filter.Price)).Where(x => x.Rent.Equals(filter.TypeRent)); } // #02 get total count with filter applied var totalRows = result.Count(); // #03 get record using paging var items = result .OrderBy(x => x.Code) .Skip(filter.Skip) .Take(filter.Take) .Select(x => new HouseListItem { Id = x.Id, Code = x.Code, Price = x.Price, Address = x.Address.Address1 + " " + x.Address.Address2 + " " + x.Address.City + " " + x.Address.Country + " " + x.Address.Province }); houseSearchResponse.Items = items.ToList(); houseSearchResponse.TotalRecords = totalRows; return(houseSearchResponse); } }