public async Task <BaseResponse> Handle(CreateRentCommandRequest request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"[Begin] - Creating rent : { JsonSerializer.Serialize(request) }");

            try
            {
                var validationResult = request.Validate();

                if (!validationResult.IsValid)
                {
                    var validationResultErrors = ValidationResultHelper.GetValidationResultErrors(validationResult);

                    _logger.LogInformation($"[Error] - Request not valid: { validationResultErrors }");

                    return(new BaseResponse(false, "Ocorreu um problema ao criar o empréstimo!", HttpStatusCode.BadRequest, validationResultErrors));
                }

                request.SetGames(await _repository.GetGamesFromIds(request.GameIds));

                if (!_rentService.CheckIfGamesAreValid(request.GetGames()))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Um ou mais jogos não estão disponíveis para empréstimo!", HttpStatusCode.BadRequest, request));
                }

                if (!await _rentService.CheckIfClientExists(request.ClientId))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Cliente não encontrado na base dados!", HttpStatusCode.NotFound, request));
                }

                request.SetClient(await _repository.GetClientById(request.ClientId));

                var rent = new Domain.Aggregates.Rent(request.EndDate);

                rent.AddGamesRent(request.GetGames());
                rent.SetClient(request.GetClient());

                await _repository.Create(rent);

                _logger.LogInformation($"[End] - Rent successfully created: { JsonSerializer.Serialize(rent) }");

                return(new BaseResponse(true, "Empréstimo criado com sucesso!", HttpStatusCode.OK, rent));
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"[Error] - An error occurred while creating the rent: { JsonSerializer.Serialize(ex) }");

                return(new BaseResponse(false, "Ocorreu um problema ao criar o empréstimo!", HttpStatusCode.InternalServerError, ex.InnerException.ToString()));
            }
        }
Esempio n. 2
0
        public async Task <BaseResponse> Handle(UpdateClientCommandRequest request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"[Begin] - Updating client : { JsonSerializer.Serialize(request) }");

            try
            {
                var validationResult = request.Validate();

                if (!validationResult.IsValid)
                {
                    var validationResultErrors = ValidationResultHelper.GetValidationResultErrors(validationResult);

                    _logger.LogInformation($"[Error] - Request not valid: { validationResultErrors }");

                    return(new BaseResponse(false, "Ocorreu um problema ao atualizar o cliente!", HttpStatusCode.BadRequest, validationResultErrors));
                }

                if (await _clientService.CheckIfClientExists(request.Id))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Cliente não existe na base de dados!", HttpStatusCode.NotFound, request));
                }

                if (!await _clientService.CheckIfClientCpfIsUnique(request.Cpf, request.Id))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Já existe um cliente cadastrado com o mesmo cpf!", HttpStatusCode.BadRequest, request));
                }

                var client = new Domain.Entities.Client(request.Id, request.FirstName, request.LastName, request.Cpf, request.Email, request.Username, request.Password, request.Role, request.IsActive);

                await _repository.Update(client);

                _logger.LogInformation($"[End] - Client successfully updated: { JsonSerializer.Serialize(client) }");

                return(new BaseResponse(true, "Cliente atualizado com sucesso!", HttpStatusCode.OK, client));
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"[Error] - An error occurred while updating the client: { JsonSerializer.Serialize(ex) }");

                return(new BaseResponse(false, "Ocorreu um problema ao atualizar o cliente!", HttpStatusCode.InternalServerError, ex.InnerException.ToString()));
            }
        }
        public async Task <BaseResponse> Handle(UpdateGameCommandRequest request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"[Begin] - Updating game : { JsonSerializer.Serialize(request) }");

            try
            {
                var validationResult = request.Validate();

                if (!validationResult.IsValid)
                {
                    var validationResultErrors = ValidationResultHelper.GetValidationResultErrors(validationResult);

                    _logger.LogInformation($"[Error] - Request not valid: { validationResultErrors }");

                    return(new BaseResponse(false, "Ocorreu um problema ao atualizar o jogo!", HttpStatusCode.BadRequest, validationResultErrors));
                }

                if (await _gameService.CheckIfGameExists(request.Id))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Jogo não existe na base de dados!", HttpStatusCode.NotFound, request));
                }

                if (!await _gameService.CheckIfGameNameIsUnique(request.Name, request.Id))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Já existe um jogo cadastrado com o mesmo nome!", HttpStatusCode.BadRequest, request));
                }

                var game = new Domain.Entities.Game(request.Id, request.Name, request.Genre, request.Synopsis, request.Platform, request.LaunchDate, request.IsAvailable, request.IsActive);

                await _repository.Update(game);

                _logger.LogInformation($"[End] - Game successfully updated: { JsonSerializer.Serialize(game) }");

                return(new BaseResponse(true, "Jogo atualizado com sucesso!", HttpStatusCode.OK, game));
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"[Error] - An error occurred while updating the game: { JsonSerializer.Serialize(ex) }");

                return(new BaseResponse(false, "Ocorreu um problema ao atualizar o jogo!", HttpStatusCode.InternalServerError, ex.InnerException.ToString()));
            }
        }
