usingSystem;usingSystem.Web.Http;usingPollutometerWebApi.Models;namespacePollutometerWebApi.Controllers{publicclassReadingsController:ApiController{publicReadingsController(){}publicIHttpActionResultGetAllReadings(){varcommand="SELECT * FROM Readings";varreadings=SqlOperator.GetReadings(command);if(readings.Count>0)returnOk(readings);elsereturnNotFound();}publicIHttpActionResultGetReading(intid){varcommand=$"SELECT * FROM Readings WHERE Id={id}";varreadings=SqlOperator.GetReadings(command);if(readings.Count==1)returnOk(readings[0]);elsereturnNotFound();}[Route("api/Readings/latest")]publicIHttpActionResultGetLatestReading(){varcommand="SELECT * FROM Readings "+"WHERE TimeStamp IN(SELECT MAX(TimeStamp) FROM Readings)";varreading=SqlOperator.GetReadings(command)[0];if(reading!=null)returnOk(reading);elsereturnNotFound();}[Route("api/Readings/lastweek")]publicIHttpActionResultGetReadingsFromLastWeek(){vartimeNow=DateTimeOffset.Now.ToUnixTimeSeconds();varcommand="SELECT * FROM Readings "+$"WHERE TimeStamp BETWEEN {timeNow-7*24*3600} AND {timeNow}";varreadings=SqlOperator.GetReadings(command);if(readings.Count>0)returnOk(readings);elsereturnNotFound();}publicIHttpActionResultPutReading(intid,Readingreading){if(reading!=null){SqlOperator.PutReading(id,reading);returnOk();}elsereturnBadRequest();}publicIHttpActionResultPostReading(Readingreading){if(reading!=null){SqlOperator.PostReading(reading);varaqi=AqiCalculator.CalculateAqi(reading);if(aqi.Value>=151)EmailSender.SendEmail(aqi);returnOk(reading);}elsereturnBadRequest();}publicIHttpActionResultDeleteReading(intid){varcommand=$"SELECT * FROM Readings WHERE Id={id}";varreadings=SqlOperator.GetReadings(command);if(readings.Count==0)returnNotFound();SqlOperator.DeleteReading(id);returnOk(readings[0]);}}}