| | 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.Update; |
| | 8 | |
|
| 4 | 9 | | public sealed class UpdateActivityHandler(IRepository<Activity> repository) |
| | 10 | | : ICommandHandler<UpdateActivityRequest, Result> |
| | 11 | | { |
| | 12 | | public async Task<Result> Handle(UpdateActivityRequest request, CancellationToken cancellationToken) |
| | 13 | | { |
| 4 | 14 | | var activityId = request.Id; |
| 4 | 15 | | var existingActivity = await repository.GetByIdAsync(activityId, cancellationToken); |
| | 16 | |
|
| 4 | 17 | | if (existingActivity is null) |
| | 18 | | { |
| 2 | 19 | | return ActivityError.NotFound(activityId); |
| | 20 | | } |
| | 21 | |
|
| | 22 | | // Map the existing activity to an updated one, only changing properties that were specified |
| 2 | 23 | | var updatedActivity = existingActivity.MapToUpdatedEntity(request); |
| | 24 | |
|
| 2 | 25 | | await repository.UpdateAsync(updatedActivity, cancellationToken); |
| | 26 | |
|
| 2 | 27 | | return Result.Success(); |
| 4 | 28 | | } |
| | 29 | | } |