dotnet-digital-ops/DigitalOps.CQRS/Commands/TimeEntry/AddTimeEntryCommand.cs

45 lines
1.4 KiB
C#

using DigitalOps.Authority.Services;
using DigitalOps.CQRS.Services.TimeEntry;
using DigitalOps.CQRS.Services.TimeEntry.Options;
using DigitalOps.Dal;
using DigitalOps.Dal.Validators;
using FluentValidation;
using OpenHarbor.CQRS.Abstractions;
namespace DigitalOps.CQRS.Commands.TimeEntry;
public class AddTimeEntryCommand
{
public long ProjectId { get; set; }
public long? ForUser { get; set; }
public required string Description { get; set; }
public DateTime StartAt { get; set; }
public DateTime? EndAt { get; set; }
public TimeOnly? Offset { get; set; }
}
public class AddTimeEntryCommandHandler(TimeEntryService timeEntryService) : ICommandHandler<AddTimeEntryCommand>
{
public async Task HandleAsync(AddTimeEntryCommand command, CancellationToken cancellationToken = new CancellationToken())
{
await timeEntryService.AddTimeEntryAsync(new AddTimeEntryOptions
{
ProjectId = command.ProjectId,
ForUser = command.ForUser,
Description = command.Description,
StartAt = command.StartAt,
EndAt = command.EndAt,
Offset = command.Offset,
}, cancellationToken);
}
}
public class AddTimeEntryCommandValidator : AbstractValidator<AddTimeEntryCommand>
{
public AddTimeEntryCommandValidator(MainDbContext dbContext, UserIdentityService userIdentityService)
{
RuleFor(command => command.ProjectId)
.NotEmpty()
.SetValidator(new DbEntityExistValidator<Dal.DbEntity.Project, long>(dbContext));
}
}