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 images and update planes controller #2

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
95 changes: 87 additions & 8 deletions WrightBrothersApi/Controllers/PlanesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,72 @@

namespace WrightBrothersApi.Controllers
{
/// <summary>
/// Controller for managing planes.
/// </summary>
[ApiController]
[Route("[controller]")]
public class PlanesController : ControllerBase
{
private readonly ILogger<PlanesController> _logger;

/// <summary>
/// Initializes a new instance of the <see cref="PlanesController"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
public PlanesController(ILogger<PlanesController> logger)
{
_logger = logger;
}

private static readonly List<Plane> Planes = new List<Plane>
{
new Plane
{
Id = 1,
Name = "Wright Flyer",
Year = 1903,
Description = "The first successful heavier-than-air powered aircraft."
},
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.",
ImageUrl = "https://cdn-vecozo.com/wikipedia/commons/2/2d/Wright_Flyer_II.jpg"
},
new Plane
{
Id = 3,
Name = "Wright Flyer III",
Year = 1905,
Description = "The final version of the Wright brothers' aircraft.",
ImageUrl = "https://cdn-vecozo.com/wikipedia/commons/3/3d/Wright_Flyer_III.jpg"
},
new Plane
{
Id = 4,
Name = "Wright Model A",
Year = 1908,
Description = "The first mass-produced aircraft.",
ImageUrl = "https://cdn-vecozo.com/wikipedia/commons/4/4d/Wright_Model_A.jpg"
}
};

/// <summary>
/// Gets all planes.
/// </summary>
/// <returns>A list of planes.</returns>
[HttpGet]
public ActionResult<List<Plane>> Get()
{
_logger.LogInformation(" >>> Getting planes >>>");
return Planes;
}

/// <summary>
/// Gets a plane by its ID.
/// </summary>
/// <param name="id">The ID of the plane.</param>
/// <returns>The plane with the specified ID.</returns>
[HttpGet("{id}")]
public ActionResult<Plane> Get(int id)
{
_logger.LogInformation($" >>> Getting plane with id [{id}] >>>");
var plane = Planes.Find(p => p.Id == id);

if (plane == null)
Expand All @@ -51,12 +79,63 @@
return plane;
}

/// <summary>
/// Adds a new plane.
/// </summary>
/// <param name="plane">The plane to add.</param>
/// <returns>The added plane.</returns>
[HttpPost]
public ActionResult<Plane> Post(Plane plane)
{
_logger.LogInformation($" >>> Adding plane with name [{plane.Name}] >>>");
Planes.Add(plane);

return CreatedAtAction(nameof(Get), new { id = plane.Id }, plane);
}

/// <summary>
/// Updates a plane.
/// </summary>
/// <param name="id">The ID of the plane to update.</param>
/// <param name="plane">The updated plane.</param>
/// <param name="lastUpdateDate">The last update date of the plane.</param>
/// <returns>No content if the update is successful, bad request if the ID doesn't match, or not found if the plane doesn't exist.</returns>
[HttpPut("{id}")]
public IActionResult Put(int id, Plane plane, DateTime lastUpdateDate)
{
if (id != plane.Id)
{
return BadRequest();
}

var existingPlane = Planes.Find(p => p.Id == id);

if (existingPlane == null)
{
return NotFound();
}

existingPlane.Name = plane.Name;
existingPlane.Year = plane.Year;
existingPlane.Description = plane.Description;

return NoContent();
}

public IActionResult FindallKerosinePlanes() {
// Find all planes that use kerosine
// must be made after 1913
// non military vehicle
// needs two pilots
// has a range of 1000 km

var kerosinePlanes = Planes.Where(
p => p.Year > 1913 &&
p.FuelIsKerosine()) &&
p.Pilots.Count() = 2) &&

Check failure on line 135 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

; expected

Check failure on line 135 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

} expected

Check failure on line 135 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

Invalid expression term '&&'

Check failure on line 135 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

; expected

Check failure on line 135 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

} expected

Check failure on line 135 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

Invalid expression term '&&'
p.Description.Contains("non military vehicle") &&
p.Description.Contains("range of 1000 km")).ToList();

Check failure on line 137 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

; expected

Check failure on line 137 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

} expected

Check failure on line 137 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

; expected

Check failure on line 137 in WrightBrothersApi/Controllers/PlanesController.cs

View workflow job for this annotation

GitHub Actions / build

} expected

}
}
}
10 changes: 10 additions & 0 deletions WrightBrothersApi/Models/Plane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ public class Plane
public int Year { get; set; }

public string Description { get; set; }

public string ImageUrl { get; set; }

public string Fuel { get; set; }

public bool FuelIsKerosine()
{
return Fuel == "Kerosine";
}

}
}
6 changes: 1 addition & 5 deletions WrightBrothersApi/WrightBrothersApi.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>


</Project>
</Project>
Loading