Beispiel #1
0
 public WeatherDataViewModel(WeatherData.Models.WeatherData weatherData, bool includeReferences = true)
 {
     // wrap WeatherData repository object into WeatherDataViewModel object
     WeatherDataId            = weatherData.WeatherDataId;
     Temperature              = weatherData.Temperature;
     Weather                  = weatherData.Weather;
     DateTime                 = weatherData.DateTime;
     Humidity                 = weatherData.Humidity;
     WindSpeed                = weatherData.WindSpeed;
     PrecipitationProbability = weatherData.PrecipitationProbability;
     AddressId                = weatherData.AddressId;
     if (includeReferences && weatherData.Address != null)
     {
         Address = new AddressViewModel(weatherData.Address);
     }
 }
Beispiel #2
0
        public async Task <IActionResult> CreateWeatherData([FromBody] WeatherDataViewModel vmWeatherData)
        {
            if (vmWeatherData == null)
            {
                return(BadRequest(new { error = "No Weather Data object in request body." }));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try {
                // create new WeatherData
                var newWeatherData = new WeatherData.Models.WeatherData
                {
                    Temperature = vmWeatherData.Temperature,
                    Weather     = vmWeatherData.Weather,
                    Humidity    = vmWeatherData.Humidity,
                    WindSpeed   = vmWeatherData.WindSpeed,
                    PrecipitationProbability = vmWeatherData.PrecipitationProbability,
                    DateTime  = DateTime.Now,
                    AddressId = vmWeatherData.AddressId // if Address doesn't exists it throws error
                };

                // Add new WeatherData to DBContext and Save Changes
                await _unitOfWork.WeatherDatas.AddAsync(newWeatherData);

                _unitOfWork.Complete();

                vmWeatherData.WeatherDataId = newWeatherData.WeatherDataId;
                return(CreatedAtAction(nameof(GetWeatherData), new { weatherDataId = vmWeatherData.WeatherDataId }, vmWeatherData));
            } catch (Exception e)
            {
                return(BadRequest(new { error = e.Message }));
            }
        }