From 46664d62bab764b4d66cc972c20fa3a753da9f38 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 1 Dec 2025 15:47:58 +0100 Subject: [PATCH] Refactor GetOrCreateCommand and add AutoMapper profile Refactored the `GetOrCreateCommand` to simplify the creation of `Endpoint` entities by delegating the creation logic to `repo.CreateAsync`. Added a new `MappingProfile` class using AutoMapper to define mappings between command objects (`CreateEndpointCommand` and `GetOrCreateCommand`) and the `Endpoint` entity, improving code maintainability and reducing boilerplate. Included necessary `using` directives in `MappingProfile.cs` to support the new mapping functionality. --- .../Commands/GetOrCreateCommand.cs.cs | 7 +------ .../Endpoints/MappingProfile.cs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src/ReC.Application/Endpoints/MappingProfile.cs diff --git a/src/ReC.Application/Endpoints/Commands/GetOrCreateCommand.cs.cs b/src/ReC.Application/Endpoints/Commands/GetOrCreateCommand.cs.cs index e12d53d..0535020 100644 --- a/src/ReC.Application/Endpoints/Commands/GetOrCreateCommand.cs.cs +++ b/src/ReC.Application/Endpoints/Commands/GetOrCreateCommand.cs.cs @@ -1,7 +1,6 @@ using DigitalData.Core.Abstraction.Application.Repository; using MediatR; using Microsoft.EntityFrameworkCore; -using ReC.Domain.Entities; namespace ReC.Application.Endpoints.Commands; @@ -19,11 +18,7 @@ public class GetOrCreateCommandHandler(IRepository repo) : IRequestHan if (endpoint is not null) return endpoint; - endpoint = new Endpoint - { - Uri = request.Uri - }; - await repo.CreateAsync(endpoint, cancel); + endpoint = await repo.CreateAsync(request, cancel); return endpoint; } } \ No newline at end of file diff --git a/src/ReC.Application/Endpoints/MappingProfile.cs b/src/ReC.Application/Endpoints/MappingProfile.cs new file mode 100644 index 0000000..1709186 --- /dev/null +++ b/src/ReC.Application/Endpoints/MappingProfile.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using ReC.Application.Endpoints.Commands; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ReC.Application.Endpoints; + +public class MappingProfile : Profile +{ + public MappingProfile() + { + CreateMap(); + CreateMap(); + } +}