Skip to content

Commit

Permalink
Merge pull request #18 from nanotaboada/feature/http-409-conflict
Browse files Browse the repository at this point in the history
refactor: add HTTP 409 Conflict response on HTTP POST verb
  • Loading branch information
nanotaboada authored Dec 18, 2023
2 parents ce96d8b + 084e0f9 commit 4c719fd
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ public PlayersController(IPlayerService playerService, ILogger<PlayersController
_logger = logger;
}

/*
-----------------------------------------------------------------------------------------------
HTTP POST
To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
-----------------------------------------------------------------------------------------------
*/

[HttpPost]
public async Task<ActionResult<Player>> PostPlayer(Player player)
{
if (await _playerService.RetrieveById(player.Id) != null)
{
return Conflict();
}
else
{
await _playerService.Create(player);

return CreatedAtAction(nameof(GetPlayer), new { id = player.Id }, player);
}
}

/*
-----------------------------------------------------------------------------------------------
HTTP GET
Expand All @@ -39,13 +61,13 @@ public async Task<ActionResult<IEnumerable<Player>>> GetPlayers()
{
var players = await _playerService.Retrieve();

if (players == null || players.Count == 0)
if (players.Any())
{
return NotFound();
return players;
}
else
{
return players;
return NotFound();
}
}

Expand All @@ -54,13 +76,13 @@ public async Task<ActionResult<Player>> GetPlayer(long id)
{
var player = await _playerService.RetrieveById(id);

if (player == null)
if (player != null)
{
return NotFound();
return player;
}
else
{
return player;
return NotFound();
}
}

Expand Down Expand Up @@ -90,21 +112,6 @@ public async Task<IActionResult> PutPlayer(long id, Player player)
}
}

/*
-----------------------------------------------------------------------------------------------
HTTP POST
To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
-----------------------------------------------------------------------------------------------
*/

[HttpPost]
public async Task<ActionResult<Player>> PostPlayer(Player player)
{
await _playerService.Create(player);

return CreatedAtAction(nameof(GetPlayer), new { id = player.Id }, player);
}

/*
-----------------------------------------------------------------------------------------------
HTTP DELETE
Expand Down

0 comments on commit 4c719fd

Please sign in to comment.