Skip to content

Commit

Permalink
Added typed content query api
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Dec 15, 2022
1 parent 1f2c704 commit aa75354
Show file tree
Hide file tree
Showing 28 changed files with 314 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/DragonFly.API.Client/ClientContentService.ContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task DeleteAsync(string schema, Guid id)
response.EnsureSuccessStatusCode();
}

public async Task<QueryResult<ContentItem>> QueryAsync(ContentItemQuery queryParameters)
public async Task<QueryResult<ContentItem>> QueryAsync(ContentQuery queryParameters)
{
HttpResponseMessage response = await Client.PostAsJsonAsync($"api/content/query", queryParameters, JsonSerializerDefault.Options);

Expand All @@ -83,14 +83,14 @@ public async Task<QueryResult<ContentItem>> QueryAsync(ContentItemQuery queryPar
};
}

public async Task PublishQueryAsync(ContentItemQuery query)
public async Task PublishQueryAsync(ContentQuery query)
{
var response = await Client.PostAsJsonAsync($"api/content/publish", query, JsonSerializerDefault.Options);

response.EnsureSuccessStatusCode();
}

public async Task UnpublishQueryAsync(ContentItemQuery query)
public async Task UnpublishQueryAsync(ContentQuery query)
{
var response = await Client.PostAsJsonAsync($"api/content/unpublish", query, JsonSerializerDefault.Options);

Expand Down
6 changes: 3 additions & 3 deletions src/DragonFly.API/Extensions/ContentItemApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void MapContentItemRestApi(this IEndpointRouteBuilder endpoints)
groupRoute.MapPost("unpublish", MapUnpublishQuery);
}

