public async Task <CreatePercentageActivityResponse> Handle(CreatePercentageActivityCommand request, CancellationToken cancellationToken) { var activityId = this.GuidGenerator.GenerateGuid(); var activity = ActivityAggregateRoot.Create(activityId, request.Name, ActivityType.Percentage); var activityList = this.AggregateRepository.GetById <ActivityListAggregateRoot>(ActivityListAggregateRoot.ActivityListId); activityList.AddActivity(activity.AggregateId, activity.Name); if (request.PreviewImageBytes != null) { using (var connection = this._writeModelStore.OpenConnection()) { var fileCreateCommand = new StoreFileCommand(request.PreviewImageBytes); var response = await FileCommandHandler.GetOrCreateFileAsync(fileCreateCommand, this.AggregateRepository, connection, this.GuidGenerator); activity.AssignPreviewImage(response.FileId); } } await this.AggregateRepository.SaveAsync(activityList); await this.AggregateRepository.SaveAsync(activity); return(new CreatePercentageActivityResponse(activityId)); }
public async Task <AttachFileToTaskResponse> Handle(AttachFileToTaskCommand request, CancellationToken cancellationToken) { if (!request.FileId.HasValue && string.IsNullOrEmpty(request.FilePath)) { throw new ArgumentException(); } using (var connection = this._writeModelStore.OpenConnection()) { Guid fileId; var fileExistedBefore = false; if (!request.FileId.HasValue && !string.IsNullOrEmpty(request.FilePath)) { var fileCreateCommand = new StoreFileCommand(request.FilePath); var fileInStore = await FileCommandHandler.GetOrCreateFileAsync(fileCreateCommand, this.AggregateRepository, connection, this.GuidGenerator); fileId = fileInStore.FileId; fileExistedBefore = fileInStore.FileExistedBefore; } else { Debug.Assert(request.FileId != null, "request.FileId != null"); fileId = request.FileId.Value; fileExistedBefore = true; } var task = this.AggregateRepository.GetById <TaskAggregateRoot>(request.TaskId); task.AttachFile(fileId); await this.AggregateRepository.SaveAsync(task); return(new AttachFileToTaskResponse(fileId, fileExistedBefore)); } }