diff --git a/WrightBrothersApi/Controllers/PlanesController.cs b/WrightBrothersApi/Controllers/PlanesController.cs index aaa665e..a381578 100644 --- a/WrightBrothersApi/Controllers/PlanesController.cs +++ b/WrightBrothersApi/Controllers/PlanesController.cs @@ -21,26 +21,41 @@ public PlanesController(ILogger logger) Id = 1, Name = "Wright Flyer", Year = 1903, - Description = "The first successful heavier-than-air powered aircraft." + Description = "The first successful heavier-than-air powered aircraft.", + RangeInKm = 12, + ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/8/8d/Wright_Flyer.jpg" }, new Plane { Id = 2, Name = "Wright Flyer II", Year = 1904, - Description = "A refinement of the original Flyer with better performance." + Description = "A refinement of the original Flyer with better performance.", + RangeInKm = 24, + ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/8/8d/Wright_Flyer.jpg" }, + new Plane + { + Id = 3, + Name = "Wright Flyer III", + Year = 1905, + Description = "The first fully controllable Flyer.", + RangeInKm = 39, + ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/8/8d/Wright_Flyer.jpg" + } }; [HttpGet] public ActionResult> Get() { + Console.WriteLine("GET /planes"); return Planes; } [HttpGet("{id}")] public ActionResult Get(int id) { + Console.WriteLine($"GET /plane with Id = [{id}]"); var plane = Planes.Find(p => p.Id == id); if (plane == null) @@ -54,9 +69,54 @@ public ActionResult Get(int id) [HttpPost] public ActionResult Post(Plane plane) { + Console.WriteLine($"POST /planes with Id = [{plane.Id}]"); Planes.Add(plane); return CreatedAtAction(nameof(Get), new { id = plane.Id }, plane); } + + [HttpPut("{id}")] + public IActionResult Put(int id, Plane plane) + { + if (id != plane.Id) + { + return BadRequest(); + } + + var index = Planes.FindIndex(p => p.Id == id); + + if (index == -1) + { + return NotFound(); + } + + Planes[index] = plane; + + return NoContent(); + } + + [HttpDelete("{id}")] + public IActionResult Delete(int id) + { + var index = Planes.FindIndex(p => p.Id == id); + + if (index == -1) + { + return NotFound(); + } + + Planes.RemoveAt(index); + + return NoContent(); + } + + // calculate the count of planes between two input years + [HttpGet("count")] + public ActionResult Count(int startYear, int endYear) + { + var count = Planes.Count(p => p.Year >= startYear && p.Year <= endYear); + + return count; + } } } diff --git a/WrightBrothersApi/Models/Plane.cs b/WrightBrothersApi/Models/Plane.cs index c361b73..7430be9 100644 --- a/WrightBrothersApi/Models/Plane.cs +++ b/WrightBrothersApi/Models/Plane.cs @@ -9,5 +9,8 @@ public class Plane public int Year { get; set; } public string Description { get; set; } + + public int RangeInKm { get; set; } + public string ImageUrl { get; set; } } }