public async Task HandleAsync(SignUp command) { var userId = Guid.NewGuid().ToString("N"); await _handler .Run(async() => await _userService.SignUpAsync(userId, command.Email, command.Role.Empty() ? Roles.User : command.Role, Providers.Collectively, password: command.Password, name: command.Name, culture: command.Request.Culture, activate: command.State == "active")) .OnSuccess(async() => { var user = await _userService.GetAsync(userId); var resource = _resourceFactory.Resolve <SignedUp>(userId); await _bus.PublishAsync(new SignedUp(command.Request.Id, resource, userId, user.Value.Provider, user.Value.Role, user.Value.State)); await PublishSendActivationEmailMessageCommandAsync(user.Value, command.Request); }) .OnCustomError(async ex => await _bus.PublishAsync(new SignUpRejected(command.Request.Id, null, ex.Code, ex.Message, command.Provider))) .OnError(async(ex, logger) => { logger.Error(ex, "Error occured while signing up a user"); await _bus.PublishAsync(new SignUpRejected(command.Request.Id, null, OperationCodes.Error, ex.Message, command.Provider)); }) .ExecuteAsync(); }
public async Task HandleAsync(ResolveRemark command) { File file = null; await _handler.Validate(async() => { if (command.ValidatePhoto) { var resolvedFile = _fileResolver.FromBase64(command.Photo.Base64, command.Photo.Name, command.Photo.ContentType); if (resolvedFile.HasNoValue) { Logger.Error($"File cannot be resolved from base64, photoName:{command.Photo.Name}, " + $"contentType:{command.Photo.ContentType}, userId:{command.UserId}"); throw new ServiceException(OperationCodes.CannotConvertFile); } file = resolvedFile.Value; var isImage = _fileValidator.IsImage(file); if (isImage == false) { Logger.Warning($"File is not an image! name:{file.Name}, contentType:{file.ContentType}, " + $"userId:{command.UserId}"); throw new ServiceException(OperationCodes.InvalidFile); } } var remark = await _remarkService.GetAsync(command.RemarkId); if (remark.Value.Group == null) { return; } await _groupService.ValidateIfRemarkCanBeResolvedOrFailAsync(remark.Value.Group.Id, command.UserId); }) .Run(async() => { Location location = null; if (command.Latitude != 0 && command.Longitude != 0) { location = Location.Create(command.Latitude, command.Longitude, command.Address); } await _remarkStateService.ResolveAsync(command.RemarkId, command.UserId, command.Description, location, file, command.ValidateLocation); }) .OnSuccess(async() => { var remark = await _remarkService.GetAsync(command.RemarkId); var state = remark.Value.GetLatestStateOf(RemarkState.Names.Resolved).Value; var resource = _resourceFactory.Resolve <RemarkResolved>(command.RemarkId); await _bus.PublishAsync(new RemarkResolved(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(async ex => await _bus.PublishAsync(new ResolveRemarkRejected(command.Request.Id, command.UserId, command.RemarkId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, "Error occured while resolving a remark."); await _bus.PublishAsync(new ResolveRemarkRejected(command.Request.Id, command.UserId, command.RemarkId, OperationCodes.Error, ex.Message)); }) .ExecuteAsync(); }
public async Task HandleAsync(AddPhotosToRemark command) { await _handler .Validate(() => { if (command.Photos == null || !command.Photos.Any()) { throw new ServiceException(OperationCodes.NoFiles, $"There are no photos to be added to the remark with id: '{command.RemarkId}'."); } if (command.Photos.Count() > _generalSettings.PhotosLimit) { throw new ServiceException(OperationCodes.TooManyFiles); } }) .Run(async() => { var photos = new List <File>(); foreach (var file in command.Photos) { var resolvedFile = _fileResolver.FromBase64(file.Base64, file.Name, file.ContentType); if (resolvedFile.HasNoValue) { throw new ServiceException(OperationCodes.CannotConvertFile); } var photo = resolvedFile.Value; // var isImage = _fileValidator.IsImage(photo); // if (!isImage) // { // throw new ServiceException(OperationCodes.InvalidFile); // } photos.Add(photo); } await _remarkPhotoService.AddPhotosAsync(command.RemarkId, command.UserId, photos.ToArray()); }) .OnSuccess(async() => { var remark = await _remarkService.GetAsync(command.RemarkId); var resource = _resourceFactory.Resolve <PhotosToRemarkAdded>(command.RemarkId); await _bus.PublishAsync(new PhotosToRemarkAdded(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(ex => _bus.PublishAsync(new AddPhotosToRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, "Error occured while adding photos to remark."); await _bus.PublishAsync(new AddPhotosToRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, OperationCodes.Error, ex.Message)); }) .ExecuteAsync(); }
public async Task HandleAsync(RemovePhotosFromRemark command) { var groupIds = new Guid[] {}; var removedPhotos = new string[] {}; await _handler .Validate(async() => await _remarkService.ValidateEditorAccessOrFailAsync(command.RemarkId, command.UserId)) .Run(async() => { groupIds = command.Photos? .Where(x => x.GroupId != Guid.Empty) .Select(x => x.GroupId) .ToArray() ?? new Guid[] {}; var names = command.Photos? .Where(x => x.Name.NotEmpty()) .Select(x => x.Name) .ToArray() ?? new string[] {}; var namesForGroups = await _remarkPhotoService.GetPhotosForGroupsAsync(command.RemarkId, groupIds); removedPhotos = names .Union(namesForGroups.HasValue ? namesForGroups.Value : Enumerable.Empty <string>()) .Distinct() .ToArray(); await _remarkPhotoService.RemovePhotosAsync(command.RemarkId, removedPhotos); }) .OnSuccess(async() => { var resource = _resourceFactory.Resolve <PhotosFromRemarkRemoved>(command.RemarkId); await _bus.PublishAsync(new PhotosFromRemarkRemoved(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(ex => _bus.PublishAsync(new RemovePhotosFromRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, $"Error occured while removing photos from the remark with id: '{command.RemarkId}'."); await _bus.PublishAsync(new RemovePhotosFromRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, OperationCodes.Error, ex.Message)); }) .ExecuteAsync(); }
public async Task HandleAsync(CreateOrganization command) => await _handler .Run(async() => await _organizationService.CreateAsync(command.OrganizationId, command.Name, command.UserId, command.IsPublic, command.Criteria)) .OnSuccess(async() => { var resource = _resourceFactory.Resolve <OrganizationCreated>(command.OrganizationId); await _bus.PublishAsync(new OrganizationCreated(command.Request.Id, resource, command.UserId, command.OrganizationId)); }) .OnCustomError(async ex => await _bus.PublishAsync( new CreateOrganizationRejected(command.Request.Id, command.UserId, command.OrganizationId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, $"Error when trying to create an organization: '{command.Name}', id: '{command.OrganizationId}'."); await _bus.PublishAsync(new CreateOrganizationRejected(command.Request.Id, command.UserId, command.OrganizationId, OperationCodes.Error, "Error when trying to create an organization.")); }) .ExecuteAsync();
public async Task HandleAsync(DeleteRemark command) { await _handler .Validate(async() => { var remark = await _remarkService.GetAsync(command.RemarkId); try { await _remarkService.ValidateEditorAccessOrFailAsync(command.RemarkId, command.UserId); return; } catch { if (remark.Value.Group == null) { throw; } } await _groupService.ValidateIfRemarkCanBeCanceledOrFailAsync(remark.Value.Group.Id, command.UserId); }) .Run(async() => { await _remarkService.DeleteAsync(command.RemarkId); }) .OnSuccess(async() => { var resource = _resourceFactory.Resolve <RemarkDeleted>(command.RemarkId); await _bus.PublishAsync(new RemarkDeleted(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(ex => _bus.PublishAsync(new DeleteRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, "Error occured while deleting a remark."); await _bus.PublishAsync(new DeleteRemarkRejected(command.Request.Id, command.RemarkId, command.UserId, OperationCodes.Error, ex.Message)); }) .ExecuteAsync(); }
private async Task <Maybe <User> > HandleFacebookSignUpAsync(FacebookUser facebookUser, SignIn command) { var externalUserId = facebookUser.Id; var userId = Guid.NewGuid().ToString("N"); await _userService.SignUpAsync(userId, facebookUser.Email, Roles.User, Providers.Facebook, externalUserId : externalUserId); Logger.Information($"Created new user with id: '{userId}' using Facebook user id: '{externalUserId}'"); await TryUploadFacebookAvatarAsync(userId, externalUserId); var user = await _userService.GetByExternalUserIdAsync(externalUserId); var resource = _resourceFactory.Resolve <SignedUp>(userId); await _bus.PublishAsync(new SignedUp(command.Request.Id, resource, userId, user.Value.Provider, user.Value.Role, user.Value.State)); await _authenticationService.SignInViaFacebookAsync(command.SessionId, command.AccessToken, command.IpAddress, command.UserAgent); return(await _userService.GetByExternalUserIdAsync(externalUserId)); }
public async Task HandleAsync(RenewRemark command) { await _handler .Validate(async() => { var remark = await _remarkService.GetAsync(command.RemarkId); if (remark.Value.Group == null) { return; } await _groupService.ValidateIfRemarkCanBeRenewedOrFailAsync(remark.Value.Group.Id, command.UserId); }) .Run(async() => { Location location = null; if (command.Latitude != 0 && command.Longitude != 0) { location = Location.Create(command.Latitude, command.Longitude, command.Address); } await _remarkStateService.RenewAsync(command.RemarkId, command.UserId, command.Description, location); }) .OnSuccess(async() => { var remark = await _remarkService.GetAsync(command.RemarkId); var state = remark.Value.GetLatestStateOf(RemarkState.Names.Renewed).Value; var resource = _resourceFactory.Resolve <RemarkRenewed>(command.RemarkId); await _bus.PublishAsync(new RemarkRenewed(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(async ex => await _bus.PublishAsync(new RenewRemarkRejected(command.Request.Id, command.UserId, command.RemarkId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, "Error occured while renewing a remark."); await _bus.PublishAsync(new RenewRemarkRejected(command.Request.Id, command.UserId, command.RemarkId, OperationCodes.Error, ex.Message)); }) .ExecuteAsync(); }
public async Task HandleAsync(EditRemark command) => await _handler .Validate(async() => await _remarkService.ValidateEditorAccessOrFailAsync(command.RemarkId, command.UserId)) .Run(async() => { Location location = null; if (command.Latitude.HasValue && command.Latitude != 0 && command.Longitude.HasValue && command.Longitude != 0) { var locations = await _locationService.GetAsync(command.Latitude.Value, command.Longitude.Value); 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}."); } var address = locations.Value.Results.First().FormattedAddress; location = Domain.Location.Create(command.Latitude.Value, command.Longitude.Value, address); } await _remarkService.EditAsync(command.RemarkId, command.UserId, command.GroupId, command.Category, command.Description, location); }) .OnSuccess(async() => { var remark = await _remarkService.GetAsync(command.RemarkId); var resource = _resourceFactory.Resolve <RemarkEdited>(command.RemarkId); await _bus.PublishAsync(new RemarkEdited(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(async ex => await _bus.PublishAsync(new EditRemarkRejected(command.Request.Id, command.UserId, command.RemarkId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, "Error occured while editing a remark."); await _bus.PublishAsync(new EditRemarkRejected(command.Request.Id, command.UserId, command.RemarkId, OperationCodes.Error, ex.Message)); }) .ExecuteAsync();
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(); }
public async Task HandleAsync(ProcessRemark command) { var remarkProcessed = false; await _handler .Validate(async() => { await _policy.ValidateAsync(command.RemarkId, command.UserId); var remark = await _remarkService.GetAsync(command.RemarkId); if (remark.Value.Group == null) { return; } await _groupService.ValidateIfRemarkCanBeProcessedOrFailAsync(remark.Value.Group.Id, command.UserId); }) .Run(async() => { Location location = null; if (command.Latitude != 0 && command.Longitude != 0) { location = Location.Create(command.Latitude, command.Longitude, command.Address); } await _remarkStateService.ProcessAsync(command.RemarkId, command.UserId, command.Description, location); }) .OnSuccess(async() => { remarkProcessed = true; var remark = await _remarkService.GetAsync(command.RemarkId); var state = remark.Value.GetLatestStateOf(RemarkState.Names.Processing).Value; var resource = _resourceFactory.Resolve <RemarkProcessed>(command.RemarkId); await _bus.PublishAsync(new RemarkProcessed(command.Request.Id, resource, command.UserId, command.RemarkId)); }) .OnCustomError(async ex => await _bus.PublishAsync(new ProcessRemarkRejected(command.Request.Id, command.UserId, command.RemarkId, ex.Code, ex.Message))) .OnError(async(ex, logger) => { logger.Error(ex, "Error occured while processing a remark."); await _bus.PublishAsync(new ProcessRemarkRejected(command.Request.Id, command.UserId, command.RemarkId, OperationCodes.Error, ex.Message)); }) .Next() .Run(async() => { if (!remarkProcessed) { return; } var participant = await _remarkActionService.GetParticipantAsync(command.RemarkId, command.UserId); if (participant.HasValue) { return; } var takeRemarkAction = new TakeRemarkAction { Request = Messages.Commands.Request.From <TakeRemarkAction>(command.Request), UserId = command.UserId, RemarkId = command.RemarkId, Description = command.Description }; await _bus.PublishAsync(takeRemarkAction); }) .Next() .ExecuteAllAsync(); }