Add CreateProfileHistoryCommand and mapping profiles
Introduce `CreateProfileHistoryCommand` to handle the creation of profile execution history records, including properties for `ProfileId`, `Result`, `ResultText`, and `AddedWho`. Add `CreateProfileHistoryCommandHandler` to process the command and persist the data using `IProfileHistoryRepository`. Define AutoMapper mappings in `MappingProfiles` to map `CreateProfileHistoryCommand` to the `ProfileHistory` entity, with specific configurations for ignored and mapped properties.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using ECMJobRunner.Domain.Entities;
|
||||
using ECMJobRunner.Domain.Interfaces;
|
||||
using ECMJobRunner.Domain.ValueObjects;
|
||||
using MediatR;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ECMJobRunner.Application.ProfileHistories.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Command to create a new profile execution history record
|
||||
/// </summary>
|
||||
public class CreateProfileHistoryCommand : IRequest<Unit>
|
||||
{
|
||||
/// <summary>
|
||||
/// Foreign key to the related profile
|
||||
/// </summary>
|
||||
public long ProfileId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Execution result type
|
||||
/// </summary>
|
||||
public ResultType Result { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Result text/message
|
||||
/// </summary>
|
||||
#if NET
|
||||
public required string ResultText { get; set; }
|
||||
#else
|
||||
|
||||
public string ResultText { get; set; } = null!;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Created by (max 50 chars)
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public string AddedWho { get; set; } = "ECMJobRunner";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handler for <see cref="CreateProfileHistoryCommand"/>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Constructor
|
||||
/// </remarks>
|
||||
public class CreateProfileHistoryCommandHandler(IProfileHistoryRepository Repository) : IRequestHandler<CreateProfileHistoryCommand, Unit>
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles the command by persisting a new <see cref="ProfileHistory"/> record
|
||||
/// </summary>
|
||||
public async Task<Unit> Handle(CreateProfileHistoryCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
await Repository.AddAsync(request, cancellationToken);
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
ECMJobRunner.Application/ProfileHistories/MappingProfiles.cs
Normal file
28
ECMJobRunner.Application/ProfileHistories/MappingProfiles.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using ECMJobRunner.Application.ProfileHistories.Commands;
|
||||
using ECMJobRunner.Domain.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ECMJobRunner.Application.ProfileHistories;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class MappingProfiles : AutoMapper.Profile
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public MappingProfiles()
|
||||
{
|
||||
CreateMap<CreateProfileHistoryCommand, ProfileHistory>()
|
||||
.ForMember(dest => dest.Id, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.AddedWhen, opt => opt.MapFrom(_ => DateTime.Now))
|
||||
.ForMember(dest => dest.ChangedWho, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.ChangedWhen, opt => opt.Ignore())
|
||||
.ForMember(dest => dest.CfgProfile, opt => opt.Ignore());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user