Skip to content

Commit

Permalink
Add range and image URL properties to Plane model
Browse files Browse the repository at this point in the history
  • Loading branch information
rajbos committed Nov 28, 2023
1 parent 1c73d90 commit 3f38760
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
64 changes: 62 additions & 2 deletions WrightBrothersApi/Controllers/PlanesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,41 @@ public PlanesController(ILogger<PlanesController> 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<List<Plane>> Get()
{
Console.WriteLine("GET /planes");
return Planes;
}

[HttpGet("{id}")]
public ActionResult<Plane> Get(int id)
{
Console.WriteLine($"GET /plane with Id = [{id}]");
var plane = Planes.Find(p => p.Id == id);

if (plane == null)
Expand All @@ -54,9 +69,54 @@ public ActionResult<Plane> Get(int id)
[HttpPost]
public ActionResult<Plane> 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<int> Count(int startYear, int endYear)
{
var count = Planes.Count(p => p.Year >= startYear && p.Year <= endYear);

return count;
}
}
}
3 changes: 3 additions & 0 deletions WrightBrothersApi/Models/Plane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}

0 comments on commit 3f38760

Please sign in to comment.