public CityWeatherResponse GetByCity(string cityName)
        {
            try
            {
                var cityWeatherResponse = new CityWeatherResponse();
                var client  = new RestClient(_openWeatherMapConfig.Value.BaseEndpoint);
                var request = new RestRequest("")
                              .AddParameter("q", cityName)
                              .AddParameter("appid", _openWeatherMapConfig.Value.Token)
                              .AddParameter("units", UNIT);

                var response = client.Get(request);
                if (!response.IsSuccessful)
                {
                    _notificator.Handle(CityWeatherConstants.CITY_NOT_FOUND);
                    _logger.LogWarning(CityWeatherLogConstants.CITY_NOT_FOUND, cityName);

                    return(null);
                }

                var jsonAsString = response.Content;
                var jsonObject   = JObject.Parse(jsonAsString)["main"];
                cityWeatherResponse = jsonObject.ToObject <CityWeatherResponse>();

                return(cityWeatherResponse);
            }
            catch (Exception error)
            {
                _notificator.Handle(CityWeatherConstants.WEATHER_CLIENT_ERROR);
                _logger.LogError(error, error.Message);

                return(null);
            }
        }
        public async Task AddUserAsync(RegisterUserCommand command)
        {
            var user = new UserEntity(command.Fullname, command.Username);

            user.SetPasswordHashed(HashPassword(user, command.Password));

            if (!user.IsValid())
            {
                _logger.LogInformation(UserLogConstants.ADD_USER_ERROR);
                return;
            }

            await _userRepository.AddUserAsync(user);

            _notificator.Handle(UserConstants.ADD_SUCCESS, ENotificationType.Success);
        }
        public async Task <IEnumerable <CityRequestStatisticsResponse> > GetRequestStatisticsAsync()
        {
            try
            {
                var citiesRequests = await _cityRequestRepository.GetAsync();

                if (!citiesRequests.Any())
                {
                    _notificator.Handle(WeatherPlaylistConstants.NOT_FOUND);
                    _logger.LogInformation(WeatherPlaylistLogConstants.NOT_FOUND);

                    return(default);
예제 #4
0
        public async Task <IEnumerable <string> > GetPlaylistTracksAsync(string genre)
        {
            try
            {
                var playlistSearch = await SearchPlaylistByGenre(genre);

                if (playlistSearch is null)
                {
                    _notificator.Handle(PlaylistConstants.GENRE_NOT_FOUND, ENotificationType.NotFound);
                    _logger.LogInformation(PlaylistLogConstants.GENRE_NOT_FOUND, genre);

                    return(default);
예제 #5
0
 protected void NotificarErro(string mensagem)
 {
     _notificator.Handle(new Notification(mensagem));
 }
예제 #6
0
 protected void NotifyError(string message)
 {
     _notificator.Handle(new Notification(message));
 }
예제 #7
0
 protected virtual void Notify(NotificationType type, string property = "", string message = "")
 {
     Notificator.Handle(new Notification(type, property, message));
 }
예제 #8
0
        public Token GenerateAccessToken(User user, bool resetRefreshTokenExpiresIn = true)
        {
            var now = DateTime.UtcNow;

            var tokenExpiresIn = now.Add(_jwt.TokenLifetime);
            var securityKey    = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_jwt.SecretKey));

            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.Username),
                new Claim(ClaimTypes.Email, user.Email),
                new Claim(ClaimTypes.GivenName, user.FirstName)
            };

            foreach (var role in user.Roles)
            {
                claims.Add(new Claim(ClaimTypes.Role, role.Name));
            }

            var accessTokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = tokenExpiresIn,
                SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature)
            };

            var accessTokenHandler  = new JwtSecurityTokenHandler();
            var accessTokenSecurity = accessTokenHandler.CreateToken(accessTokenDescriptor);
            var accessToken         = accessTokenHandler.WriteToken(accessTokenSecurity);

            try
            {
                EntityEntry <Token> token = null;

                var refreshToken = System.Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                var userToken    = _context.Tokens.FirstOrDefault(t => t.UserId == user.Id);

                if (userToken != null)
                {
                    userToken.AccessToken         = accessToken;
                    userToken.TokenType           = _jwt.TokenType;
                    userToken.AccessTokenLifetime = (int)_jwt.TokenLifetime.TotalSeconds;
                    userToken.RefreshToken        = refreshToken;

                    if (resetRefreshTokenExpiresIn)
                    {
                        userToken.RefreshTokenExpiresIn = now.Add(_jwt.RefreshLifetime);
                    }

                    token = _context.Tokens.Update(userToken);
                }
                else
                {
                    token = _context.Tokens.Add(new Token
                    {
                        UserId                = user.Id,
                        TokenType             = _jwt.TokenType,
                        AccessToken           = accessToken,
                        AccessTokenLifetime   = (int)_jwt.TokenLifetime.TotalSeconds,
                        RefreshToken          = refreshToken,
                        RefreshTokenExpiresIn = now.Add(_jwt.RefreshLifetime)
                    });
                }

                _context.SaveChanges();

                return(token.Entity);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                _notificator.Handle(new Notification(NotificationType.ERROR, string.Empty,
                                                     "An unexpected error occurred while attempting to login, please try again."));

                return(null);
            }
        }
예제 #9
0
 public void Notificate(string error)
 {
     _notificator.Handle(new Notification(error));
 }
예제 #10
0
 protected void NotifyError(string error)
 {
     _notificator.Handle(new Notification(error));
 }
예제 #11
0
 private void AddError(string property, string message)
 {
     Notificator.Handle(new Notification(NotificationType.ERROR, property, message));
 }