Skip to content

Commit ed4cc90

Browse files
committed
Allows to query referenced content
1 parent da1534e commit ed4cc90

File tree

12 files changed

+96
-25
lines changed

12 files changed

+96
-25
lines changed

src/DragonFly.ApiKeys/DragonFly.ApiKeys.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
3-
<PackageReference Include="MongoDB.Driver" Version="3.3.0" />
3+
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
44
</ItemGroup>
55

66
<ItemGroup>

src/DragonFly.Assets.ImageSharp/Actions/CropWhitespacesAssetAction.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public class CropWhitespacesAssetAction : IAssetAction
1313
{
1414
public string Name => "CropWhitespaces";
1515

16-
public string[] MimeTypes => throw new NotImplementedException();
17-
1816
public async Task<bool> ProcessAsync(IAssetActionContext context)
1917
{
2018
Image<Rgb24> image;

src/DragonFly.Client/Extensions/NavigationManagerExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public static void NavigateToContent(this NavigationManager manager, ContentItem
2727
{
2828
manager.NavigateTo($"content/{content.Schema.Name}/{content.Id}");
2929
}
30+
31+
public static void NavigateToContentQuery(this NavigationManager manager, string schema, ContentReference? reference)
32+
{
33+
manager.NavigateTo($"content/{schema}?ref={reference}");
34+
}
3035
}

src/DragonFly.Client/Pages/ContentItems/ContentItemDetail.razor.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
using BlazorStrap;
66
using DragonFly.Razor.Base;
77
using Microsoft.AspNetCore.Components;
8-
using System;
9-
using System.Collections.Generic;
10-
using System.Linq;
11-
using System.Threading.Tasks;
128
using Microsoft.JSInterop;
139
using BlazorStrap.V5;
1410
using DragonFly.Razor.Extensions;

src/DragonFly.Client/Pages/ContentItems/ContentItemList.razor.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
using BlazorStrap;
66
using Microsoft.AspNetCore.Components;
7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
10-
using System.Threading.Tasks;
117

128
namespace DragonFly.Client.Pages.ContentItems;
139

@@ -48,6 +44,9 @@ public partial class ContentItemList
4844
[SupplyParameterFromQuery]
4945
public string State { get; set; }
5046

47+
[SupplyParameterFromQuery]
48+
public string? Ref { get; set; }
49+
5150
protected override void BuildToolbarItems(IList<ToolbarItem> toolbarItems)
5251
{
5352
base.BuildToolbarItems(toolbarItems);
@@ -88,9 +87,14 @@ protected override async Task RefreshActionAsync()
8887
}
8988
}
9089

91-
ContentQuery quey = CreateQuery();
90+
ContentQuery query = CreateQuery();
91+
92+
if (ContentReference.TryParse(Ref, null, out ContentReference contentReference))
93+
{
94+
query.Reference = contentReference;
95+
}
9296

93-
SearchResult = await ContentService.QueryAsync(quey);
97+
SearchResult = await ContentService.QueryAsync(query);
9498
}
9599
}
96100

src/DragonFly.Client/Pages/ContentReferences/ReferencedBy.razor

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<BSListGroup>
1+
@inject NavigationManager NavigationManager
2+
3+
<BSListGroup>
24
@if (ReferenceIndex != null)
35
{
46
foreach (var content in ReferenceIndex.Entries)
57
{
6-
<BSListGroupItem>@content.Schema (@content.Count)</BSListGroupItem>
8+
<BSListGroupItem> <BSButton Color="BSColor.Primary" OnClick="x => NavigateToQuery(content.Schema)">@content.Schema (@content.Count)</BSButton></BSListGroupItem>
79
}
810
}
911
</BSListGroup>
@@ -26,4 +28,9 @@
2628

2729
ReferenceIndex = result.Value;
2830
}
31+
32+
private void NavigateToQuery(string schema)
33+
{
34+
NavigationManager.NavigateToContentQuery(schema, new ContentReference(Schema, Id));
35+
}
2936
}

