Skip to content

Commit ed9ab26

Browse files
committed
ProxyFactory ensure that entity exists in database
1 parent f18cd96 commit ed9ab26

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

src/DragonFly.Core/Modules/Assets/Models/Asset.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ public class Asset : ContentBase<Asset>
1111
{
1212
public Asset()
1313
{
14-
//Name = string.Empty;
15-
//Alt = string.Empty;
16-
//Description = string.Empty;
17-
//Slug = string.Empty;
18-
//MimeType = string.Empty;
19-
//Hash = string.Empty;
20-
//PreviewUrl = string.Empty;
21-
//Size = 0;
22-
//_metaddata = new AssetMetadatas();
2314
}
2415

2516
public Asset(Guid id)

src/DragonFly.Generator/ProxyGenerator.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
9898
{
9999
bool ignoreProperty = ignoredProperties.Contains(propertySymbol.Name);
100100

101+
if (ignoreProperty)
102+
{
103+
continue;
104+
}
105+
101106
builder.AppendLine($"public override {propertySymbol.Type.ToDisplayString()} {propertySymbol.Name}");
102107
builder.AppendBlock(x =>
103108
{
@@ -106,13 +111,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
106111
builder.AppendLine($"get");
107112
builder.AppendBlock(x =>
108113
{
109-
if (ignoreProperty == false)
110-
{
111-
x.AppendLine($"{methodCaller}(\"{propertySymbol.GetMethod.Name}\"){methodCallerAsyncExtensions};");
112-
}
114+
x.AppendLine($"{methodCaller}(\"{propertySymbol.GetMethod.Name}\"){methodCallerAsyncExtensions};");
113115
x.AppendLine($"return _invocationTarget.{propertySymbol.Name};");
114116
});
115-
116117
}
117118
if (propertySymbol.SetMethod != null)
118119
{
@@ -136,17 +137,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
136137
{
137138
bool ignoreMethod = ignoredMethods.Contains(methodSymbol.Name);
138139

140+
if (ignoreMethod)
141+
{
142+
continue;
143+
}
144+
139145
string parameterDefinition = $"{string.Join(", ", methodSymbol.Parameters.Select(x => $"{x.Type.ToDisplayString()} {x.Name}"))}";
140146

141147
builder.AppendTabs();
142148
builder.Append($"public override {methodSymbol.ReturnType.OriginalDefinition.ToDisplayString()} {methodSymbol.Name}({parameterDefinition})");
143149
builder.AppendLineBreak();
144150
builder.AppendBlock(x =>
145151
{
146-
if (ignoreMethod == false)
147-
{
148-
builder.AppendLine($"{methodCaller}(\"{methodSymbol.Name}\"){methodCallerAsyncExtensions};");
149-
}
152+
builder.AppendLine($"{methodCaller}(\"{methodSymbol.Name}\"){methodCallerAsyncExtensions};");
150153

151154
builder.AppendTabs();
152155

src/DragonFly.Generator/SourceBuilder/SourceBuilder.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,23 @@ public void AppendBlock(Action<SourceBuilder> action, bool command = false)
6767
}
6868
}
6969

70+
public void AppendTryCatch(Action<SourceBuilder> tryBlock, Action<SourceBuilder>? catchBlock = null, Action<SourceBuilder>? finallyBlock = null)
71+
{
72+
AppendLine("try");
73+
AppendBlock(tryBlock);
74+
75+
if (catchBlock != null)
76+
{
77+
AppendLine("catch (Exception ex)");
78+
AppendBlock(catchBlock);
79+
}
80+
81+
if (finallyBlock != null)
82+
{
83+
AppendLine("finally");
84+
AppendBlock(finallyBlock);
85+
}
86+
}
87+
7088
public override string ToString() => Builder.ToString();
7189
}

src/DragonFly.MongoDB/Proxies/ProxyFactory.cs

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

5+
using MongoDB.Driver;
6+
57
namespace DragonFly.MongoDB;
68

79
internal static class ProxyFactory
810
{
9-
public static ContentItem CreateContent(string schema, Guid id)
11+
public static ContentItem? CreateContent(string schema, Guid id)
1012
{
11-
return new ContentItemProxy(CreateSchema(schema), id);
13+
var collection = MongoStorage.Default.GetMongoCollection(schema);
14+
15+
if (collection.AsQueryable().Where(x => x.Id == id).Any())
16+
{
17+
return new ContentItemProxy(CreateSchema(schema), id);
18+
}
19+
else
20+
{
21+
return null;
22+
}
1223
}
1324

1425
public static ContentSchema CreateSchema(string schema)
1526
{
1627
return new ContentSchemaProxy(schema);
1728
}
1829

19-
public static Asset CreateAsset(Guid assetId)
30+
public static Asset? CreateAsset(Guid assetId)
2031
{
21-
return new AssetProxy(assetId);
32+
if (MongoStorage.Default.Assets.AsQueryable().Where(x => x.Id == assetId).Any())
33+
{
34+
return new AssetProxy(assetId);
35+
}
36+
else
37+
{
38+
return null;
39+
}
2240
}
2341

2442
public static AssetFolder CreateAssetFolder(Guid assetFolderId)

0 commit comments

Comments
 (0)