i have worked in one application and in that required filter for search spaces. now table model is like below.
public class Listing { public int ID { get; set; } public string Title { get; set; } public Nullable< int> NoOfSpaces { get; set; } public Nullable<int> PricePerDay { get; set; } public bool ShowPricePerDay { get; set; } public Nullable<int> PricePerWeek { get; set; } public bool ShowPricePerWeek { get; set; } public Nullable<int> PricePerMonth { get; set; } public bool ShowPricePerMonth { get; set; } public Nullable<double> PricePerHour { get; set; } public bool ShowPricePerHour { get; set; } }
now in my front-end side filter by either hour/day/week/month. for that i have bind drop-down from enum and my enum like.
public enum ListingAvailableBy { Hour, Day, Week, Month }
currently my search method
public async Task<IEnumerable<Listing>> GetSearchResult(SearchListingModel model) { IEnumerable<Listing> items = null; items = await _listingService.Query(x => !x.IsDeleted).SelectAsync(); if (model.ListingAvailableBy.HasValue) { items = items.Where(x => x.ListingAvailableBy(model.ListingAvailableBy.Value)); } return items; }
and i have added ListingAvailableBy(ListingAvailableBy filter) method in my Listing
class
public bool ListingAvailableBy(ListingAvailableBy filter) { if (filter == Enum.ListingAvailableBy.Hour) { return ShowPricePerHour && PricePerHour.HasValue; } else if (filter == Enum.ListingAvailableBy.Day) { return ShowPricePerDay && Price.HasValue; } else if (filter == Enum.ListingAvailableBy.Week) { return ShowPricePerWeek && PricePerWeek.HasValue; } else if (filter == Enum.ListingAvailableBy.Month) { return ShowPricePerMonth && PricePerMonth.HasValue; } return true; }
please suggest me what i am implemented is current or any better improvement for code any changes.