Esempio n. 2
0
        public async Task <IEnumerable <Vehicle> > GetVehicles(VehicleQuery queryObj)
        {
            if (queryObj is null)
            {
                throw new ArgumentNullException(nameof(queryObj));
            }

            var query = context.Vehicles
                        .Include(v => v.Features)
                        .ThenInclude(vf => vf.Feature)
                        .Include(v => v.Model)
                        .ThenInclude(m => m.Make)
                        .AsQueryable();

            if (queryObj.MakeId.HasValue)
            {
                query = query.Where(q => q.Model.MakeId == queryObj.MakeId.Value);
            }

            //Expression<Func<Vehicle, object>> exp; prikaz kako smo napravili expression
            var columnsMap = new Dictionary <string, Expression <Func <Vehicle, object> > >()
            {
                ["make"]        = v => v.Model.Make.Name,
                ["model"]       = v => v.Model.Name,
                ["contactName"] = v => v.ContactName,
                ["id"]          = v => v.Id
            };

            query = query.ApplyOrdering(queryObj, columnsMap);
            query = query.ApplyPaging(queryObj);

            return(await query.ToListAsync());
        }
Esempio n. 3
0
        public async Task <QueryResult <Vehicle> > GetVehicles(VehicleQuery queryObj)
        {
            var result = new QueryResult <Vehicle>();

            var query = context.Vehicles
                        .Include(v => v.Model)
                        .ThenInclude(m => m.Make)
                        .AsQueryable();

            query = query.ApplyFiltering(queryObj);

            var columnsMap = new Dictionary <string, Expression <Func <Vehicle, object> > >()
            {
                ["make"]        = v => v.Model.Make.Name,
                ["model"]       = v => v.Model.Name,
                ["contactName"] = v => v.ContactName
            };

            query = query.ApplyOrdering(queryObj, columnsMap);

            result.TotalItems = await query.CountAsync();

            query = query.ApplyPaging(queryObj);

            result.Items = await query.ToListAsync();

            return(result);
        }
Esempio n. 4
0
        public async Task <QueryResult <Vehicle> > GetVehicles(VehicleQuery queryObj)
        {
            var result = new QueryResult <Vehicle>();
            var query  = _context.Vehicles.Include(f => f.Features)
                         .ThenInclude(vf => vf.Feature).Include(v => v.Model).ThenInclude(m => m.Make.Models).AsQueryable();

            if (queryObj.MakeId.HasValue)
            {
                query = query.Where(v => v.Model.MakeId == queryObj.MakeId);
            }
            if (queryObj.ModelId.HasValue)
            {
                query = query.Where(v => v.Model.Id == queryObj.ModelId);
            }

            var columnsMap = new Dictionary <string, Expression <Func <Vehicle, Object> > >
            {
                ["make"]        = v => v.Model.Make.Name,
                ["model"]       = v => v.Model.Name,
                ["contactName"] = v => v.ContactName,
            };

            result.TotalItems = await query.CountAsync();

            query = query.ApplyOrdering(queryObj, columnsMap);
            query = query.ApplyPaging(queryObj);

            result.Items = await query.ToListAsync();

            return(result);
        }
Esempio n. 5
0
        public async Task<QueryResult<Vehicle>> GetVehicles(VehicleQuery queryObj)
        {
            var result = new QueryResult<Vehicle>();

            var query = vegaDbContext.Vehicle
                .Include(v => v.Model)
                    .ThenInclude(m => m.Make)
                .Include(v => v.Features)
                    .ThenInclude(vf => vf.Feature)
                .AsQueryable();
 
            if (queryObj.MakeId.HasValue)
                query = query.Where(v => v.Model.MakeId == queryObj.MakeId);

            if (queryObj.ModelId.HasValue)
                query = query.Where(v => v.ModelId == queryObj.ModelId);
                
            var columnsMap = new Dictionary<string, Expression<Func<Vehicle, object>>>()
            {
                ["make"] = v => v.Model.Make.Name,
                ["model"] = v => v.Model.Name,
                // ["contactName"] = v => v.Contact.Name,
                //["id"] = v => v.Id   NOTE EF auto adds Id if not existing here!
            };

            query = query.ApplyOrdering(queryObj, columnsMap);
            
            result.TotalItems = await query.CountAsync();

            query = query.ApplyPaging(queryObj);

            result.Items = await query.ToListAsync();

            return result;     
        }
        public async Task <QueryResult <Vehicle> > GetVehicles(VehicleQuery queryObj)
        {
            var result = new QueryResult <Vehicle>();
            var query  = context.Vehicles
                         .Include(v => v.Model)
                         .ThenInclude(m => m.Make)
                         .Include(f => f.Features)
                         .ThenInclude(vf => vf.Feature)
                         .AsQueryable();

            if (queryObj.MakeId.HasValue)
            {
                query = query.Where(v => v.Model.MakeId == queryObj.MakeId);
            }

            var columnMap = new Dictionary <string, Expression <Func <Vehicle, object> > >()
            {
                ["make"]        = v => v.Model.Make.Name,
                ["model"]       = v => v.Model.Name,
                ["contactName"] = v => v.ContactName,
                // ["id"] = v => v.Id, //ลบออกเพราะ sort แล้วจะ error เพราะ entity framwork เป็นตัว gen id จะไม่ยอมให้ client sort ได้
            };

            query             = query.ApplyOrdering(queryObj, columnMap);
            result.TotalItems = await query.CountAsync();

            query        = query.ApplyPaging(queryObj);
            result.Items = await query.ToListAsync();

            return(result);
        }
