Пример #1
0
        public async Task <GetContactsQueryResponse> Handle(GetContactsQuery query, CancellationToken cancellation)
        {
            var user = new User();

            try {
                var data = await _responseCacheService.ManageTokenAsync(query.Token);

                if (data != null)
                {
                    user = JsonConvert.DeserializeObject <User> (data);
                }
            } catch (Exception ex) {
                return(new GetContactsQueryResponse()
                {
                });
            }

            var response = new GetContactsQueryResponse();

            try {
                var cachedValue = await _responseCacheService.GetCachedResponseAsync(user.Email);

                if (cachedValue != null)
                {
                    var cachedResponse = JsonConvert.DeserializeObject <IEnumerable <Contact> > (cachedValue);
                    response.Contacts = cachedResponse;
                    response.Success  = true;
                }
            } catch (Exception ex) {
                throw ex;
            }

            return(response);
        }
Пример #2
0
        public async Task <AddContactCommandResponse> Handle(AddContactCommand command, CancellationToken cancellation)
        {
            var user = new AddUserCommand();

            try {
                var data = await _responseCacheService.ManageTokenAsync(command.Token);

                if (data != null)
                {
                    user = JsonConvert.DeserializeObject <AddUserCommand> (data);
                }
            } catch (Exception ex) {
                return(new AddContactCommandResponse()
                {
                    Message = "Você precisa logar!", Success = false
                });
            }

            var contacts = new List <AddContactCommand> ();

            try {
                var cachedValue = await _responseCacheService.GetCachedResponseAsync(user.Email);

                if (cachedValue != null)
                {
                    contacts = JsonConvert.DeserializeObject <List <AddContactCommand> > (cachedValue);
                }
            } catch (Exception ex) {
                return(new AddContactCommandResponse()
                {
                    Message = "Erro ao adicionar o contato!", Success = false
                });
            }

            contacts.Add(command);

            var expiration = Convert.ToInt32(Environment.GetEnvironmentVariable("DATA_EXPIRATION_SECONDS"));

            expiration = expiration < 1 ? 900 : expiration;

            _responseCacheService.CacheResponseAsync(user.Email, contacts, new TimeSpan(0, 0, expiration)).ConfigureAwait(false);

            return(new AddContactCommandResponse()
            {
                Message = "Usuario cadastrado com sucesso!", Success = true
            });
        }
        public async Task <GetUserQueryResponse> Handle(GetUserQuery query, CancellationToken cancellation)
        {
            var users = new List <User> ();

            try {
                var cachedValue = await _responseCacheService.GetCachedResponseAsync("users");

                if (cachedValue != null)
                {
                    users = JsonConvert.DeserializeObject <List <User> > (cachedValue);
                }
            } catch (Exception ex) {
                return(new GetUserQueryResponse()
                {
                    Message = "Ocorreu um erro inesperado.", Success = false
                });
            }

            var loggedUser = users.FirstOrDefault(x => x.Email.Equals(query.Email) && x.Password.Equals(query.Password));

            if (loggedUser == null)
            {
                return new GetUserQueryResponse()
                       {
                           Message = "Senha ou email invalido", Success = false
                       }
            }
            ;

            try {
                loggedUser.Token = WindowsIdentity.GetCurrent().Token.ToString();
            } catch {
                loggedUser.Token = System.Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(loggedUser.Email + loggedUser.Name));
            }
            loggedUser.Role = "user";

            _responseCacheService.ManageTokenAsync(loggedUser.Token, loggedUser).ConfigureAwait(false);

            return(new GetUserQueryResponse()
            {
                Message = "", Success = true, User = loggedUser
            });;
        }
    }
Пример #4
0
        public async Task <UpdateUserCommandResponse> Handle(UpdateUserCommand command, CancellationToken cancellation)
        {
            var sessionUser = new AddUserCommand();

            try {
                var data = await _responseCacheService.ManageTokenAsync(command.Token);

                if (data != null)
                {
                    sessionUser = JsonConvert.DeserializeObject <AddUserCommand> (data);
                }
            } catch (Exception ex) {
                return(new UpdateUserCommandResponse()
                {
                    Message = "Você precisa logar!", Success = false
                });
            }

            var users = new List <AddUserCommand> ();

            try {
                var cachedValue = await _responseCacheService.GetCachedResponseAsync("users");

                if (cachedValue != null)
                {
                    users = JsonConvert.DeserializeObject <List <AddUserCommand> > (cachedValue);
                }
            } catch (Exception ex) {
                return(new UpdateUserCommandResponse()
                {
                    Message = "Erro ao alterar senha", Success = false
                });
            }

            var user = users.FirstOrDefault(x => x.Email.Equals(command.Email));

            if (user != null)
            {
                if (sessionUser.Email.Equals(user.Email))
                {
                    user.Password = command.Password;
                }
                else
                {
                    return new UpdateUserCommandResponse()
                           {
                               Message = "Alteração não autorizada!", Success = false
                           }
                };
            }
            else
            {
                return(new UpdateUserCommandResponse()
                {
                    Message = "Email não cadastrado", Success = false
                });
            }

            var expiration = Convert.ToInt32(Environment.GetEnvironmentVariable("DATA_EXPIRATION_SECONDS"));

            expiration = expiration < 1 ? 900 : expiration;
            _responseCacheService.CacheResponseAsync("users", users, new System.TimeSpan(0, 0, expiration)).ConfigureAwait(false);

            return(new UpdateUserCommandResponse()
            {
                Message = "Senha alterada com sucesso!", Success = true
            });
        }