Skip to content

Commit

Permalink
feat: Add null check and error handling in PostHero method for better…
Browse files Browse the repository at this point in the history
… stability 🚀
  • Loading branch information
0GiS0 committed Dec 29, 2024
1 parent a97d743 commit 0b4edee
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Controllers/HeroController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,21 @@ public ActionResult PutHero(int id, Hero hero)
[HttpPost]
public ActionResult<Hero> PostHero(Hero hero)
{
if (hero == null)
{
return BadRequest("Hero cannot be null.");
}

_heroRepository.Add(hero);

return Ok(hero);

try
{
_heroRepository.Add(hero);
return CreatedAtAction(nameof(GetHero), new { id = hero.Id }, hero);
}
catch (Exception ex)
{
// Log the exception (ex) here if necessary
return StatusCode(500, "Internal server error");
}
}

// DELETE: api/Hero/5
Expand Down

0 comments on commit 0b4edee

Please sign in to comment.