// by convention routing: GET/api.lawyers?includeGender=<bool>&includeTitle=<bool>&includeInactive=<bool>  automagically!
 public IEnumerable <Lawyer> GetAll([FromUri] LawyerSearchParameters lawyerSearchParameters)
 {
     using (LawyerService lawyerService = new LawyerService(new DataContext()))
     {
         return(lawyerService.GetAll(lawyerSearchParameters));
     }
 }
Пример #2
0
 public ActionResult Index(LawyerSearchParameters searchParameters)
 {
     if (searchParameters == null)
     {
         searchParameters = new LawyerSearchParameters();
     }
     ViewData.Add("parameters", searchParameters);
     return(View(ApiConsumer.Get <List <Lawyer> >(string.Format("Lawyers{0}", searchParameters.ToString()))));
 }
Пример #3
0
        /// <summary>
        /// retrieves a list of lawyers with the specified parameters.
        /// </summary>
        /// <param name="IncludeGender">if true lawyer will include Gender entity</param>
        /// <param name="IncludeTitle">if true lawyer will include Title entity</param>
        /// <param name="IncludeInactive">if true the list will contain all records from the DB. Not just the active.</param>
        /// <param name="Name">if a name is provided the list will be filtered with the lawyers with that name</param>
        /// <param name="Surname">if a surname is provided the list will be filtered with the lawyers with that surname</param>
        /// <returns>A list with all Lawyer records that are active</returns>
        public IEnumerable <Lawyer> GetAll(LawyerSearchParameters lawyerSearchParameters)
        {
            // IQueryable will create the sql query but it will not execute it. It queries the DB lazily when needed.
            // DbSet from EF is smart enough to know how to convert the set to a queryable. Both share the same lazy principal.
            IQueryable <Lawyer> query = Set;

            // if caller needs only the active records, we include a where clause in the query.
            if (!lawyerSearchParameters.IncludeInactive)
            {
                query = Set.Where(l => l.Active == true);
            }

            // if caller needs gender included, query will include it by joining the tables with the gender FK.
            if (lawyerSearchParameters.IncludeGender)
            {
                query = query.Include(s => s.Gender);
            }

            // same goes for the title.
            if (lawyerSearchParameters.IncludeTitle)
            {
                query = query.Include(s => s.Title);
            }

            // if both name and surname are provided, query will include all records matching with either of them.
            if (!string.IsNullOrWhiteSpace(lawyerSearchParameters.Name) && !string.IsNullOrWhiteSpace(lawyerSearchParameters.Surname))
            {
                lawyerSearchParameters.Name    = lawyerSearchParameters.Name.ToLower();
                lawyerSearchParameters.Surname = lawyerSearchParameters.Surname.ToLower();
                query = query.Where(l => l.Name.ToLower().Contains(lawyerSearchParameters.Name) || l.Surname.ToLower().Contains(lawyerSearchParameters.Surname));
            }

            else if (!string.IsNullOrWhiteSpace(lawyerSearchParameters.Name))
            {
                lawyerSearchParameters.Name = lawyerSearchParameters.Name.ToLower();
                query = query.Where(l => l.Name.ToLower().Contains(lawyerSearchParameters.Name));
            }

            else if (!string.IsNullOrWhiteSpace(lawyerSearchParameters.Surname))
            {
                lawyerSearchParameters.Surname = lawyerSearchParameters.Surname.ToLower();
                query = query.Where(l => l.Surname.ToLower().Contains(lawyerSearchParameters.Surname));
            }

            // finally we execute the query with ToList(). This method executes the IQueryable immediately and converts the result to a List(IEnumerable).
            return(query.ToList());
        }