public async Task <IActionResult> Get([FromQuery] DeviceQueryFilter filter,
                                              [FromQuery] DeviceQuerySort sort,
                                              [FromQuery] DeviceQueryProjection projection,
                                              [FromQuery] DeviceQueryPaging paging,
                                              [FromQuery] DeviceQueryOptions options)
        {
            var validationResult = _service.ValidateGetDevices(
                User, filter, sort, projection, paging, options);

            if (!validationResult.Valid)
            {
                return(BadRequest(validationResult.Result));
            }
            var result = await _service.QueryDeviceDynamic(
                projection, options, filter, sort, paging);

            if (options.single_only)
            {
                if (result == null)
                {
                    return(NotFound(new AppResultBuilder().NotFound()));
                }
                return(Ok(new AppResultBuilder().Success(result.SingleResult)));
            }
            return(Ok(new AppResultBuilder().Success(result)));
        }
Ejemplo n.º 2
0
 public ValidationResult ValidateGetDevices(
     ClaimsPrincipal principal,
     DeviceQueryFilter filter,
     DeviceQuerySort sort,
     DeviceQueryProjection projection,
     DeviceQueryPaging paging,
     DeviceQueryOptions options)
 {
     return(ValidationResult.Pass());
 }
Ejemplo n.º 3
0
        public static DynamicSql SqlJoin(
            this DynamicSql query, DeviceQueryProjection model)
        {
            query = DynamicSql.DeepClone(query);
            var joins = model.GetFieldsArr()
                        .Where(f => DeviceQueryProjection.Joins.ContainsKey(f))
                        .Select(f => DeviceQueryProjection.Joins[f]);

            if (joins.Any())
            {
                var joinClause = string.Join('\n', joins);
                query.DynamicForm = query.DynamicForm
                                    .Replace(DynamicSql.JOIN, joinClause);
            }
            return(query);
        }
Ejemplo n.º 4
0
        public QueryResult <IDictionary <string, object> > GetDeviceDynamic(
            IEnumerable <DeviceQueryRow> rows, DeviceQueryProjection projection,
            DeviceQueryOptions options, int?totalCount = null)
        {
            var list = new List <IDictionary <string, object> >();

            foreach (var o in rows)
            {
                var obj = GetDeviceDynamic(o, projection, options);
                list.Add(obj);
            }
            var resp = new QueryResult <IDictionary <string, object> >();

            resp.Results = list;
            if (options.count_total)
            {
                resp.TotalCount = totalCount;
            }
            return(resp);
        }
Ejemplo n.º 5
0
        public static DynamicSql SqlProjectFields(
            this DynamicSql query, DeviceQueryProjection model)
        {
            query = DynamicSql.DeepClone(query);
            var finalFields = model.GetFieldsArr()
                              .Where(f => DeviceQueryProjection.Projections.ContainsKey(f))
                              .Select(f => DeviceQueryProjection.Projections[f]);

            if (finalFields.Any())
            {
                var projectionClause = string.Join(',', finalFields);
                query.DynamicForm = query.DynamicForm
                                    .Replace(DynamicSql.PROJECTION, projectionClause);
            }
            var finalResults = model.GetFieldsArr()
                               .Where(f => DeviceQueryProjection.Results.ContainsKey(f))
                               .Select(f => DeviceQueryProjection.Results[f]);

            query.MultiResults.AddRange(finalResults);
            return(query);
        }
