Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
Imporve API response codes
  • Loading branch information
mtracewicz committed Sep 27, 2024
1 parent ed90653 commit c5ddb62
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions backend/src/api/Controllers/TodoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ public async Task<IActionResult> CreateItem([FromBody] TodoItem request)
return userId switch
{
null => Unauthorized(),
var user => await Create(user, request),
var user => await Handle(user, request),
};

async Task<IActionResult> Create(string user, TodoItem item)
async Task<IActionResult> Handle(string user, TodoItem item)
{
var newItem = await _service.CreateItem(user, item);
if (newItem is null)
{
return BadRequest();
}
return Created(HttpContext.Request.Path.Add(new PathString($"/{newItem.Id}")), newItem);
}
}
Expand All @@ -134,8 +138,18 @@ public async Task<IActionResult> GetItem([FromRoute] int id)
return userId switch
{
null => Unauthorized(),
var user => Ok(await _service.GetItem(user, id)),
var user => await Handle(user, id),
};

async Task<IActionResult> Handle(string user, int id)
{
var item = await _service.GetItem(user, id);
if (item is null)
{
return NotFound();
}
return Ok(item);
}
}

[HttpDelete("items/{id}")]
Expand All @@ -146,8 +160,18 @@ public async Task<IActionResult> DeleteItem([FromRoute] int id)
return userId switch
{
null => Unauthorized(),
var user => Ok(await _service.DeleteItem(user, id)),
var user => await Handle(user, id),
};

async Task<IActionResult> Handle(string user, int id)
{
var success = await _service.DeleteItem(user, id);
if (success)
{
return Ok();
}
return BadRequest();
}
}

[HttpPut("items/{id}")]
Expand All @@ -158,8 +182,18 @@ public async Task<IActionResult> UpdateItem([FromRoute] int id, [FromBody] TodoI
return userId switch
{
null => Unauthorized(),
var user => Ok(await _service.UpdateItem(user, request)),
var user => await Handle(user, id),
};

async Task<IActionResult> Handle(string user, int id)
{
var success = await _service.UpdateItem(user, request);
if (success)
{
return Ok();
}
return BadRequest();
}
}
}

Expand Down

0 comments on commit c5ddb62

Please sign in to comment.