Esempio n. 4
0
        public async Task <BaseResponse> Handle(LoginCommandRequest request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"[Begin] - Authenticating user : { JsonSerializer.Serialize(request) }");

            try
            {
                var validationResult = request.Validate();

                if (!validationResult.IsValid)
                {
                    var validationResultErrors = ValidationResultHelper.GetValidationResultErrors(validationResult);

                    _logger.LogInformation($"[Error] - Request not valid: { validationResultErrors }");

                    return(new BaseResponse(false, "Ocorreu um problema ao autenticar o usuário!", HttpStatusCode.BadRequest, validationResultErrors));
                }

                var user = await _repository.GetByUsernameAndPassword(request.Username, request.Password);

                if (user == null)
                {
                    _logger.LogInformation($"[Error] - Request not valid: { JsonSerializer.Serialize(request) }");

                    return(new BaseResponse(false, "Usuário não encontrado na base de dados!", HttpStatusCode.NotFound, request));
                }

                var tokenResponse = await _tokenService.GenerateToken(user);

                if (!tokenResponse.Success)
                {
                    return(new BaseResponse(false, "Ocorreu um problema ao gerar o token para o usuário!", tokenResponse.Data));
                }

                return(new BaseResponse(true, "Usuário autenticado com sucesso!", HttpStatusCode.OK, new { request.Username, Token = tokenResponse.Data }));
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"[Error] - An error occurred while authenticating the user: { JsonSerializer.Serialize(ex) }");

                return(new BaseResponse(false, "Ocorreu um problema ao autenticar o usuário!", HttpStatusCode.InternalServerError, ex.InnerException.ToString()));
            }
        }
Esempio n. 5
0
        public async Task <BaseResponse> Handle(FinishRentCommandRequest request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"[Begin] - Finishing rent : { JsonSerializer.Serialize(request) }");

            try
            {
                var validationResult = request.Validate();

                if (!validationResult.IsValid)
                {
                    var validationResultErrors = ValidationResultHelper.GetValidationResultErrors(validationResult);

                    _logger.LogInformation($"[Error] - Request not valid: { validationResultErrors }");

                    return(new BaseResponse(false, "Ocorreu um problema ao encerrar o empréstimo!", HttpStatusCode.BadRequest, validationResultErrors));
                }

                if (!await _rentService.CheckIfRentExists(request.Id))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Empréstimo não existe na base de dados!", HttpStatusCode.NotFound, request));
                }

                await _repository.Finish(request.Id);

                _logger.LogInformation($"[End] - Rent successfully finished: { JsonSerializer.Serialize(request) }");

                return(new BaseResponse(true, "Empréstimo encerrado com sucesso!", HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"[Error] - An error occurred while finishing the rent: { JsonSerializer.Serialize(ex) }");

                return(new BaseResponse(false, "Ocorreu um problema ao encerrar o empréstimo!", HttpStatusCode.InternalServerError, ex.InnerException.ToString()));
            }
        }
        public async Task <BaseResponse> Handle(DeleteGameCommandRequest request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"[Begin] - Deleting game : { JsonSerializer.Serialize(request) }");

            try
            {
                var validationResult = request.Validate();

                if (!validationResult.IsValid)
                {
                    var validationResultErrors = ValidationResultHelper.GetValidationResultErrors(validationResult);

                    _logger.LogInformation($"[Error] - Request not valid: { validationResultErrors }");

                    return(new BaseResponse(false, "Ocorreu um problema ao deletar o jogo!", HttpStatusCode.BadRequest, validationResultErrors));
                }

                if (await _gameService.CheckIfGameExists(request.Id))
                {
                    _logger.LogInformation($"[Error] - Request not valid: { request }");

                    return(new BaseResponse(false, "Jogo não existe na base de dados!", HttpStatusCode.NotFound, request));
                }

                await _repository.Delete(request.Id);

                _logger.LogInformation($"[End] - Game successfully deleted: { JsonSerializer.Serialize(request) }");

                return(new BaseResponse(true, "Jogo deletado com sucesso!", HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"[Error] - An error occurred while deleting the game: { JsonSerializer.Serialize(ex) }");

                return(new BaseResponse(false, "Ocorreu um problema ao deletar o jogo!", HttpStatusCode.InternalServerError, ex.InnerException.ToString()));
            }
        }