Ejemplo n.º 6
0
        public IDictionary <string, object> GetDeviceDynamic(
            DeviceQueryRow row, DeviceQueryProjection projection,
            DeviceQueryOptions options)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case DeviceQueryProjection.INFO:
                {
                    var entity = row.Device;
                    obj["id"]          = entity.Id;
                    obj["code"]        = entity.Code;
                    obj["name"]        = entity.Name;
                    obj["description"] = entity.Description;
                    obj["area_id"]     = entity.AreaId;
                    obj["buliding_id"] = entity.BuildingId;
                    obj["floor_id"]    = entity.FloorId;
                    obj["location_id"] = entity.LocationId;
                    obj["lat"]         = entity.Lat;
                    obj["lon"]         = entity.Lon;
                    obj["schedule_id"] = entity.ScheduleId;
                }
                break;

                case DeviceQueryProjection.AREA:
                {
                    var entity = row.Area;
                    if (entity != null)
                    {
                        obj["area"] = new
                        {
                            id   = entity.Id,
                            name = entity.Name,
                            code = entity.Code,
                        }
                    }
                    ;
                }
                break;

                case DeviceQueryProjection.FLOOR:
                {
                    var entity = row.Floor;
                    if (entity != null)
                    {
                        obj["floor"] = new
                        {
                            id             = entity.Id,
                            name           = entity.Name,
                            code           = entity.Code,
                            floor_plan_svg = entity.FloorPlanSvg
                        }
                    }
                    ;
                }
                break;

                case DeviceQueryProjection.BUILDING:
                {
                    var entity = row.Building;
                    if (entity != null)
                    {
                        obj["building"] = new
                        {
                            id   = entity.Id,
                            name = entity.Name,
                            code = entity.Code
                        }
                    }
                    ;
                }
                break;

                case DeviceQueryProjection.LOCATION:
                {
                    var entity = row.Location;
                    if (entity != null)
                    {
                        obj["location"] = new
                        {
                            id   = entity.Id,
                            name = entity.Name,
                            code = entity.Code,
                        }
                    }
                    ;
                }
                break;

                case DeviceQueryProjection.ACCOUNT:
                {
                    var entity = row.DeviceAccount;
                    obj["account"] = new
                    {
                        id       = entity.Id,
                        username = entity.UserName
                    };
                }
                break;

                case DeviceQueryProjection.SCHEDULE:
                {
                    var entity = row.Schedule;
                    if (entity != null)
                    {
                        obj["schedule"] = new
                        {
                            id   = entity.Id,
                            name = entity.Name,
                            code = entity.Code,
                        }
                    }
                    ;
                }
                break;
                }
            }
            return(obj);
        }
Ejemplo n.º 7
0
        public async Task <QueryResult <DeviceQueryRow> > QueryDevice(
            DeviceQueryFilter filter         = null,
            DeviceQuerySort sort             = null,
            DeviceQueryProjection projection = null,
            DeviceQueryPaging paging         = null,
            DeviceQueryOptions options       = null)
        {
            var conn     = context.Database.GetDbConnection();
            var openConn = conn.OpenAsync();
            var query    = DeviceQuery.CreateDynamicSql();

            #region General
            if (filter != null)
            {
                query = query.SqlFilter(filter);
            }
            if (projection != null)
            {
                query = query.SqlJoin(projection);
            }
            DynamicSql countQuery = null; int?totalCount = null; Task <int> countTask = null;
            if (options != null && options.count_total)
            {
                countQuery = query.SqlCount("*");
            }
            if (projection != null)
            {
                query = query.SqlProjectFields(projection);
            }
            #endregion
            await openConn;
            if (options != null && !options.single_only)
            {
                #region List query
                if (sort != null)
                {
                    query = query.SqlSort(sort);
                }
                if (paging != null && (!options.load_all || !DeviceQueryOptions.IsLoadAllAllowed))
                {
                    query = query.SqlSelectPage(paging.page, paging.limit);
                }
                #endregion
                #region Count query
                if (options.count_total)
                {
                    countTask = conn.ExecuteScalarAsync <int>(
                        sql: countQuery.PreparedForm,
                        param: countQuery.DynamicParameters);
                }
                #endregion
            }
            var queryResult = await conn.QueryAsync(
                sql : query.PreparedForm,
                types : query.GetTypesArr(),
                map : (objs) => ProcessMultiResults(query, objs),
                splitOn : string.Join(',', query.GetSplitOns()),
                param : query.DynamicParameters);

            if (options != null && options.single_only)
            {
                var single = queryResult.FirstOrDefault();
                return(new QueryResult <DeviceQueryRow>
                {
                    SingleResult = single
                });
            }
            if (options != null && options.count_total)
            {
                totalCount = await countTask;
            }
            return(new QueryResult <DeviceQueryRow>
            {
                Results = queryResult,
                TotalCount = totalCount
            });
        }