예제 #1
0
        public async Task <CommandResult> Handle(RegisterMeasureCommand request, CancellationToken cancellationToken)
        {
            CommandResult commandInvariantResult = await ValidateCommandInvariants(request);

            if (!commandInvariantResult.Success)
            {
                return(FailureDueTo(commandInvariantResult.Notifications.ToList()));
            }

            BasicMeasure basicMeasure = new BasicMeasure(request.Height, request.Weight);

            List <Image> bodyPictures = new List <Image>();

            foreach (IFormFile bodyPicture in request.BodyPictures)
            {
                StorageResult storageResult = await UploadBodyPictureToStorage(bodyPicture);

                if (!storageResult.Success)
                {
                    return(FailureDueToUploadFailure());
                }

                bodyPictures.Add(new Image(storageResult.FileName, storageResult.FileUrl));
            }

            Measure measure = new Measure(
                _currentProfileId,
                request.Title,
                request.Details,
                basicMeasure,
                request.MeasureDate,
                bodyPictures,
                _mapper.Map <List <MeasureLine> >(request.MeasureLines)
                );

            await _measureRepository.RegisterAsync(measure);

            return(await CommitAndPublishAsync(new MeasureRegisteredDomainEvent(
                                                   measure.Id,
                                                   measure.ProfileId,
                                                   request.WritePost
                                                   )));
        }