From 08e7a82cc7b237e550c0236bebd4f41c3c3f4acf Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Fri, 16 Mar 2018 21:40:28 +0100 Subject: Moved API to separate folder. --- .../Controllers/HomeController.cs | 19 +++++ .../Controllers/ReadingsController.cs | 85 ++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 pollutometer-api/PollutometerWebApi/Controllers/HomeController.cs create mode 100644 pollutometer-api/PollutometerWebApi/Controllers/ReadingsController.cs (limited to 'pollutometer-api/PollutometerWebApi/Controllers') diff --git a/pollutometer-api/PollutometerWebApi/Controllers/HomeController.cs b/pollutometer-api/PollutometerWebApi/Controllers/HomeController.cs new file mode 100644 index 0000000..69e4846 --- /dev/null +++ b/pollutometer-api/PollutometerWebApi/Controllers/HomeController.cs @@ -0,0 +1,19 @@ +using System; +using System.Web.Mvc; + +namespace PollutometerWebApi.Controllers +{ + public class HomeController : Controller + { + public ActionResult Index() + { + var mvcName = typeof(Controller).Assembly.GetName(); + var isMono = Type.GetType("Mono.Runtime") != null; + + ViewData["Version"] = mvcName.Version.Major + "." + mvcName.Version.Minor; + ViewData["Runtime"] = isMono ? "Mono" : ".NET"; + + return View(); + } + } +} diff --git a/pollutometer-api/PollutometerWebApi/Controllers/ReadingsController.cs b/pollutometer-api/PollutometerWebApi/Controllers/ReadingsController.cs new file mode 100644 index 0000000..adf4d5d --- /dev/null +++ b/pollutometer-api/PollutometerWebApi/Controllers/ReadingsController.cs @@ -0,0 +1,85 @@ +using System; +using System.Web.Http; +using PollutometerWebApi.Models; + +namespace PollutometerWebApi.Controllers +{ + public class ReadingsController : ApiController + { + public ReadingsController() {} + + public IHttpActionResult GetAllReadings() + { + var command = "SELECT * FROM Readings"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count > 0) return Ok(readings); + else return NotFound(); + } + + public IHttpActionResult GetReading(int id) + { + var command = $"SELECT * FROM Readings WHERE Id={id}"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count == 1) return Ok(readings[0]); + else return NotFound(); + } + + [Route("api/Readings/latest")] + public IHttpActionResult GetLatestReading() + { + var command = "SELECT * FROM Readings " + + "WHERE TimeStamp IN(SELECT MAX(TimeStamp) FROM Readings)"; + var reading = SqlOperator.GetReadings(command)[0]; + + if (reading != null) return Ok(reading); + else return NotFound(); + } + + [Route("api/Readings/lastweek")] + public IHttpActionResult GetReadingsFromLastWeek() + { + var timeNow = DateTimeOffset.Now.ToUnixTimeSeconds(); + var command = "SELECT * FROM Readings " + + $"WHERE TimeStamp BETWEEN {timeNow-7*24*3600} AND {timeNow}"; + var readings = SqlOperator.GetReadings(command); + + if (readings.Count > 0) return Ok(readings); + else return NotFound(); + } + + public IHttpActionResult PutReading(int id, Reading reading) + { + if (reading != null) + { + SqlOperator.PutReading(id, reading); + return Ok(); + } + else return BadRequest(); + } + + public IHttpActionResult PostReading(Reading reading) + { + if (reading != null) + { + SqlOperator.PostReading(reading); + var aqi = AqiCalculator.CalculateAqi(reading); + if (aqi.Value >= 151) + EmailSender.SendEmail(aqi); + return Ok(reading); + } + else return BadRequest(); + } + + public IHttpActionResult DeleteReading(int id) + { + var command = $"SELECT * FROM Readings WHERE Id={id}"; + var readings = SqlOperator.GetReadings(command); + if (readings.Count == 0) return NotFound(); + SqlOperator.DeleteReading(id); + + return Ok(readings[0]); + } + } +} \ No newline at end of file -- cgit v1.2.3