public override async Task <SynchronizeChallengeResponse> SynchronizeChallenge(SynchronizeChallengeRequest request, ServerCallContext context)
        {
            var challengeId = request.ChallengeId.ParseEntityId <ChallengeId>();

            if (!await _challengeService.ChallengeExistsAsync(challengeId))
            {
                throw context.NotFoundRpcException("Challenge not found.");
            }

            var challenge = await _challengeService.FindChallengeAsync(challengeId);

            var result = await _challengeService.SynchronizeChallengeAsync(challenge, new UtcNowDateTimeProvider());

            if (result.IsValid)
            {
                var response = new SynchronizeChallengeResponse
                {
                    Challenge = ChallengeProfile.Map(result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }
Beispiel #2
0
        public override async Task <DeleteRoleResponse> DeleteRole(DeleteRoleRequest request, ServerCallContext context)
        {
            if (!await _roleService.RoleExistsAsync(request.RoleName))
            {
                throw context.NotFoundRpcException($"The role '{request.RoleName}' wasn't found.");
            }

            var role = await _roleService.FindByNameAsync(request.RoleName);

            var result = await _roleService.DeleteAsync(role);

            if (result.Succeeded)
            {
                var detail = $"The role '{role.Name}' as been deleted. (roleId=${role.Id})";

                var response = new DeleteRoleResponse
                {
                    Role = new RoleDto
                    {
                        Id   = role.Id.ToString(),
                        Name = role.Name
                    }
                };

                return(context.Ok(response, detail));
            }

            throw context.FailedPreconditionRpcException(result, $"Failed to delete the role '{role.Name}'. (roleId=${role.Id})");
        }
        public override async Task <CreateChallengeResponse> CreateChallenge(CreateChallengeRequest request, ServerCallContext context)
        {
            // TODO: Validation...

            var result = await _challengeService.CreateChallengeAsync(
                new ChallengeName(request.Name),
                request.Game.ToEnumeration <Game>(),
                new BestOf(request.BestOf),
                new Entries(request.Entries),
                new ChallengeDuration(TimeSpan.FromDays(request.Duration)),
                new UtcNowDateTimeProvider(),
                new Scoring(request.Scoring.Items.OrderBy(item => item.Order).ToDictionary(item => item.StatName, item => item.StatWeighting)));

            if (result.IsValid)
            {
                var response = new CreateChallengeResponse
                {
                    Challenge = ChallengeProfile.Map(result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }
Beispiel #4
0
        public override async Task <CreateTransactionResponse> CreateTransaction(CreateTransactionRequest request, ServerCallContext context)
        {
            var httpContext = context.GetHttpContext();

            var userId = httpContext.GetUserId();

            var account = await _accountService.FindAccountOrNullAsync(userId);

            if (account == null)
            {
                throw context.NotFoundRpcException("User account not found.");
            }

            var result = await CreateTransactionByOneofCaseAsync();

            if (result.IsValid)
            {
                var response = new CreateTransactionResponse
                {
                    Transaction = _mapper.Map <TransactionDto>(result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);

            async Task <DomainValidationResult <ITransaction> > CreateTransactionByOneofCaseAsync()
            {
                switch (request.TransactionCase)
                {
                case CreateTransactionRequest.TransactionOneofCase.Bundle:
                {
                    return(await _accountService.CreateTransactionAsync(account !, request.Bundle, new TransactionMetadata(request.Metadata)));
                }

                case CreateTransactionRequest.TransactionOneofCase.Custom:
                {
                    return(await _accountService.CreateTransactionAsync(
                               account !,
                               request.Custom.Currency.Amount,
                               request.Custom.Currency.Type.ToEnumeration <CurrencyType>(),
                               request.Custom.Type.ToEnumeration <TransactionType>(),
                               new TransactionMetadata(request.Metadata)));
                }

                default:
                {
                    throw context.RpcException(
                              new Status(
                                  StatusCode.InvalidArgument,
                                  $"The case ({request.TransactionCase}) is not supported for {nameof(this.CreateTransaction)}."));
                }
                }
            }
        }
Beispiel #5
0
        public override async Task <AddUserClaimResponse> AddUserClaim(AddUserClaimRequest request, ServerCallContext context)
        {
            var user = await _userService.FindByIdAsync(request.UserId);

            if (user == null)
            {
                throw context.NotFoundRpcException($"The user '{request.UserId}' wasn't found.");
            }

            var claims = await _userService.GetClaimsAsync(user);

            var claim = new Claim(request.Claim.Type, request.Claim.Value);

            if (claims.Any(x => x.Type == claim.Type && x.Value == claim.Value))
            {
                var detail = $"The claim type '{claim.Type}' (value=\"{claim.Value}\") already exists in user '{user.Email}'. (userId=${user.Id})";

                var response = new AddUserClaimResponse
                {
                    Claim = new UserClaimDto
                    {
                        Type  = claim.Type,
                        Value = claim.Value
                    }
                };

                return(context.AlreadyExists(response, detail));
            }

            var result = await _userService.AddClaimAsync(user, claim);

            if (result.Succeeded)
            {
                var detail = $"The claim type '{claim.Type}' (value=\"{claim.Value}\") as been added to the user '{user.Email}'. (userId=${user.Id})";

                var response = new AddUserClaimResponse
                {
                    Claim = new UserClaimDto
                    {
                        Type  = claim.Type,
                        Value = claim.Value
                    }
                };

                return(context.Ok(response, detail));
            }

            throw context.FailedPreconditionRpcException(
                      result,
                      $"Failed to add the claim type '{claim.Type}' (value=\"{claim.Value}\") to the user '{user.Email}'. (userId=${user.Id})");
        }
Beispiel #6
0
        public override async Task <AddRoleClaimResponse> AddRoleClaim(AddRoleClaimRequest request, ServerCallContext context)
        {
            if (!await _roleService.RoleExistsAsync(request.RoleName))
            {
                throw context.NotFoundRpcException($"The role '{request.RoleName}' wasn't found.");
            }

            var role = await _roleService.FindByNameAsync(request.RoleName);

            var claims = await _roleService.GetClaimsAsync(role);

            var claim = new Claim(request.Claim.Type, request.Claim.Value);

            if (claims.Any(x => x.Type == claim.Type && x.Value == claim.Value))
            {
                var detail = $"The claim type '{claim.Type}' (value=\"{claim.Value}\") already exists in role '{role.Name}'. (roleId=${role.Id})";

                var response = new AddRoleClaimResponse
                {
                    Claim = new RoleClaimDto
                    {
                        Type  = claim.Type,
                        Value = claim.Value
                    }
                };

                return(context.AlreadyExists(response, detail));
            }

            var result = await _roleService.AddClaimAsync(role, claim);

            if (result.Succeeded)
            {
                var detail = $"The claim type '{claim.Type}' (value=\"{claim.Value}\") as been added to the role '{role.Name}'. (roleId=${role.Id})";

                var response = new AddRoleClaimResponse
                {
                    Claim = new RoleClaimDto
                    {
                        Type  = claim.Type,
                        Value = claim.Value
                    }
                };

                return(context.Ok(response, detail));
            }

            throw context.FailedPreconditionRpcException(
                      result,
                      $"Failed to add the claim type '{claim.Type}' (value=\"{claim.Value}\") to the role '{role.Name}'. (roleId=${role.Id})");
        }
Beispiel #7
0
        public override async Task <AddUserToRoleResponse> AddUserToRole(AddUserToRoleRequest request, ServerCallContext context)
        {
            var user = await _userService.FindByIdAsync(request.UserId);

            if (user == null)
            {
                throw context.NotFoundRpcException($"The user '{request.UserId}' wasn't found.");
            }

            if (await _userService.IsInRoleAsync(user, request.RoleName))
            {
                var existingRole = await _roleService.FindByNameAsync(request.RoleName);

                var detail = $"The role '{existingRole.Name}' already exists. (roleId=${existingRole.Id})";

                var response = new AddUserToRoleResponse
                {
                    Role = new RoleDto
                    {
                        Id   = existingRole.Id.ToString(),
                        Name = existingRole.Name
                    }
                };

                return(context.AlreadyExists(response, detail));
            }

            var result = await _userService.AddToRoleAsync(user, request.RoleName);

            if (result.Succeeded)
            {
                var role = await _roleService.FindByNameAsync(request.RoleName);

                var detail = $"The role '{role.Name}' as been created. (roleId=${role.Id})";

                var response = new AddUserToRoleResponse
                {
                    Role = new RoleDto
                    {
                        Id   = role.Id.ToString(),
                        Name = role.Name
                    }
                };

                return(context.Ok(response, detail));
            }

            throw context.FailedPreconditionRpcException(result, string.Empty);
        }
Beispiel #8
0
        public override async Task <CreateRoleResponse> CreateRole(CreateRoleRequest request, ServerCallContext context)
        {
            if (await _roleService.RoleExistsAsync(request.RoleName))
            {
                var existingRole = await _roleService.FindByNameAsync(request.RoleName);

                var detail = $"The role '{existingRole.Name}' already exists. (roleId=${existingRole.Id})";

                var response = new CreateRoleResponse
                {
                    Role = new RoleDto
                    {
                        Id   = existingRole.Id.ToString(),
                        Name = existingRole.Name
                    }
                };

                return(context.AlreadyExists(response, detail));
            }

            var role = new Role
            {
                Id   = Guid.NewGuid(),
                Name = request.RoleName
            };

            var result = await _roleService.CreateAsync(role);

            if (result.Succeeded)
            {
                var detail = $"The role '{role.Name}' as been created. (roleId=${role.Id})";

                var response = new CreateRoleResponse
                {
                    Role = new RoleDto
                    {
                        Id   = role.Id.ToString(),
                        Name = role.Name
                    }
                };

                return(context.Ok(response, detail));
            }

            throw context.FailedPreconditionRpcException(result, $"Failed to create the role '{role.Name}'. (roleId=${role.Id})");
        }
Beispiel #9
0
        public override async Task <ReplaceUserClaimResponse> ReplaceUserClaim(ReplaceUserClaimRequest request, ServerCallContext context)
        {
            var user = await _userService.FindByIdAsync(request.UserId);

            if (user == null)
            {
                throw context.NotFoundRpcException($"The user '{request.UserId}' wasn't found.");
            }

            var claims = await _userService.GetClaimsAsync(user);

            var claim = claims.SingleOrDefault(x => x.Type == request.Claim.Type);

            if (claim == null)
            {
                throw context.NotFoundRpcException(
                          $"The claim type '{request.Claim.Type}' (value=\"{request.Claim.Value}\") not found in user '{user.Email}'. (userId=${user.Id})");
            }

            var newClaim = new Claim(request.Claim.Type, request.Claim.Value);

            var result = await _userService.ReplaceClaimAsync(user, claim, newClaim);

            if (result.Succeeded)
            {
                var detail = $"The claim type '{newClaim.Type}' (value=\"{newClaim.Value}\") as been replaced for the user '{user.Email}'. (userId=${user.Id})";

                var response = new ReplaceUserClaimResponse
                {
                    Claim = new UserClaimDto
                    {
                        Type  = newClaim.Type,
                        Value = newClaim.Value
                    }
                };

                return(context.Ok(response, detail));
            }

            throw context.FailedPreconditionRpcException(
                      result,
                      $"Failed to replace the claim type '{newClaim.Type}' (value=\"{newClaim.Value}\") from the user '{user.Email}'. (userId=${user.Id})");
        }
Beispiel #10
0
        public override async Task <CreateChallengePayoutResponse> CreateChallengePayout(CreateChallengePayoutRequest request, ServerCallContext context)
        {
            var result = await _challengeService.CreateChallengeAsync(
                request.ChallengeId.ParseEntityId <ChallengeId>(),
                new ChallengePayoutEntries(request.PayoutEntries),
                new EntryFee(request.EntryFee.Amount, request.EntryFee.Type.ToEnumeration <CurrencyType>()));

            if (result.IsValid)
            {
                var response = new CreateChallengePayoutResponse
                {
                    Payout = _mapper.Map <ChallengePayoutDto>(result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }
        public override async Task <SnapshotChallengeParticipantResponse> SnapshotChallengeParticipant(
            SnapshotChallengeParticipantRequest request,
            ServerCallContext context
            )
        {
            var challengeId = request.ChallengeId.ParseEntityId <ChallengeId>();

            if (!await _challengeService.ChallengeExistsAsync(challengeId))
            {
                throw context.NotFoundRpcException("Challenge not found.");
            }

            var challenge = await _challengeService.FindChallengeAsync(challengeId);

            var result = await _challengeService.SnapshotChallengeParticipantAsync(
                challenge,
                request.GamePlayerId.ParseStringId <PlayerId>(),
                new UtcNowDateTimeProvider(),
                scoring => request.Matches.Select(
                    match => new Match(
                        new GameUuid(match.GameUuid),
                        new DateTimeProvider(match.GameCreatedAt.ToDateTime()),
                        match.GameDuration.ToTimeSpan(),
                        scoring.Map(match.Stats),
                        new UtcNowDateTimeProvider()))
                .ToImmutableHashSet());

            if (result.IsValid)
            {
                var response = new SnapshotChallengeParticipantResponse
                {
                    Participant = ChallengeProfile.Map(challenge, result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }
        public override async Task <RegisterChallengeParticipantResponse> RegisterChallengeParticipant(
            RegisterChallengeParticipantRequest request,
            ServerCallContext context
            )
        {
            var httpContext = context.GetHttpContext();

            var userId = httpContext.GetUserId();

            var challengeId = request.ChallengeId.ParseEntityId <ChallengeId>();

            if (!await _challengeService.ChallengeExistsAsync(challengeId))
            {
                throw context.NotFoundRpcException("Challenge not found.");
            }

            var challenge = await _challengeService.FindChallengeAsync(challengeId);

            var result = await _challengeService.RegisterChallengeParticipantAsync(
                challenge,
                userId,
                request.ParticipantId.ParseEntityId <ParticipantId>(),
                request.GamePlayerId.ParseStringId <PlayerId>(),
                new UtcNowDateTimeProvider());

            if (result.IsValid)
            {
                var response = new RegisterChallengeParticipantResponse
                {
                    Participant = ChallengeProfile.Map(challenge, result.Response)
                };

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }
Beispiel #13
0
        public override async Task <DeleteTransactionResponse> DeleteTransaction(DeleteTransactionRequest request, ServerCallContext context)
        {
            var httpContext = context.GetHttpContext();

            var userId = httpContext.GetUserId();

            var account = await _accountService.FindAccountOrNullAsync(userId);

            if (account == null)
            {
                throw context.NotFoundRpcException("User account not found.");
            }

            var result = await _accountService.DeleteTransactionAsync(account !, request.TransactionId.ParseEntityId <TransactionId>());

            if (result.IsValid)
            {
                var response = new DeleteTransactionResponse();

                return(context.Ok(response));
            }

            throw context.FailedPreconditionRpcException(result);
        }