Esempio n. 7
0
        private IQueryable <Vehicle> ApplyFiltering(VehicleQuery queryObj, IQueryable <Vehicle> query)
        {
            if (queryObj.MakeId.HasValue)
            {
                query = query.Where(v => v.Model.MakeId == queryObj.MakeId.Value);
            }

            if (queryObj.ModelId.HasValue)
            {
                query = query.Where(v => v.Model.Id == queryObj.ModelId.Value);
            }
            return(query);
        }
Esempio n. 8
0
        public async Task <QueryResult <Vehicle> > GetVehicles(VehicleQuery queryObj)
        {
            var result = new QueryResult <Vehicle>();

            var query = context.Vehicles
                        .Include(v => v.Model)
                        .ThenInclude(m => m.Make)
                        .Include(v => v.Features)
                        .ThenInclude(vf => vf.Feature)
                        .AsQueryable();

            // filtering
            if (queryObj.MakeId.HasValue)
            {
                query = query.Where(v => v.Model.MakeId == queryObj.MakeId);
            }

            if (queryObj.ModelId.HasValue)
            {
                query = query.Where(v => v.ModelId == queryObj.ModelId);
            }

            // sorting - poor way
            var columnMap = new Dictionary <string, Expression <Func <Vehicle, object> > >()
            {
                // the way provided in C#6
                ["make"]          = v => v.Model.Make.Name
                , ["model"]       = v => v.Model.Name
                , ["contactName"] = v => v.ContactName
                                    // , ["id"] = v => v.Id,  // EF add sorty by id by itself, so we can't use it ()
            };

            // the old way
            // columnMap.Add("make", v => v.Model.Make.Name);
            // columnMap.Add("model", v => v.Model.Name);
            // columnMap.Add("contactName", v => v.ContactName);
            // columnMap.Add("id", v => v.Id);

            query = query.ApplyOrdering(queryObj, columnMap);

            // total
            result.TotalItems = await query.CountAsync();

            // pagging
            query        = query.ApplyPaging(queryObj);
            result.Items = await query.ToListAsync();

            return(result);
        }
Esempio n. 9
0
        public async Task <QueryResult <Vehicle> > GetAllAsync(VehicleQuery query = null)
        {
            var result = new QueryResult <Vehicle>();

            var queryable = this.context.Vehicles
                            .Include(v => v.Model)
                            .ThenInclude(m => m.Make)
                            .Include(v => v.Features)
                            .ThenInclude(vf => vf.Feature)
                            .AsQueryable();

            // apply query attributes
            if (query != null)
            {
                if (query.MakeId.HasValue)
                {
                    queryable = queryable.Where(v => v.Model.MakeId == query.MakeId.Value);
                }

                if (query.ModelId.HasValue)
                {
                    queryable = queryable.Where(v => v.Model.Id == query.ModelId.Value);
                }

                // get total item count *before* applying paging options
                result.TotalItems = await queryable.CountAsync();

                queryable = queryable
                            .ApplyOrdering(query, this.vehicleFieldExpressionMap)
                            .ApplyPaging(query);
            }
            else
            {
                result.TotalItems = await queryable.CountAsync();
            }

            result.Items = await queryable.ToArrayAsync();

            return(result);
        }
Esempio n. 10
0
        public async Task <QueryResult <Vehicle> > GetVehiclesAsync(VehicleQuery vehicleQuery)
        {
            var result = new QueryResult <Vehicle>();

            var query = context.Vehicle
                        .Include(v => v.Features)
                        .ThenInclude(f => f.Feature)
                        .Include(v => v.Model)
                        .ThenInclude(m => m.Make)
                        .AsQueryable();

            if (vehicleQuery.MakeId.HasValue)
            {
                query = query.Where(v => v.Model.Make.Id == vehicleQuery.MakeId.Value);
            }

            if (vehicleQuery.ModelId.HasValue)
            {
                query = query.Where(v => v.Model.Id == vehicleQuery.ModelId.Value);
            }

            var ColumnsMap = new Dictionary <string, Expression <Func <Vehicle, object> > >()
            {
                ["Make"]        = v => v.Model.Make.Name,
                ["Model"]       = v => v.Model.Name,
                ["ContactName"] = v => v.ContactName,
                ["Id"]          = v => v.Id
            };

            query = query.ApplyOrdering(vehicleQuery, ColumnsMap);

            result.TotalItems = await query.CountAsync();

            query = query.ApplyPaging(vehicleQuery);

            result.Items = await query.ToListAsync();

            return(result);
        }