| | 1 | | using planora.Application.Common; |
| | 2 | | using planora.Application.Interfaces.Mediator; |
| | 3 | | using planora.Domain.Entities; |
| | 4 | | using planora.Domain.Errors; |
| | 5 | | using planora.Domain.Repositories; |
| | 6 | |
|
| | 7 | | namespace planora.Application.Features.Activities.Commands.Delete; |
| | 8 | |
|
| 8 | 9 | | public sealed class DeleteActivityHandler(IRepository<Activity> repository) |
| | 10 | | : ICommandHandler<DeleteActivityRequest, Result> |
| | 11 | | { |
| | 12 | | public async Task<Result> Handle(DeleteActivityRequest request, CancellationToken cancellationToken) |
| | 13 | | { |
| 8 | 14 | | var activityId = request.Id; |
| | 15 | | // Fetch the activity to be deleted |
| 8 | 16 | | var activityToBeRemoved = await repository.GetByIdAsync(activityId, cancellationToken); |
| | 17 | |
|
| | 18 | | // Check if the activity exists |
| 8 | 19 | | if (activityToBeRemoved is null) |
| | 20 | | { |
| 2 | 21 | | return ActivityError.NotFound(activityId); |
| | 22 | | } |
| | 23 | |
|
| | 24 | | // Ensure the activity is not in the past |
| 6 | 25 | | if (activityToBeRemoved.Date < DateTime.UtcNow) |
| | 26 | | { |
| 2 | 27 | | return ActivityError.CannotDeletePastActivity(activityId); |
| | 28 | | } |
| | 29 | |
|
| 4 | 30 | | await repository.DeleteAsync(activityToBeRemoved, cancellationToken); |
| | 31 | |
|
| 2 | 32 | | return Result.Success(); |
| 6 | 33 | | } |
| | 34 | | } |