Exemple #1
0
        public IEnumerable <IPersonEntity> ListPeople(IPeopleFilter filter)
        {
            var people = new List <IPersonEntity>();

            // For most databases this would be call to a Stored Procedure (or a View) with the SPROC handling the filtering
            // and the age group.
            // But here - particularly since we're allowing unfiltered lists - just do a simple
            // SELECT * and do the filtering with LINQ.
            var sql = " SELECT * FROM Person ";
            var dt  = ExecuteQuery(sql);

            // Q: How would this happen without an exception?
            // A: Someone changed the ExecuteQuery method
            if (dt == null)
            {
                throw new Exception($"An empty dataset was returned from the query: {sql}");
            }

            foreach (DataRow row in dt.Rows)
            {
                var person = new PersonEntity();

                person.ID        = (int)((Int64)row["ID"]);
                person.FirstName = row["FirstName"].ToString();
                person.LastName  = row["LastName"].ToString();
                person.Age       = (int)((Int64)row["Age"]);

                person.AgeRange = _agegroup.Get(person.Age);

                people.Add(person);
            }

            if (filter != null)
            {
                people = people.Where(p => (filter.FirstName == null || p.FirstName.StartsWith(filter.FirstName, StringComparison.CurrentCultureIgnoreCase)) &&
                                      (filter.LastName == null || p.LastName.StartsWith(filter.LastName, StringComparison.CurrentCultureIgnoreCase)) &&
                                      (filter.MinAge == null || filter.MinAge <= p.Age) &&
                                      (filter.MaxAge == null || filter.MaxAge > p.Age)).ToList();
            }


            return(people);
        }
Exemple #2
0
        public async Task <IEnumerable <IPersonEntity> > List(IPeopleFilter filter = null)
        {
            IEnumerable <IPersonEntity> people = new List <IPersonEntity>();

            var uriAppendix = $"api/Person{GetFilterQuery(filter)}";

            HttpResponseMessage response = await _httpClient.GetAsync(uriAppendix);

            if (response.IsSuccessStatusCode)
            {
                var data = await response.Content.ReadAsStringAsync();

                // var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All };
                people = JsonConvert.DeserializeObject <List <PersonEntity> >(data);
            }
            else
            {
                _logger.Write($"Failed to GET People from the Web Api Service at {_personApiBaseAddress}", enLogType.Error);
            }

            return(people);
        }
Exemple #3
0
        private string GetFilterQuery(IPeopleFilter filter)
        {
            if (filter == null)
            {
                return(string.Empty);
            }

            var query = string.Empty;

            if (!string.IsNullOrEmpty(filter.FirstName))
            {
                query += $"FirstName={filter.FirstName}&";
            }

            if (!string.IsNullOrEmpty(filter.LastName))
            {
                query += $"LastName={filter.LastName}&";
            }

            if (filter.MinAge.HasValue)
            {
                query += $"MinAge={filter.MinAge}&";
            }

            if (filter.MaxAge.HasValue)
            {
                query += $"MaxAge={filter.MaxAge}&";
            }

            query = query.TrimEnd('&');

            if (!string.IsNullOrEmpty(query))
            {
                query = $"?{query}";
            }

            return(query);
        }
 public IEnumerable <IPersonEntity> ListPeople(IPeopleFilter filter)
 {
     throw new NotImplementedException();
 }
 public IEnumerable <IPersonEntity> ListPeople(IPeopleFilter filter)
 {
     return(new List <IPersonEntity>());
 }