예제 #1
0
        public async Task <IActionResult> Post([FromBody] CreateRemark command)
        {
            var remark = await _remarkService.CreateAsync(command);

            var dto = new RemarkDto
            {
                Id = remark.Id
            };

            return(Ok(dto));
        }
예제 #2
0
        public async Task HandleAsync(CreateRemark command)
        {
            var address = "";
            await _handler
            .Validate(async() =>
            {
                await _policy.ValidateAsync(command.UserId);
                if (command.GroupId.HasValue)
                {
                    await _groupService.ValidateIfRemarkCanBeCreatedOrFailAsync(command.GroupId.Value,
                                                                                command.UserId, command.Latitude, command.Longitude);
                }
                var locations = await _locationService.GetAsync(command.Latitude, command.Longitude);
                if (locations.HasNoValue || locations.Value.Results == null || !locations.Value.Results.Any())
                {
                    throw new ServiceException(OperationCodes.AddressNotFound,
                                               $"Address was not found for remark with id: '{command.RemarkId}' " +
                                               $"latitude: {command.Latitude}, longitude:  {command.Longitude}.");
                }
                address = locations.Value.Results.First().FormattedAddress;
            })
            .Run(async() =>
            {
                Logger.Debug($"Handle {nameof(CreateRemark)} command, userId: {command.UserId}, " +
                             $"category: {command.Category}, latitude: {command.Latitude}, " +
                             $"longitude:  {command.Longitude}.");

                var location = Domain.Location.Create(command.Latitude, command.Longitude, address);
                var offering = command.Offering;
                if (offering != null)
                {
                    Logger.Information($"Offering for remark: '{command.RemarkId}' " +
                                       $"with price: '{offering.Price} {offering.Currency}'.");
                }

                //TODO: Pass GUID tags
                await _remarkService.CreateAsync(command.RemarkId, command.UserId, command.Category,
                                                 location, command.Description, Enumerable.Empty <string>(), command.GroupId,
                                                 offering?.Price, offering?.Currency, offering?.StartDate, offering?.EndDate);
            })
            .OnSuccess(async() =>
            {
                await PublishOnSocialMediaAsync(command.RemarkId, command.Request.Culture, command.SocialMedia);
                var resource = _resourceFactory.Resolve <RemarkCreated>(command.RemarkId);
                await _bus.PublishAsync(new RemarkCreated(command.Request.Id, resource,
                                                          command.UserId, command.RemarkId));
            })
            .OnCustomError(ex => _bus.PublishAsync(new CreateRemarkRejected(command.Request.Id,
                                                                            command.RemarkId, command.UserId, ex.Code, ex.Message)))
            .OnError(async(ex, logger) =>
            {
                logger.Error(ex, "Error occured while creating remark.");
                await _bus.PublishAsync(new CreateRemarkRejected(command.Request.Id,
                                                                 command.RemarkId, command.UserId, OperationCodes.Error, ex.Message));
            })
            .Next()
            .Run(async() =>
            {
                if (command.Photo == null)
                {
                    return;
                }

                await _bus.PublishAsync(new AddPhotosToRemark
                {
                    RemarkId = command.RemarkId,
                    Request  = Request.New <AddPhotosToRemark>(),
                    UserId   = command.UserId,
                    Photos   = new List <Collectively.Messages.Commands.Models.File>
                    {
                        command.Photo
                    }
                });
            })
            .Next()
            .ExecuteAllAsync();
        }