public async Task <IActionResult> Register([FromForm] DeviceViewModel postData) { if (!this.ModelState.IsValid) { return(BadRequest()); } Device device = new Device { DeviceId = Guid.NewGuid(), DeviceName = postData.DeviceName, StudentNumber = postData.StudentNumber, Latitude = postData.Latitude, Longitude = postData.Longitude }; _context.Device.Add(device); await _context.SaveChangesAsync(); return(RedirectToAction("Success", new { deviceId = device.DeviceId })); }
public async Task <IActionResult> PostWeatherUpdate([FromBody] WeatherUpdate weatherUpdate) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!_context.Device.Any(d => d.DeviceId == weatherUpdate.DeviceId)) { return(BadRequest("Device does not exist")); } weatherUpdate.TimeStamp = DateTime.UtcNow; weatherUpdate.WeatherUpdateId = Guid.NewGuid(); weatherUpdate.Humidity = double.IsNaN(weatherUpdate.Humidity) ? 0 : weatherUpdate.Humidity; weatherUpdate.TemperatureC = double.IsNaN(weatherUpdate.TemperatureC) ? 0 : weatherUpdate.TemperatureC; weatherUpdate.Windspeed = double.IsNaN(weatherUpdate.Windspeed) ? 0 : weatherUpdate.Windspeed; _context.WeatherUpdate.Add(weatherUpdate); await _context.SaveChangesAsync(); return(CreatedAtAction("GetWeatherUpdate", new { id = weatherUpdate.WeatherUpdateId }, weatherUpdate)); }