Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add range and image URL properties to Plane model #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}
}
Loading