Exemple #1
0
        public async Task <int> InsertUserAsync(User user)
        {
            User u = await GetUserByEmailAsync(user.Email);

            if (u != null)
            {
                return(-1);  // If email alreay exists
            }
            user.Id = Guid.NewGuid();

            Context.Users.Add(user);

            return(await Context.SaveChangesAsync());
        }
Exemple #2
0
        public async Task <int> InsertRunAsync(Guid userId, RunInput runInput)
        {
            if (runInput.Distance <= 0)
            {
                throw new ArgumentOutOfRangeException("Distance must be greater than zero");
            }

            if (runInput.Time <= 0)
            {
                throw new ArgumentOutOfRangeException("Time must be greater than zero");
            }


            DateTime dd = runInput.Date.AddDays(-1);
            string   dt = new DateTimeOffset(dd, TimeSpan.Zero).ToUnixTimeSeconds().ToString();


            string weather = "";
            string weatherReqUri;
            HttpResponseMessage response;

            using (HttpClient httpClient = new HttpClient())
            {
                weatherReqUri = Configuration["WeatherByCityReq"];
                weatherReqUri = weatherReqUri.Replace("{cityCountry}", runInput.Location);
                response      = await httpClient.GetAsync(weatherReqUri);

                string s = await response.Content.ReadAsStringAsync();

                s = s.Substring(16);
                int    cp  = s.IndexOf(',');
                string lon = s.Substring(0, cp);
                s  = s.Substring(lon.Length + 7);
                cp = s.IndexOf('}');
                string lat = s.Substring(0, cp);

                weatherReqUri = Configuration["HistoricalWeatherReq"];
                weatherReqUri =
                    weatherReqUri
                    .Replace("{lat}", lat)
                    .Replace("{lon}", lon)
                    .Replace("{time}", dt);
                response = await httpClient.GetAsync(weatherReqUri);

                weather = await response.Content.ReadAsStringAsync();
            }

            if (weather.Length > 1000)          // Limitazione alla lunghezza della colonna nel db
            {
                weather = weather.Substring(0, 1000);
            }

            Run run =
                new Run()
            {
                Id       = Guid.NewGuid(),
                UserId   = userId,
                Date     = runInput.Date,
                Distance = runInput.Distance,
                Time     = runInput.Time,
                Location = runInput.Location,
                Weather  = weather
            };

            Context.Runs.Add(run);
            return(await Context.SaveChangesAsync());
        }