Exemplo n.º 1
0
        public virtual async Task <ActionResult <T> > Search([FromQuery] string lastname, [FromQuery] string genre, [FromQuery] string sort)
        {
            var query = _context.Set <T>().AsQueryable();

            //solution 2: optimisation de la requete SQL

            if (!string.IsNullOrWhiteSpace(lastname))
            {
                //var tab1 = lastname.Split(',');

                if (lastname.StartsWith("*") && lastname.EndsWith("*"))
                {
                    //&& lastname.EndsWith("*")
                    query = IQueryableExtensions.SelectColonnesName(query, "lastname", lastname);
                }
                else
                {
                    return(NotFound(new { Message = $"Le lastname {lastname} doit etre encadré par des *" }));
                }


                if (!string.IsNullOrWhiteSpace(genre))
                {
                    var tab3 = genre.Split(',');
                    //  //on verifie si on a deux éléments dans le tableau pour savoir si on va faire le expression.or()

                    if (tab3.Length == 1)
                    {
                        query = IQueryableExtensions.SelectColonnesGenderOne(query, "genre", tab3[0]);
                    }
                    else
                    {
                        query = IQueryableExtensions.SelectColonnesGender(query, "genre", tab3);
                    }
                }
                if (!string.IsNullOrWhiteSpace(sort))
                {
                    var tab2 = sort.Split(',');

                    query = IQueryableExtensions.SelectColonnesAsc(query, tab2);
                }

                return(Ok(ToJsonList(await query.ToListAsync())));
            }
            else
            {
                return(NotFound(new { Message = $"name {lastname} not found" }));
            }
        }