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))); }
public static DynamicSql SqlSort(this DynamicSql query, BuildingQuerySort model) { query = DynamicSql.DeepClone(query); var listSorts = new List <string>(); foreach (var s in model._sortsArr) { var asc = s[0] == 'a'; var fieldName = s.Remove(0, 1); switch (fieldName) { case BuildingQuerySort.NAME: { listSorts.Add($"{nameof(Building)}" + $".{nameof(Building.Name)}{(asc ? "" : " DESC")}"); } break; } } if (listSorts.Any()) { var orderByClause = "ORDER BY " + string.Join(',', listSorts); query.SortClause = orderByClause; } return(query); }
public ValidationResult ValidateGetBuildings( ClaimsPrincipal principal, BuildingQueryFilter filter, BuildingQuerySort sort, BuildingQueryProjection projection, BuildingQueryPaging paging, BuildingQueryOptions options) { return(ValidationResult.Pass()); }
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); }