Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 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})");
        }
Ejemplo n.º 3
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)}."));
                }
                }
            }
        }
Ejemplo n.º 4
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})");
        }
Ejemplo n.º 5
0
        public override async Task <RemoveRoleClaimResponse> RemoveRoleClaim(RemoveRoleClaimRequest 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))
            {
                throw context.NotFoundRpcException(
                          $"The claim type '{claim.Type}' (value=\"{claim.Value}\") not found in role '{role.Name}'. (roleId=${role.Id})");
            }

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

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

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

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

            throw context.FailedPreconditionRpcException(
                      result,
                      $"Failed to remove the claim type '{claim.Type}' (value=\"{claim.Value}\") from the role '{role.Name}'. (roleId=${role.Id})");
        }
Ejemplo n.º 6
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})");
        }
Ejemplo n.º 7
0
        public override async Task <RemoveUserFromRoleResponse> RemoveUserFromRole(RemoveUserFromRoleRequest 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);

                throw context.NotFoundRpcException($"The role '{existingRole.Name}' already exists. (roleId=${existingRole.Id})");
            }

            var result = await _userService.RemoveFromRoleAsync(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 RemoveUserFromRoleResponse
                {
                    Role = new RoleDto
                    {
                        Id   = role.Id.ToString(),
                        Name = role.Name
                    }
                };

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

            throw context.FailedPreconditionRpcException(result, string.Empty);
        }
Ejemplo n.º 8
0
        public override async Task <FindChallengePayoutResponse> FindChallengePayout(FindChallengePayoutRequest request, ServerCallContext context)
        {
            var challenge = await _challengeQuery.FindChallengeAsync(request.ChallengeId.ParseEntityId <ChallengeId>());

            if (challenge == null)
            {
                throw context.NotFoundRpcException("Challenge not found.");
            }

            var response = new FindChallengePayoutResponse
            {
                Payout = _mapper.Map <ChallengePayoutDto>(challenge)
            };

            return(context.Ok(response));
        }
Ejemplo n.º 9
0
        public override async Task <FindChallengeResponse> FindChallenge(FindChallengeRequest request, ServerCallContext context)
        {
            var challengeId = request.ChallengeId.ParseEntityId <ChallengeId>();

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

            var challenge = await _challengeQuery.FindChallengeAsync(challengeId);

            var response = new FindChallengeResponse
            {
                Challenge = ChallengeProfile.Map(challenge !)
            };

            return(context.Ok(response));
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        public override async Task <FindPlayerGameCredentialResponse> FindPlayerGameCredential(
            FindPlayerGameCredentialRequest request,
            ServerCallContext context
            )
        {
            var httpContext = context.GetHttpContext();

            var userId = httpContext.GetUserId();

            var credential = await _gameCredentialService.FindCredentialAsync(userId, request.Game.ToEnumeration <Game>());

            if (credential == null)
            {
                throw context.NotFoundRpcException("Game credential not found.");
            }

            var response = new FindPlayerGameCredentialResponse
            {
                Credential = _mapper.Map <GameCredentialDto>(credential)
            };

            return(context.Ok(response));
        }
Ejemplo n.º 12
0
        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);
        }
Ejemplo n.º 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);
        }