private static async Task<QueryResult<RestContentItem>> MapQuery(HttpContext context, IContentStorage storage, ContentItemQuery query)
private static async Task<QueryResult<RestContentItem>> MapQuery(HttpContext context, IContentStorage storage, ContentQuery query)
{
QueryResult<ContentItem> contentItems = await storage.QueryAsync(query);

Expand Down Expand Up @@ -95,12 +95,12 @@ private static async Task MapUnpublish(HttpContext context, IContentStorage cont
await contentStore.UnpublishAsync(schema, id);
}

private static async Task MapPublishQuery(HttpContext context, IContentStorage contentStore, ContentItemQuery query)
private static async Task MapPublishQuery(HttpContext context, IContentStorage contentStore, ContentQuery query)
{
await contentStore.PublishQueryAsync(query);
}

private static async Task MapUnpublishQuery(HttpContext context, IContentStorage contentStore, ContentItemQuery query)
private static async Task MapUnpublishQuery(HttpContext context, IContentStorage contentStore, ContentQuery query)
{
await contentStore.UnpublishQueryAsync(query);
}
Expand Down
4 changes: 2 additions & 2 deletions src/DragonFly.AspNetCore/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace DragonFly.AspNetCore;
public static class Extensions
{
public static ContentItemQuery GetQuery(this HttpRequest request)
public static ContentQuery GetQuery(this HttpRequest request)
{
ContentItemQuery result = new ContentItemQuery();
ContentQuery result = new ContentQuery();

StringValues skip = request.Query["$skip"];
StringValues top = request.Query["$top"];
Expand Down
6 changes: 3 additions & 3 deletions src/DragonFly.Core/ContentItems/IContentStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace DragonFly.Storage;
/// </summary>
public interface IContentStorage
{
Task<QueryResult<ContentItem>> QueryAsync(ContentItemQuery query);
Task<QueryResult<ContentItem>> QueryAsync(ContentQuery query);

Task<ContentItem?> GetContentAsync(string schema, Guid id);

Expand All @@ -21,9 +21,9 @@ public interface IContentStorage

Task DeleteAsync(string schema, Guid id);

Task PublishQueryAsync(ContentItemQuery query);
Task PublishQueryAsync(ContentQuery query);

Task UnpublishQueryAsync(ContentItemQuery query);
Task UnpublishQueryAsync(ContentQuery query);

Task PublishAsync(string schema, Guid id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace DragonFly.Query;

/// <summary>
/// ContentItemQuery
/// ContentQuery
/// </summary>
public class ContentItemQuery
public class ContentQuery : IContentQuery
{
public ContentItemQuery()
public ContentQuery()
{
Fields = new List<FieldQuery>();
OrderFields = new List<FieldOrder>();
Expand Down
12 changes: 8 additions & 4 deletions src/DragonFly.Core/ContentItems/Queries/FieldQueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@ namespace DragonFly.Query;

public static class FieldQueryExtensions
{
public static ContentItemQuery AddFieldOrder(this ContentItemQuery queryParameters, string field, bool asc = true)
public static TContentQuery AddFieldOrder<TContentQuery>(this TContentQuery queryParameters, string field, bool asc = true)
where TContentQuery : IContentQuery
{
queryParameters.OrderFields.Add(new FieldOrder($"Fields.{field}", asc));

return queryParameters;
}

public static ContentItemQuery Top(this ContentItemQuery query, int value)
public static TContentQuery Top<TContentQuery>(this TContentQuery query, int value)
where TContentQuery : IContentQuery
{
query.Top = value;

return query;
}

public static ContentItemQuery Skip(this ContentItemQuery query, int value)
public static TContentQuery Skip<TContentQuery>(this TContentQuery query, int value)
where TContentQuery : IContentQuery
{
query.Skip = value;

return query;
}

public static ContentItemQuery Published(this ContentItemQuery query, bool value)
public static TContentQuery Published<TContentQuery>(this TContentQuery query, bool value)
where TContentQuery : IContentQuery
{
query.Published = value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace DragonFly.Query;
/// </summary>
public static class AssetFieldQueryExtensions
{
public static ContentItemQuery AddAssetQuery(this ContentItemQuery queryParameters, string name, Guid? id)
public static TContentQuery AddAssetQuery<TContentQuery>(this TContentQuery query, string name, Guid? id)
where TContentQuery : IContentQuery
{
queryParameters.Fields.Add(new AssetFieldQuery() { FieldName = name, AssetId = id });
query.Fields.Add(new AssetFieldQuery() { FieldName = name, AssetId = id });

return queryParameters;
return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace DragonFly.Query;
/// </summary>
public static class BoolFieldQueryExtensions
{
public static ContentItemQuery AddBoolQuery(this ContentItemQuery queryParameters, string name, bool? value)
public static TContentQuery AddBoolQuery<TContentQuery>(this TContentQuery query, string name, bool? value)
where TContentQuery : IContentQuery
{
queryParameters.Fields.Add(new BoolFieldQuery() { FieldName = name, Value = value});
query.Fields.Add(new BoolFieldQuery() { FieldName = name, Value = value});

return queryParameters;
return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace DragonFly.Query;
/// </summary>
public static class IntegerFieldQueryExtensions
{
public static ContentItemQuery AddIntegerQuery(this ContentItemQuery queryParameters, string name, int? value, int? minValue = null, int? maxValue = null)
public static TContentQuery AddIntegerQuery<TContentQuery>(this TContentQuery query, string name, int? value, int? minValue = null, int? maxValue = null)
where TContentQuery : IContentQuery
{
queryParameters.Fields.Add(new IntegerFieldQuery() { FieldName = name, Value = value, MinValue = minValue, MaxValue = maxValue });
query.Fields.Add(new IntegerFieldQuery() { FieldName = name, Value = value, MinValue = minValue, MaxValue = maxValue });

return queryParameters;
return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace DragonFly.Query;
/// </summary>
public static class ReferenceFieldQueryExtensions
{
public static ContentItemQuery AddReferenceQuery(this ContentItemQuery queryParameters, string name, Guid? id)
public static TContentQuery AddReferenceQuery<TContentQuery>(this TContentQuery query, string name, Guid? id)
where TContentQuery : IContentQuery
{
queryParameters.Fields.Add(new ReferenceFieldQuery() { FieldName = name, ContentItemId = id });
query.Fields.Add(new ReferenceFieldQuery() { FieldName = name, ContentItemId = id });

return queryParameters;
return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace DragonFly.Query;
/// </summary>
public static class SlugFieldQueryExtensions
{
public static ContentItemQuery AddSlugQuery(this ContentItemQuery queryParameters, string name, string value)
public static TContentQuery AddSlugQuery<TContentQuery>(this TContentQuery query, string name, string value)
where TContentQuery : IContentQuery
{
queryParameters.Fields.Add(new SlugFieldQuery() { FieldName = name, Value = value });
query.Fields.Add(new SlugFieldQuery() { FieldName = name, Value = value });

return queryParameters;
return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace DragonFly.Query;
/// </summary>
public static class StringFieldQueryExtensions
{
public static ContentItemQuery AddStringQuery(this ContentItemQuery queryParameters, string name, string pattern, StringFieldQueryType type = StringFieldQueryType.Equals)
public static TContentQuery AddStringQuery<TContentQuery>(this TContentQuery query, string name, string pattern, StringFieldQueryType type = StringFieldQueryType.Equals)
where TContentQuery : IContentQuery
{
queryParameters.Fields.Add(new StringFieldQuery() { FieldName = name, Pattern = pattern, PatternType = type });
query.Fields.Add(new StringFieldQuery() { FieldName = name, Pattern = pattern, PatternType = type });

return queryParameters;
return query;
}
}
51 changes: 51 additions & 0 deletions src/DragonFly.Core/ContentItems/Queries/IContentQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) usercode
// https://github.com/usercode/DragonFly
// MIT License

namespace DragonFly.Query;

/// <summary>
/// IContentQuery
/// </summary>
public interface IContentQuery
{
/// <summary>
/// Skip
/// </summary>
int Skip { get; set; }

/// <summary>
/// Top
/// </summary>
int Top { get; set; }

/// <summary>
/// SearchPattern
/// </summary>
string SearchPattern { get; set; }

/// <summary>
/// Fields
/// </summary>
IList<FieldQuery> Fields { get; set; }

/// <summary>
/// OrderFields
/// </summary>
IList<FieldOrder> OrderFields { get; set; }

/// <summary>
///
/// </summary>
bool IncludeListFieldsOnly { get; set; }

/// <summary>
/// UsedAsset
/// </summary>
Guid? UsedAsset { get; set; }

/// <summary>
/// Published
/// </summary>
bool Published { get; set; }
}
6 changes: 3 additions & 3 deletions src/DragonFly.MongoDB/Storages/MongoStorage.ContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public IMongoCollection<MongoContentItem> GetMongoCollection(string type, bool p
return items;
}

public async Task<QueryResult<ContentItem>> QueryAsync(ContentItemQuery query)
public async Task<QueryResult<ContentItem>> QueryAsync(ContentQuery query)
{
ContentSchema schema = await GetSchemaAsync(query.Schema);
IMongoCollection<MongoContentItem> collection = GetMongoCollection(schema.Name, query.Published);
Expand Down Expand Up @@ -340,7 +340,7 @@ await drafts.UpdateOneAsync(
}
}

public async Task PublishQueryAsync(ContentItemQuery query)
public async Task PublishQueryAsync(ContentQuery query)
{
int pageSize = 50;

Expand All @@ -365,7 +365,7 @@ public async Task PublishQueryAsync(ContentItemQuery query)
}
}

public async Task UnpublishQueryAsync(ContentItemQuery query)
public async Task UnpublishQueryAsync(ContentQuery query)
{
int pageSize = 50;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,23 @@ public async Task PublishAsync(string schema, Guid id)
await Storage.PublishAsync(schema, id);
}

public async Task PublishQueryAsync(ContentItemQuery query)
public async Task PublishQueryAsync(ContentQuery query)
{
await Api.AuthorizeAsync(ContentPermissions.ContentQuery);
await Api.AuthorizeAsync(ContentPermissions.ContentPublish);

await Storage.PublishQueryAsync(query);
}

public async Task UnpublishQueryAsync(ContentItemQuery query)
public async Task UnpublishQueryAsync(ContentQuery query)
{
await Api.AuthorizeAsync(ContentPermissions.ContentQuery);
await Api.AuthorizeAsync(ContentPermissions.ContentUnpublish);

await Storage.UnpublishQueryAsync(query);
}

public async Task<QueryResult<ContentItem>> QueryAsync(ContentItemQuery query)
public async Task<QueryResult<ContentItem>> QueryAsync(ContentQuery query)
{
await Api.AuthorizeAsync(ContentPermissions.ContentQuery);

Expand Down
6 changes: 3 additions & 3 deletions src/DragonFly.Proxy/Builder/ContentSchemaBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.Proxy.Query;
using DragonFly.Query;
using DragonFly.Storage;

Expand Down Expand Up @@ -47,11 +48,10 @@ public static async Task UnpublishAsync<TContentType>(this IContentStorage stora

}

public static async Task<QueryResult<TContentType>> QueryAsync<TContentType>(this IContentStorage storage, Action<ContentItemQuery>? action = null)
public static async Task<QueryResult<TContentType>> QueryAsync<TContentType>(this IContentStorage storage, Action<IContentQuery<TContentType>>? action = null)
where TContentType : class
{
ContentItemQuery query = new ContentItemQuery();
query.Schema = ProxyTypeManager.Default.Get<TContentType>().Name;
ContentQuery<TContentType> query = new ContentQuery<TContentType>();

action?.Invoke(query);

Expand Down
16 changes: 16 additions & 0 deletions src/DragonFly.Proxy/Builder/Query/ContentQuery~1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) usercode
// https://github.com/usercode/DragonFly
// MIT License

using System.Net.Mime;
using DragonFly.Query;

namespace DragonFly.Proxy.Query;

public class ContentQuery<TContentType> : ContentQuery, IContentQuery<TContentType>
{
public ContentQuery()
{
Schema = ProxyTypeManager.Default.Get<TContentType>().Name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) usercode
// https://github.com/usercode/DragonFly
// MIT License

using System.Linq.Expressions;
using DragonFly.Proxy.Helpers;
using DragonFly.Query;

namespace DragonFly.Proxy.Query;

/// <summary>
/// AssetFieldQueryExtensions
/// </summary>
public static class AssetFieldQueryExtensions
{
public static IContentQuery<TModel> AddAssetQuery<TModel>(this IContentQuery<TModel> query, Expression<Func<TModel, Asset?>> name, Guid? id)
{
query.AddAssetQuery(ReflectionHelper.GetPropertyName(name), id);

return query;
}
}
Loading

0 comments on commit aa75354

Please sign in to comment.