src/DragonFly.Core/Modules/ContentItems/Models/References/ContentReference.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// https://github.com/usercode/DragonFly
33
// MIT License
44

5+
using System.ComponentModel;
6+
using System.Diagnostics.CodeAnalysis;
57
using System.Text.Json.Serialization;
68
using DragonFly.Core;
79

@@ -10,9 +12,43 @@ namespace DragonFly;
1012
/// <summary>
1113
/// ContentReference
1214
/// </summary>
13-
[JsonConverter(typeof(ContentReferenceConverter))]
14-
public readonly struct ContentReference : IEquatable<ContentReference>
15+
[JsonConverter(typeof(ContentReferenceJsonConverter))]
16+
[TypeConverter(typeof(ContentReferenceTypeConverter))]
17+
public readonly struct ContentReference : IEquatable<ContentReference>, IParsable<ContentReference>
1518
{
19+
public static ContentReference Parse(string s, IFormatProvider? provider)
20+
{
21+
if (TryParse(s, provider, out ContentReference result))
22+
{
23+
return result;
24+
}
25+
26+
throw new Exception();
27+
}
28+
29+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, [MaybeNullWhen(false)] out ContentReference result)
30+
{
31+
if (s == null)
32+
{
33+
result = default;
34+
35+
return false;
36+
}
37+
38+
int pos = s.IndexOf('/');
39+
40+
if (pos == -1)
41+
{
42+
result = default;
43+
44+
return false;
45+
}
46+
47+
result = new ContentReference(s[..pos], Guid.Parse(s[(pos + 1)..]));
48+
49+
return true;
50+
}
51+
1652
public ContentReference(string schema, Guid id)
1753
{
1854
Schema = schema;
@@ -52,7 +88,7 @@ public override int GetHashCode()
5288
public override string ToString()
5389
{
5490
return $"{Schema}/{Id}";
55-
}
91+
}
5692

5793
public static bool operator ==(ContentReference left, ContentReference right)
5894
{

src/DragonFly.Core/Modules/ContentItems/Models/References/ContentReferenceConverter.cs renamed to src/DragonFly.Core/Modules/ContentItems/Models/References/ContentReferenceJsonConverter.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
// https://github.com/usercode/DragonFly
33
// MIT License
44

5+
using System.Globalization;
56
using System.Text.Json;
67
using System.Text.Json.Serialization;
78

89
namespace DragonFly.Core;
910

10-
internal class ContentReferenceConverter : JsonConverter<ContentReference>
11+
internal class ContentReferenceJsonConverter : JsonConverter<ContentReference>
1112
{
1213
public override ContentReference Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1314
{
@@ -18,9 +19,7 @@ public override ContentReference Read(ref Utf8JsonReader reader, Type typeToConv
1819
throw new Exception();
1920
}
2021

21-
int pos = value.IndexOf('/');
22-
23-
return new ContentReference(value[..pos], Guid.Parse(value[(pos + 1)..]));
22+
return ContentReference.Parse(value, CultureInfo.InvariantCulture);
2423
}
2524

2625
public override void Write(Utf8JsonWriter writer, ContentReference value, JsonSerializerOptions options)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) usercode
2+
// https://github.com/usercode/DragonFly
3+
// MIT License
4+
5+
using System.ComponentModel;
6+
using System.Globalization;
7+
8+
namespace DragonFly.Core;
9+
10+
internal class ContentReferenceTypeConverter : TypeConverter
11+
{
12+
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
13+
{
14+
return sourceType == typeof(string);
15+
}
16+
17+
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
18+
{
19+
if (value is string stringValue)
20+
{
21+
return ContentReference.Parse(stringValue, culture);
22+
}
23+
24+
return base.ConvertFrom(context, culture, value);
25+
}
26+
}

src/DragonFly.Identity/DragonFly.Identity.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
3-
<PackageReference Include="MongoDB.Driver" Version="3.3.0" />
3+
<PackageReference Include="MongoDB.Driver" Version="3.4.0" />
44
</ItemGroup>
55

66
<ItemGroup>

0 commit comments

Comments
 (0)