public async Task <IActionResult> PutTempData([FromRoute] int id, [FromBody] TempData tempData) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != tempData.Id) { return(BadRequest()); } _context.Entry(tempData).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TempDataExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutTemperature(long id, Temperature temperature) { if (id != temperature.Id) { return(BadRequest()); } _context.Entry(temperature).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TemperatureExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Post([FromBody] TemperatureRead tempRead) { if (!ModelState.IsValid) { return(BadRequest()); } // Because binding doesn't seem to work. tempRead.ID = 0; // Because NTP is hard. tempRead.Date = DateTime.Now; _context.Add(tempRead); await _context.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> PostSensor([FromBody] string sensorName) { var device = await GetAuthorizedDevice(); if (device == null) { return(Unauthorized()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var existingSensor = await _temperatureContext.Sensor .FirstOrDefaultAsync(x => x.Name.ToLower().Equals(sensorName.ToLower())); if (existingSensor == null) { var newSensor = new Sensor { Name = sensorName }; var entityEntry = await _temperatureContext.Sensor.AddAsync(newSensor); await _temperatureContext.SaveChangesAsync(); _cachingService.ResetCache("AllSensors"); return(Ok(new SensorDto { Id = entityEntry.Entity.Id, Name = entityEntry.Entity.Name })); } return(Ok(new SensorDto { Id = existingSensor.Id, Name = existingSensor.Name })); }
private async Task <Weather> UpdateWeather(WeatherResponse weatherResponse) { if (weatherResponse == null) { return(null); } var actualWeather = await _context.Weathers .FirstOrDefaultAsync(weather => weather.CityId == weatherResponse.id && weather.Date == DateTime.Now.Date); var weather = new Weather { Id = actualWeather?.Id ?? 0, Date = DateTime.Now.Date, CityId = weatherResponse.id, City = new City { Name = weatherResponse.name, Latitude = weatherResponse.coord.lat, Longitude = weatherResponse.coord.lon, }, Temperature = weatherResponse.main.temp }; if (actualWeather != null) { actualWeather.Temperature = weather.Temperature; _context.Weathers.Update(actualWeather); } else { _context.Weathers.Add(weather); } await _context.SaveChangesAsync(); return(weather); }