public async Task <Saga> Handle(GetSagaQuery request, CancellationToken cancellationToken = default) { var saga = await _repository.GetAsync(request.Id, cancellationToken); if (saga is null) { throw new SagaNotFoundException(request.Id.ToString()); } return(saga); }
public async Task <IEnumerable <Chapter> > Handle(GetAllChaptersQuery request, CancellationToken cancellationToken = default) { var saga = await _sagaRepository.GetAsync(request.SagaId, cancellationToken); if (saga is null) { throw new SagaNotFoundException(request.SagaId.ToString()); } var chapters = await _chapterRepository.GetAsync(request.SagaId, cancellationToken); return(chapters?.AsReadOnly() ?? Enumerable.Empty <Chapter>()); }
public async Task <Unit> Handle(UpdateSagaCommand request, CancellationToken cancellationToken = default) { var timestamp = DateTimeOffset.UtcNow; var saga = await _repository.GetAsync(request.Id, cancellationToken); if (saga == null) { throw new SagaNotFoundException(request.Id.ToString()); } if (request.Title != null) { saga.Title = request.Title; saga.UpdatedAt = timestamp; } await _repository.UpdateAsync(saga.Id, saga, cancellationToken); return(Unit.Value); }
public async Task <Chapter> Handle(CreateChapterCommand request, CancellationToken cancellationToken = default) { var saga = await _sagaRepository.GetAsync(request.SagaId, cancellationToken); if (saga is null) { throw new SagaNotFoundException(request.SagaId.ToString()); } var timestamp = DateTimeOffset.UtcNow; var chapter = new Chapter { Id = Guid.NewGuid(), SagaId = request.SagaId, Content = request.Content, CreatedAt = timestamp, UpdatedAt = timestamp, }; return(await _chapterRepository.CreateAsync(chapter, cancellationToken)); }
public async Task <Unit> Handle(DeleteSagaCommand request, CancellationToken cancellationToken = default) { var saga = await _sagaRepository.GetAsync(request.SagaId, cancellationToken); if (saga == null) { throw new SagaNotFoundException(request.SagaId.ToString()); } // We cascade delete the chapters var chapters = await _chapterRepository.GetAsync(request.SagaId, cancellationToken); if (chapters != null) { foreach (var chapter in chapters) { await _chapterRepository.RemoveAsync(chapter, cancellationToken); } } await _sagaRepository.RemoveAsync(saga, cancellationToken); return(Unit.Value); }
public async Task <IEnumerable <Saga> > Handle(GetAllSagasQuery request, CancellationToken cancellationToken = default) { var sagas = await _repository.GetAsync(cancellationToken); return(sagas?.AsReadOnly() ?? Enumerable.Empty <Saga>()); }