Skip to content

Commit 2f693c6

Browse files
committed
feat: general improvements
1 parent 77d17bf commit 2f693c6

File tree

11 files changed

+55
-10
lines changed

11 files changed

+55
-10
lines changed

src/WebExpress.WebCore/WebAsset/AssetContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class AssetContext : IAssetContext
5151
/// <summary>
5252
/// Returns the attributes associated with the page.
5353
/// </summary>
54-
public IEnumerable<Type> Attributes => [];
54+
public IEnumerable<Attribute> Attributes => [];
5555

5656
/// <summary>
5757
/// Initializes a new instance of the class with the specified endpoint manager, parent type, context path, and path segment.

src/WebExpress.WebCore/WebEndpoint/EndpointManager.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,37 @@ public static IRoute CreateEndpointRoute
249249

250250
return uri;
251251
}
252+
253+
/// <summary>
254+
/// Creates instances of attributes from a collection of <see cref="CustomAttributeData"/> objects.
255+
/// </summary>
256+
/// <param name="customAttributesData">A collection of objects representing the metadata of attributes.</param>
257+
/// <returns>An enumerable of instances created from the provided metadata. If
258+
/// no attributes are instantiated, an empty collection is returned.</returns>
259+
public static IEnumerable<Attribute> GetAttributeInstances(IEnumerable<CustomAttributeData> customAttributesData)
260+
{
261+
List<Attribute> attributeInstances = [];
262+
263+
foreach (var attrData in customAttributesData)
264+
{
265+
var attributeType = attrData.AttributeType;
266+
var constructorArgs = new List<object>();
267+
268+
// extract constructor arguments
269+
foreach (var arg in attrData.ConstructorArguments)
270+
{
271+
constructorArgs.Add(arg.Value);
272+
}
273+
274+
// instantiate the attribute using Reflection
275+
var attributeInstance = Activator.CreateInstance(attributeType, constructorArgs.ToArray()) as Attribute;
276+
if (attributeInstance != null)
277+
{
278+
attributeInstances.Add(attributeInstance);
279+
}
280+
}
281+
282+
return attributeInstances;
283+
}
252284
}
253285
}

src/WebExpress.WebCore/WebEndpoint/IEndpointContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public interface IEndpointContext : IContext
5050
/// <summary>
5151
/// Returns the attributes associated with the page.
5252
/// </summary>
53-
IEnumerable<Type> Attributes { get; }
53+
IEnumerable<Attribute> Attributes { get; }
5454
}
5555
}

src/WebExpress.WebCore/WebMessage/Response.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,17 @@ public abstract class Response
3434
protected Response()
3535
{
3636
}
37+
38+
/// <summary>
39+
/// Appends the specified content type to the existing Content-Type header value.
40+
/// </summary>
41+
/// <param name="contentType">The content type to append. Cannot be null or empty.</param>
42+
/// <returns>The current instance, allowing for method chaining.</returns>
43+
public Response AddHeaderContentType(string contentType)
44+
{
45+
Header.ContentType = contentType?.Trim();
46+
47+
return this;
48+
}
3749
}
3850
}

src/WebExpress.WebCore/WebPage/PageContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class PageContext : IPageContext
5959
/// <summary>
6060
/// Returns the attributes associated with the page.
6161
/// </summary>
62-
public IEnumerable<Type> Attributes { get; internal set; }
62+
public IEnumerable<Attribute> Attributes { get; internal set; }
6363

6464
/// <summary>
6565
/// Returns the context path.

src/WebExpress.WebCore/WebPage/PageManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private void Register(IPluginContext pluginContext, IEnumerable<IApplicationCont
371371
Cache = cache,
372372
Conditions = conditions,
373373
IncludeSubPaths = includeSubPaths,
374-
Attributes = attributes.Select(x => x.AttributeType)
374+
Attributes = EndpointManager.GetAttributeInstances(attributes)
375375
};
376376

377377
var pageItem = new PageItem(_componentHub.EndpointManager)

src/WebExpress.WebCore/WebResource/ResourceContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class ResourceContext : IResourceContext
5757
/// <summary>
5858
/// Returns the attributes associated with the page.
5959
/// </summary>
60-
public IEnumerable<Type> Attributes { get; internal set; }
60+
public IEnumerable<Attribute> Attributes { get; internal set; }
6161

6262
/// <summary>
6363
/// Initializes a new instance of the class with the specified endpoint manager, parent type, context path, and path segment.

src/WebExpress.WebCore/WebResource/ResourceManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private void Register(IPluginContext pluginContext, IEnumerable<IApplicationCont
178178
Cache = cache,
179179
Conditions = conditions,
180180
IncludeSubPaths = includeSubPaths,
181-
Attributes = attributes.Select(x => x.AttributeType)
181+
Attributes = EndpointManager.GetAttributeInstances(attributes)
182182
};
183183

184184
var resourceItem = new ResourceItem(_componentHub.ResourceManager)

src/WebExpress.WebCore/WebRestAPI/RestApiContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class RestApiContext : IRestApiContext
5757
/// <summary>
5858
/// Returns the attributes associated with the page.
5959
/// </summary>
60-
public IEnumerable<Type> Attributes { get; internal set; }
60+
public IEnumerable<Attribute> Attributes { get; internal set; }
6161

6262
/// <summary>
6363
/// Returns the context path.

src/WebExpress.WebCore/WebRestAPI/RestApiManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ private RestApiManager(IComponentHub componentHub, IHttpServerContext httpServer
9191
return new ResponseOK
9292
{
9393
Content = content
94-
};
94+
}
95+
.AddHeaderContentType("application/json");
9596
}
9697

9798
return new ResponseOK();
@@ -387,7 +388,7 @@ private void Register(IPluginContext pluginContext, IEnumerable<IApplicationCont
387388
Cache = cache,
388389
Conditions = conditions,
389390
IncludeSubPaths = includeSubPaths,
390-
Attributes = attributes.Select(x => x.AttributeType),
391+
Attributes = EndpointManager.GetAttributeInstances(attributes),
391392
Version = version,
392393
Methods = methods.Distinct()
393394
};

0 commit comments

Comments
 (0)