public async Task <IActionResult> Get([FromQuery] BuildingQueryFilter filter,
                                              [FromQuery] BuildingQuerySort sort,
                                              [FromQuery] BuildingQueryProjection projection,
                                              [FromQuery] BuildingQueryPaging paging,
                                              [FromQuery] BuildingQueryOptions options)
        {
            var validationResult = _service.ValidateGetBuildings(
                User, filter, sort, projection, paging, options);

            if (!validationResult.Valid)
            {
                return(BadRequest(validationResult.Result));
            }
            var result = await _service.QueryBuildingDynamic(
                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)));
        }
Esempio n. 2
0
 public ValidationResult ValidateGetBuildings(
     ClaimsPrincipal principal,
     BuildingQueryFilter filter,
     BuildingQuerySort sort,
     BuildingQueryProjection projection,
     BuildingQueryPaging paging,
     BuildingQueryOptions options)
 {
     return(ValidationResult.Pass());
 }
Esempio n. 3
0
        public IDictionary <string, object> GetBuildingDynamic(
            BuildingQueryRow row, BuildingQueryProjection projection,
            BuildingQueryOptions options)
        {
            var obj = new Dictionary <string, object>();

            foreach (var f in projection.GetFieldsArr())
            {
                switch (f)
                {
                case BuildingQueryProjection.INFO:
                {
                    var entity = row.Building;
                    obj["id"]          = entity.Id;
                    obj["code"]        = entity.Code;
                    obj["name"]        = entity.Name;
                    obj["description"] = entity.Description;
                    obj["location_id"] = entity.LocationId;
                }
                break;

                case BuildingQueryProjection.LOCATION:
                {
                    var entity = row.Location;
                    obj["location"] = new
                    {
                        id   = entity.Id,
                        name = entity.Name,
                        code = entity.Code
                    };
                }
                break;

                case BuildingQueryProjection.SELECT:
                {
                    var entity = row.Building;
                    obj["id"]   = entity.Id;
                    obj["name"] = entity.Name;
                    obj["code"] = entity.Code;
                }
                break;
                }
            }
            return(obj);
        }
Esempio n. 4
0
        public QueryResult <IDictionary <string, object> > GetBuildingDynamic(
            IEnumerable <BuildingQueryRow> rows, BuildingQueryProjection projection,
            BuildingQueryOptions options, int?totalCount = null)
        {
            var list = new List <IDictionary <string, object> >();

            foreach (var o in rows)
            {
                var obj = GetBuildingDynamic(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);
        }
Esempio n. 5
0
        public async Task <QueryResult <IDictionary <string, object> > > QueryBuildingDynamic(
            BuildingQueryProjection projection,
            BuildingQueryOptions options,
            BuildingQueryFilter filter = null,
            BuildingQuerySort sort     = null,
            BuildingQueryPaging paging = null)
        {
            var conn     = context.Database.GetDbConnection();
            var openConn = conn.OpenAsync();
            var query    = BuildingQuery.CreateDynamicSql();

            #region General
            if (filter != null)
            {
                query = query.SqlFilter(filter);
            }
            query = query.SqlJoin(projection);
            DynamicSql countQuery = null; int?totalCount = null; Task <int> countTask = null;
            if (options.count_total)
            {
                countQuery = query.SqlCount("*");
            }
            query = query.SqlProjectFields(projection);
            #endregion
            await openConn;
            if (!options.single_only)
            {
                #region List query
                if (sort != null)
                {
                    query = query.SqlSort(sort);
                }
                if (paging != null && (!options.load_all || !BuildingQueryOptions.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.single_only)
            {
                var single = queryResult.FirstOrDefault();
                if (single == null)
                {
                    return(null);
                }
                var singleResult = GetBuildingDynamic(single, projection, options);
                return(new QueryResult <IDictionary <string, object> >()
                {
                    SingleResult = singleResult
                });
            }
            if (options.count_total)
            {
                totalCount = await countTask;
            }
            var result = GetBuildingDynamic(queryResult, projection, options, totalCount);
            return(result);
        }