Skip to content

Commit

Permalink
Added unpublish query
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Nov 18, 2022
1 parent e7c77ac commit 712a0c3
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 22 deletions.
7 changes: 7 additions & 0 deletions src/DragonFly.API.Client/ClientContentService.ContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,11 @@ public async Task PublishQueryAsync(ContentItemQuery query)

response.EnsureSuccessStatusCode();
}

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

response.EnsureSuccessStatusCode();
}
}
6 changes: 6 additions & 0 deletions src/DragonFly.API/Extensions/ContentItemApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void MapContentItemRestApi(this IDragonFlyEndpointRouteBuilder end
endpoints.MapPost("api/content/{schema}/{id:guid}/publish", MapPublish);
endpoints.MapPost("api/content/{schema}/{id:guid}/unpublish", MapUnpublish);
endpoints.MapPost("api/content/publish", MapPublishQuery);
endpoints.MapPost("api/content/unpublish", MapUnpublishQuery);
}

private static async Task<QueryResult<RestContentItem>> MapQuery(HttpContext context, IContentStorage storage, ContentItemQuery query)
Expand Down Expand Up @@ -96,4 +97,9 @@ private static async Task MapPublishQuery(HttpContext context, IContentStorage c
{
await contentStore.PublishQueryAsync(query);
}

private static async Task MapUnpublishQuery(HttpContext context, IContentStorage contentStore, ContentItemQuery query)
{
await contentStore.UnpublishQueryAsync(query);
}
}
4 changes: 3 additions & 1 deletion src/DragonFly.Core/ContentItems/IContentStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IContentStorage
{
Task<QueryResult<ContentItem>> QueryAsync(ContentItemQuery query);

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

Task CreateAsync(ContentItem contentItem);

Expand All @@ -23,6 +23,8 @@ public interface IContentStorage

Task PublishQueryAsync(ContentItemQuery query);

Task UnpublishQueryAsync(ContentItemQuery query);

Task PublishAsync(string schema, Guid id);

Task UnpublishAsync(string schema, Guid id);
Expand Down
19 changes: 11 additions & 8 deletions src/DragonFly.MongoDB/Proxies/ContenItemInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ public void Intercept(IInvocation invocation)

private async Task LoadDataAsync(ContentItem main)
{
ContentItem result = await MongoStorage.Default.GetContentAsync(main.Schema.Name, main.Id);

main.Id = result.Id;
main.Schema = result.Schema;
main.Fields = result.Fields;
main.CreatedAt = result.CreatedAt;
main.ModifiedAt = result.ModifiedAt;
main.PublishedAt = result.PublishedAt;
ContentItem? result = await MongoStorage.Default.GetContentAsync(main.Schema.Name, main.Id);

if (result != null)
{
main.Id = result.Id;
main.Schema = result.Schema;
main.Fields = result.Fields;
main.CreatedAt = result.CreatedAt;
main.ModifiedAt = result.ModifiedAt;
main.PublishedAt = result.PublishedAt;
}
}
}
30 changes: 28 additions & 2 deletions src/DragonFly.MongoDB/Storages/MongoStorage.ContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace DragonFly.MongoDB;
/// </summary>
public partial class MongoStorage : IContentStorage
{
public async Task<ContentItem> GetContentAsync(string schema, Guid id)
public async Task<ContentItem?> GetContentAsync(string schema, Guid id)
{
ContentSchema contentSchema = await GetSchemaAsync(schema);
IMongoCollection<MongoContentItem> collection = GetMongoCollection(schema);
Expand All @@ -27,7 +27,8 @@ public async Task<ContentItem> GetContentAsync(string schema, Guid id)

if (result == null)
{
throw new Exception($"ContentItem '{schema}/{id}' not found");
return null;
//throw new Exception($"ContentItem '{schema}/{id}' not found");
}

return result.ToModel(contentSchema);
Expand Down Expand Up @@ -363,4 +364,29 @@ public async Task PublishQueryAsync(ContentItemQuery query)
query.Skip += pageSize;
}
}

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

query.Skip = 0;
query.Top = pageSize;

while (true)
{
var result = await QueryAsync(query);

if (result.Items.Count == 0)
{
break;
}

foreach (ContentItem contentItem in result.Items)
{
await UnpublishAsync(contentItem.Schema.Name, contentItem.Id);
}

query.Skip += pageSize;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public async Task PublishQueryAsync(ContentItemQuery query)
await Storage.PublishQueryAsync(query);
}

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

await Storage.UnpublishQueryAsync(query);
}

public async Task<QueryResult<ContentItem>> QueryAsync(ContentItemQuery query)
{
await Api.AuthorizeAsync(ContentPermissions.ContentQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public static void NavigateToCreateAsset(this NavigationManager manager, long? f

public static void NavigateToContent(this NavigationManager manager, ContentItem content)
{
manager.NavigateTo($"content/{content.Schema.Name}/create/{content.Id}");
manager.NavigateTo($"content/{content.Schema.Name}/{content.Id}");
}
}
10 changes: 9 additions & 1 deletion src/DragonFly.Razor/Pages/ContentItems/ContentItemList.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ protected override void BuildToolbarItems(IList<ToolbarItem> toolbarItems)
base.BuildToolbarItems(toolbarItems);

toolbarItems.Add(new ToolbarItem("Create", BSColor.Success, async () => Navigation.NavigateTo($"content/{SchemaName}/create")));
toolbarItems.Add(new ToolbarItem("Publish all", BSColor.Success, async () => await PublishQueryAsync()));
toolbarItems.Add(new ToolbarItem("Publish all", BSColor.Success, PublishQueryAsync));
toolbarItems.Add(new ToolbarItem("Unpublish all", BSColor.Danger, UnpublishQueryAsync));
toolbarItems.AddRefreshButton(this);
}

Expand Down Expand Up @@ -122,6 +123,13 @@ public async Task PublishQueryAsync()
await ContentService.PublishQueryAsync(query);
}

public async Task UnpublishQueryAsync()
{
ContentItemQuery query = CreateQuery();

await ContentService.UnpublishQueryAsync(query);
}

public async Task Search(string pattern)
{

Expand Down
22 changes: 13 additions & 9 deletions src/DragonFly.Razor/Pages/ContentItems/ReferenceFields.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
@foreach (var field in ContentItem.Schema.ReferenceFields)
{
<div class="col-lg-2">
@{
var column = ContentItem.Fields[field];
}

@if (column is ISingleValueField singleValueField)
@if (ContentItem.TryGetField(field, out IContentField contentField))
{
<span>@singleValueField.Value</span>
}
@if (contentField is ISingleValueField singleValueField)
{
<span>@singleValueField.Value</span>
}

else if (column is ReferenceField referenceField)
{
@if (ContentItem != null)
else if (contentField is ReferenceField referenceField)
{
@if (ContentItem != null)
{

}
}
}
else
{
}

</div>
}
</BSRow>
Expand Down

0 comments on commit 712a0c3

Please sign in to comment.