public static DynamicSql SqlJoin( this DynamicSql query, PostQueryProjection model, PostQueryFilter filter) { query = DynamicSql.DeepClone(query); var joins = model.GetFieldsArr() .Where(f => PostQueryProjection.Joins.ContainsKey(f)) .Select(f => PostQueryProjection.Joins[f]); if (joins.Any()) { var joinClause = string.Join('\n', joins); query.DynamicForm = query.DynamicForm .Replace(DynamicSql.JOIN, joinClause); if (filter != null) { var contentFilters = new List <string>(); if (filter.lang != null) { var paramName = query.AddAutoIncrParam(filter.lang); var postContentLang = $"{nameof(PostContent)}.{nameof(PostContent.Lang)}"; contentFilters.Add($"{postContentLang}=@{paramName}"); } if (contentFilters.Any()) { var whereClause = "WHERE " + string.Join(" AND ", contentFilters); query.DynamicForm = query.DynamicForm .Replace(PostQueryPlaceholder.POST_CONTENT_FILTER, whereClause); } } } return(query); }
public async Task <IActionResult> Get([FromQuery] PostQueryFilter filter, [FromQuery] PostQuerySort sort, [FromQuery] PostQueryProjection projection, [FromQuery] PostQueryPaging paging, [FromQuery] PostQueryOptions options) { var validationResult = _service.ValidateGetPosts( User, filter, sort, projection, paging, options); if (!validationResult.Valid) { return(BadRequest(validationResult.Result)); } var result = await _service.QueryPostDynamic( 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 ValidateGetPosts( ClaimsPrincipal principal, PostQueryFilter filter, PostQuerySort sort, PostQueryProjection projection, PostQueryPaging paging, PostQueryOptions options) { return(ValidationResult.Pass()); }
public QueryResult <IDictionary <string, object> > GetPostDynamic( IEnumerable <PostQueryRow> rows, PostQueryProjection projection, PostQueryOptions options, int?totalCount = null) { var list = new List <IDictionary <string, object> >(); foreach (var o in rows) { var obj = GetPostDynamic(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, PostQueryProjection model) { query = DynamicSql.DeepClone(query); var finalFields = model.GetFieldsArr() .Where(f => PostQueryProjection.Projections.ContainsKey(f)) .Select(f => PostQueryProjection.Projections[f]); if (finalFields.Any()) { var projectionClause = string.Join(',', finalFields); query.DynamicForm = query.DynamicForm .Replace(DynamicSql.PROJECTION, projectionClause); } var finalResults = model.GetFieldsArr() .Where(f => PostQueryProjection.Results.ContainsKey(f)) .Select(f => PostQueryProjection.Results[f]); query.MultiResults.AddRange(finalResults); return(query); }
public IDictionary <string, object> GetPostDynamic( PostQueryRow row, PostQueryProjection projection, PostQueryOptions options) { var obj = new Dictionary <string, object>(); foreach (var f in projection.GetFieldsArr()) { switch (f) { case PostQueryProjection.INFO: { var entity = row.Post; var content = row.Content; obj["id"] = entity.Id; obj["owner_id"] = entity.OwnerId; obj["type"] = entity.Type; obj["archived"] = entity.Archived; obj["image_url"] = entity.ImageUrl; var createdTime = entity.CreatedTime .ToTimeZone(options.time_zone, options.culture, content?.Lang); var createdTimeStr = createdTime.ToString(options.date_format, options.culture, content?.Lang); obj["created_time"] = new { display = createdTimeStr, iso = $"{createdTime.ToUniversalTime():s}Z" }; var visibleTime = entity.VisibleTime? .ToTimeZone(options.time_zone, options.culture, content?.Lang); var visibleTimeStr = visibleTime?.ToString(options.date_format, options.culture, content?.Lang); if (visibleTimeStr != null) { obj["visible_time"] = new { display = visibleTimeStr, iso = $"{visibleTime?.ToUniversalTime():s}Z" } } ; } break; case PostQueryProjection.CONTENT: { var entity = row.Content; if (entity != null) { obj["content_id"] = entity.Id; obj["lang"] = entity.Lang; obj["description"] = entity.Description; obj["title"] = entity.Title; } } break; case PostQueryProjection.CONTENT_CONTENT: { var entity = row.Content; if (entity != null) { obj["content"] = entity.Content; } } break; case PostQueryProjection.OWNER: { var entity = row.Owner; obj["owner"] = new { id = entity.Id, code = entity.Code, name = entity.Name, }; } break; } } return(obj); }
public async Task <QueryResult <PostQueryRow> > QueryPost( PostQueryFilter filter = null, PostQuerySort sort = null, PostQueryProjection projection = null, PostQueryPaging paging = null, PostQueryOptions options = null) { var conn = context.Database.GetDbConnection(); var openConn = conn.OpenAsync(); var query = PostQuery.CreateDynamicSql(); #region General if (filter != null) { query = query.SqlFilter(filter); } if (projection != null) { query = query.SqlJoin(projection, filter); } 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 || !PostQueryOptions.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 <PostQueryRow> { SingleResult = single }); } if (options != null && options.count_total) { totalCount = await countTask; } return(new QueryResult <PostQueryRow> { Results = queryResult, TotalCount = totalCount }); }