Skip to content

Commit

Permalink
Update Node Entity Configuration (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: NikolaVetnic <[email protected]>
  • Loading branch information
HunorTotBagi and NikolaVetnic authored Jan 3, 2025
1 parent 24c0491 commit 7885103
Show file tree
Hide file tree
Showing 26 changed files with 321 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Api.Converters;

public class FileIdConverter : IRegister
{
public void Register(TypeAdapterConfig config) =>
config.NewConfig<FileAssetId, FileAssetId>().ConstructUsing(src => FileAssetId.Of(src.Value));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Api.Converters;

public class NodeIdConverter : IRegister
{
public void Register(TypeAdapterConfig config) =>
config.NewConfig<NodeId, NodeId>().ConstructUsing(src => NodeId.Of(src.Value));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Api.Converters;

public class NoteIdConverter : IRegister
{
public void Register(TypeAdapterConfig config) =>
config.NewConfig<NoteId, NoteId>().ConstructUsing(src => NoteId.Of(src.Value));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Api.Converters;

public class ReminderIdConverter : IRegister
{
public void Register(TypeAdapterConfig config) =>
config.NewConfig<ReminderId, ReminderId>().ConstructUsing(src => ReminderId.Of(src.Value));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Api.Converters;

public class TimelineIdConverter : IRegister
{
public void Register(TypeAdapterConfig config) =>
config.NewConfig<TimelineId, TimelineId>().ConstructUsing(src => TimelineId.Of(src.Value));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Mapster;
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Text.Json.Serialization;
namespace BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Domain.ValueObjects.Ids;

[JsonConverter(typeof(FileAssetIdJsonConverter))]
public record FileAssetId : StronglyTypedId
public class FileAssetId : StronglyTypedId
{
private FileAssetId(Guid value) : base(value) { }

public static FileAssetId Of(Guid value) => new(value);

private class FileAssetIdJsonConverter : StronglyTypedIdJsonConverter<FileAssetId>;
public override string ToString() => Value.ToString();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Text.Json.Serialization;
namespace BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Domain.ValueObjects.Ids;

[JsonConverter(typeof(NodeIdJsonConverter))]
public record NodeId : StronglyTypedId
public class NodeId : StronglyTypedId
{
private NodeId(Guid value) : base(value) { }

public static NodeId Of(Guid value) => new(value);

private class NodeIdJsonConverter : StronglyTypedIdJsonConverter<NodeId>;
public override string ToString() => Value.ToString();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Text.Json.Serialization;
namespace BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Domain.ValueObjects.Ids;

[JsonConverter(typeof(NoteIdJsonConverter))]
public record NoteId : StronglyTypedId
public class NoteId : StronglyTypedId
{
private NoteId(Guid value) : base(value) { }

public static NoteId Of(Guid value) => new(value);

private class NoteIdJsonConverter : StronglyTypedIdJsonConverter<NoteId>;
public override string ToString() => Value.ToString();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Text.Json.Serialization;
namespace BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Domain.ValueObjects.Ids;

[JsonConverter(typeof(ReminderIdJsonConverter))]
public record ReminderId : StronglyTypedId
public class ReminderId : StronglyTypedId
{
private ReminderId(Guid value) : base(value) { }

public static ReminderId Of(Guid value) => new(value);

private class ReminderIdJsonConverter : StronglyTypedIdJsonConverter<ReminderId>;
public override string ToString() => Value.ToString();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Text.Json.Serialization;
namespace BuildingBlocks.Domain.ValueObjects.Ids;

namespace BuildingBlocks.Domain.ValueObjects.Ids;

[JsonConverter(typeof(TimelineIdJsonConverter))]
public record TimelineId : StronglyTypedId
public class TimelineId : StronglyTypedId
{
private TimelineId(Guid value) : base(value) { }

public static TimelineId Of(Guid value) => new(value);

private class TimelineIdJsonConverter : StronglyTypedIdJsonConverter<TimelineId>;
public override string ToString() => Value.ToString();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Text.Json.Serialization;
using System.Text.Json;
namespace BuildingBlocks.Domain.ValueObjects;

namespace BuildingBlocks.Domain.ValueObjects;

public abstract record StronglyTypedId
public abstract class StronglyTypedId
{
protected StronglyTypedId(Guid value)
{
Expand All @@ -14,27 +11,6 @@ protected StronglyTypedId(Guid value)
}

public Guid Value { get; }

public override string ToString() => Value.ToString();
}

public class StronglyTypedIdJsonConverter<T> : JsonConverter<T> where T : StronglyTypedId
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();

if (Guid.TryParse(value, out var guid))
{
var constructor = typeof(T).GetConstructor(new[] { typeof(Guid) });

if (constructor != null)
return (T)constructor.Invoke(new object[] { guid });
}

throw new JsonException($"Invalid GUID format for {typeof(T).Name}: {value}");
}

public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.Value.ToString());

public abstract override string ToString();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BuildingBlocks.Api.Converters;
using Files.Application.Extensions;
using Files.Infrastructure;
using Microsoft.Extensions.Configuration;
Expand All @@ -19,6 +20,8 @@ public static IServiceCollection AddFilesModule

private static IServiceCollection AddApiServices(this IServiceCollection services)
{
TypeAdapterConfig.GlobalSettings.Scan(typeof(FileIdConverter).Assembly);

return services;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection
config.AddOpenBehavior(typeof(LoggingBehavior<,>));
});

services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BuildingBlocks.Api.Converters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nodes.Application.Extensions;
Expand All @@ -19,6 +20,8 @@ public static IServiceCollection AddNodesModule

private static IServiceCollection AddApiServices(this IServiceCollection services)
{
TypeAdapterConfig.GlobalSettings.Scan(typeof(NodeIdConverter).Assembly);

return services;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static IServiceCollection AddApplicationServices(this IServiceCollection

services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());

services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());

return services;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Nodes.Infrastructure.Data.Configurations;

public class NodeConfiguration : IEntityTypeConfiguration<Node>
{
public void Configure(EntityTypeBuilder<Node> builder)
{
builder.HasKey(n => n.Id);
builder.Property(n => n.Id).HasConversion(
nodeId => nodeId.Value,
dbId => NodeId.Of(dbId));

// ToDo: Add remaining Node configuration commands
}
}
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Nodes.Infrastructure.Data.Configurations;

public class NodeConfiguration : IEntityTypeConfiguration<Node>
{
public void Configure(EntityTypeBuilder<Node> builder)
{
builder.HasKey(n => n.Id);
builder.Property(n => n.Id).HasConversion(
nodeId => nodeId.Value,
dbId => NodeId.Of(dbId));

builder.Property(n => n.Title)
.IsRequired()
.HasMaxLength(100);

builder.Property(n => n.Description)
.IsRequired()
.HasMaxLength(500);

builder.Property(n => n.Importance)
.IsRequired();

builder.Property(n => n.Phase)
.IsRequired();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7885103

Please sign in to comment.