Exemple #1
0
        public async Task <Response> Execute(Request request)
        {
            Validate(request);
            var user = await _usersRepository.GetByCpf(request.Cpf);

            if (user == null)
            {
                _notifications.Add($"O usuário com o CPF {request.Cpf} não foi econtrado na base de dados!", nameof(request.Cpf));
            }

            if (!_notifications.IsValid())
            {
                return new Response()
                       {
                           Errors = _notifications.Notifications
                       }
            }
            ;

            var token = await _tokenService.GenerateToken(user);

            return(new Response()
            {
                Token = token
            });
        }
        public async Task <ActionResult <TResponse> > Execute <TRequest, TResponse>(ICommand <TRequest, TResponse> command, TRequest request) where TRequest : class where TResponse : class
        {
            try
            {
                var result = (dynamic)await command.Execute(request);

                if (result.Errors != null && result.Errors.Count > 0)
                {
                    return(StatusCode(400, result));
                }
                await _unitOfWork.Commit();

                return(result);
            }
            catch (Exception e)
            {
                //todo: rotina de log
                Console.WriteLine(e);

                await _unitOfWork.Rollback();

                _notifications.Add("Ocorreu um erro inesperado");

                return(StatusCode(500, (dynamic) new
                {
                    Errors = _notifications.Notifications
                }));
            }
        }
        public void Should_Be_Adding_A_Notification()
        {
            var notificationsCount = _notifications.Notifications.Count;

            _notifications.Add("Test", "MyKey");

            var notificationByKey = _notifications.Notifications.FirstOrDefault(x => x.Key == "MyKey");

            Assert.AreEqual(notificationsCount + 1, _notifications.Notifications.Count);

            Assert.IsNotNull(notificationByKey);
        }
Exemple #4
0
        public async Task <Response> Execute(Request request)
        {
            request.Treatment();

            Validate(request);

            var user = await _usersRepository.GetByCpf(request.Cpf);

            if (user != null)
            {
                _notifications.Add($"Já existe um usuário com o CPF {request.Cpf}.", nameof(Request.Cpf));
            }

            if (!_notifications.IsValid())
            {
                return(new Response()
                {
                    Errors = _notifications.Notifications
                });
            }

            user = request.ToUser();

            await _usersRepository.Add(user);

            var token = await _tokenService.GenerateToken(user);

            return(new Response()
            {
                User = UserDto.From(user),
                Token = token,
                Errors = _notifications.Notifications
            });
        }