public async Task HandleAsync(OrganizationCreated @event) { await _handler .Run(async() => { var organization = await _groupServiceClient.GetOrganizationAsync <Organization>(@event.OrganizationId); organization.Value.GroupsCount = organization.Value.Groups?.Count ?? 0; organization.Value.MembersCount = organization.Value.Members?.Count ?? 0; await _organizationRepository.AddAsync(organization.Value); await _organizationCache.AddAsync(organization.Value); var owner = organization.Value.Members.First(x => x.Role == "owner"); var user = await _userRepository.GetByIdAsync(owner.UserId); if (user.Value.Organizations == null) { user.Value.Organizations = new HashSet <UserOrganization>(); } user.Value.Organizations.Add(new UserOrganization { Id = organization.Value.Id, Name = organization.Value.Name, Role = owner.Role, IsActive = owner.IsActive }); await _userRepository.EditAsync(user.Value); await _userCache.AddAsync(user.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(UsernameChanged @event) { await _handler .Run(async() => { Logger.Debug($"Handle {nameof(UsernameChanged)} event, userId:{@event.UserId}, newName:{@event.NewName}"); var user = await _userRepository.GetByIdAsync(@event.UserId); if (user.HasNoValue) { throw new ServiceException(OperationCodes.UserNotFound, $"User name cannot be changed because user: {@event.UserId} does not exist"); } Logger.Debug($"Update userName, userId:{@event.UserId}, newName:{@event.NewName}"); user.Value.Name = @event.NewName; user.Value.State = @event.State; await _userRepository.EditAsync(user.Value); await _cache.AddAsync(user.Value); Logger.Debug($"Update user's remarks with new userName, userId:{@event.UserId}, newName:{@event.NewName}"); await _remarkRepository.UpdateUserNamesAsync(@event.UserId, @event.NewName); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(FavoriteRemarkDeleted @event) { await _handler .Run(async() => { var user = await _userRepository.GetByIdAsync(@event.UserId); if (user.HasNoValue) { throw new ServiceException(OperationCodes.UserNotFound, $"Favorite remark cannot be deleted because user: {@event.UserId} does not exist"); } user.Value.FavoriteRemarks.Remove(@event.RemarkId); await _userRepository.EditAsync(user.Value); await _userCache.AddAsync(user.Value); var remark = await _remarkRepository.GetByIdAsync(@event.RemarkId); if (remark.HasNoValue) { return; } remark.Value.UserFavorites.Remove(@event.UserId); await _remarkRepository.UpdateAsync(remark.Value); await _remarkCache.AddAsync(remark.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(SignedIn @event) { await _handler .Run(async() => { var user = await _userServiceClient.GetAsync <User>(@event.UserId); await _accountStateService.SetAsync(user.Value.UserId, user.Value.State); await _cache.AddAsync(user.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(AccountActivated @event) { await _handler .Run(async() => { var user = await _repository.GetByIdAsync(@event.UserId); user.Value.State = "active"; await _repository.EditAsync(user.Value); await _stateService.SetAsync(user.Value.UserId, user.Value.State); await _cache.AddAsync(user.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(GroupCreated @event) { await _handler .Run(async() => { var group = await _groupServiceClient.GetAsync <Group>(@event.GroupId); group.Value.MembersCount = group.Value.Members.Count; await _groupRepository.AddAsync(group.Value); await _groupCache.AddAsync(group.Value); var owner = group.Value.Members.First(x => x.Role == "owner"); var user = await _userRepository.GetByIdAsync(owner.UserId); if (user.Value.Groups == null) { user.Value.Groups = new HashSet <UserGroup>(); } user.Value.Groups.Add(new UserGroup { Id = group.Value.Id, Name = group.Value.Name, Role = owner.Role, IsActive = owner.IsActive }); await _userRepository.EditAsync(user.Value); await _userCache.AddAsync(user.Value); if (!group.Value.OrganizationId.HasValue) { return; } var organization = await _organizationRepository.GetAsync(group.Value.OrganizationId.Value); if (organization.Value.Groups == null) { organization.Value.Groups = new List <Guid>(); } organization.Value.Groups.Add(@event.GroupId); organization.Value.GroupsCount = organization.Value.Groups.Count; await _organizationRepository.UpdateAsync(organization.Value); await _organizationCache.AddAsync(organization.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(SignedUp @event) { await _handler .Run(async() => { var user = await _userServiceClient.GetAsync <User>(@event.UserId); user.Value.FavoriteRemarks = new HashSet <Guid>(); user.Value.Groups = new HashSet <UserGroup>(); user.Value.Organizations = new HashSet <UserOrganization>(); await _repository.AddAsync(user.Value); await _accountStateService.SetAsync(user.Value.UserId, user.Value.State); await _cache.AddAsync(user.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(AvatarRemoved @event) { await _handler .Run(async() => { var user = await _userRepository.GetByIdAsync(@event.UserId); if (user.HasNoValue) { throw new ServiceException(OperationCodes.UserNotFound, $"Avatar cannot be removed because user: {@event.UserId} does not exist"); } user.Value.AvatarUrl = string.Empty; await _userRepository.EditAsync(user.Value); await _cache.AddAsync(user.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(MemberAddedToOrganization @event) { await _handler .Run(async() => { var organization = await _organizationRepository.GetAsync(@event.OrganizationId); var user = await _userRepository.GetByIdAsync(@event.MemberId); organization.Value.Members.Add(new Member { UserId = user.Value.UserId, Name = user.Value.Name, Role = @event.Role, IsActive = true }); organization.Value.MembersCount++; await _organizationRepository.UpdateAsync(organization.Value); await _organizationCache.AddAsync(organization.Value); var member = organization.Value.Members.First(x => x.UserId == @event.MemberId); if (user.Value.Organizations == null) { user.Value.Organizations = new HashSet <UserOrganization>(); } user.Value.Organizations.Add(new UserOrganization { Id = organization.Value.Id, Name = organization.Value.Name, Role = member.Role, IsActive = member.IsActive }); await _userRepository.EditAsync(user.Value); await _userCache.AddAsync(user.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }
public async Task HandleAsync(MemberAddedToGroup @event) { await _handler .Run(async() => { var group = await _groupRepository.GetAsync(@event.GroupId); var user = await _userRepository.GetByIdAsync(@event.MemberId); group.Value.Members.Add(new Member { UserId = user.Value.UserId, Name = user.Value.Name, Role = @event.Role, IsActive = true }); group.Value.MembersCount++; await _groupRepository.UpdateAsync(group.Value); await _groupCache.AddAsync(group.Value); var member = group.Value.Members.First(x => x.UserId == @event.MemberId); if (user.Value.Groups == null) { user.Value.Groups = new HashSet <UserGroup>(); } user.Value.Groups.Add(new UserGroup { Id = group.Value.Id, Name = group.Value.Name, Role = member.Role, IsActive = member.IsActive }); await _userRepository.EditAsync(user.Value); await _userCache.AddAsync(user.Value); }) .OnError((ex, logger) => { logger.Error(ex, $"Error occured while handling {@event.GetType().Name} event"); }) .ExecuteAsync(); }