public IDictionary <string, object> GetLocationDynamic( LocationQueryRow row, LocationQueryProjection projection, LocationQueryOptions options) { var obj = new Dictionary <string, object>(); foreach (var f in projection.GetFieldsArr()) { switch (f) { case LocationQueryProjection.INFO: { var entity = row.Location; obj["id"] = entity.Id; obj["code"] = entity.Code; obj["name"] = entity.Name; obj["address"] = entity.Address; obj["description"] = entity.Description; obj["archived"] = entity.Archived; } break; case LocationQueryProjection.SELECT: { var entity = row.Location; obj["id"] = entity.Id; obj["code"] = entity.Code; obj["name"] = entity.Name; } break; } } return(obj); }
public async Task <IActionResult> Get([FromQuery] LocationQueryFilter filter, [FromQuery] LocationQuerySort sort, [FromQuery] LocationQueryProjection projection, [FromQuery] LocationQueryPaging paging, [FromQuery] LocationQueryOptions options) { var validationResult = _service.ValidateGetLocations( User, filter, sort, projection, paging, options); if (!validationResult.Valid) { return(BadRequest(validationResult.Result)); } var result = await _service.QueryLocationDynamic( 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 ValidationResult ValidateGetLocations( ClaimsPrincipal principal, LocationQueryFilter filter, LocationQuerySort sort, LocationQueryProjection projection, LocationQueryPaging paging, LocationQueryOptions options) { return(ValidationResult.Pass()); }
public static DynamicSql SqlJoin( this DynamicSql query, LocationQueryProjection model) { query = DynamicSql.DeepClone(query); var joins = model.GetFieldsArr() .Where(f => LocationQueryProjection.Joins.ContainsKey(f)) .Select(f => LocationQueryProjection.Joins[f]); if (joins.Any()) { var joinClause = string.Join('\n', joins); query.DynamicForm = query.DynamicForm .Replace(DynamicSql.JOIN, joinClause); } return(query); }
public QueryResult <IDictionary <string, object> > GetLocationDynamic( IEnumerable <LocationQueryRow> rows, LocationQueryProjection projection, LocationQueryOptions options, int?totalCount = null) { var list = new List <IDictionary <string, object> >(); foreach (var o in rows) { var obj = GetLocationDynamic(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); }
public static DynamicSql SqlProjectFields( this DynamicSql query, LocationQueryProjection model) { query = DynamicSql.DeepClone(query); var finalFields = model.GetFieldsArr() .Where(f => LocationQueryProjection.Projections.ContainsKey(f)) .Select(f => LocationQueryProjection.Projections[f]); if (finalFields.Any()) { var projectionClause = string.Join(',', finalFields); query.DynamicForm = query.DynamicForm .Replace(DynamicSql.PROJECTION, projectionClause); } var finalResults = model.GetFieldsArr() .Where(f => LocationQueryProjection.Results.ContainsKey(f)) .Select(f => LocationQueryProjection.Results[f]); query.MultiResults.AddRange(finalResults); return(query); }
public async Task <QueryResult <IDictionary <string, object> > > QueryLocationDynamic( LocationQueryProjection projection, LocationQueryOptions options, LocationQueryFilter filter = null, LocationQuerySort sort = null, LocationQueryPaging paging = null) { var conn = context.Database.GetDbConnection(); var openConn = conn.OpenAsync(); var query = LocationQuery.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 || !LocationQueryOptions.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 = GetLocationDynamic(single, projection, options); return(new QueryResult <IDictionary <string, object> >() { SingleResult = singleResult }); } if (options.count_total) { totalCount = await countTask; } var result = GetLocationDynamic(queryResult, projection, options, totalCount); return(result); }