add crud to api
All checks were successful
Build and Deploy Frontend / build-and-deploy (push) Successful in 6s
All checks were successful
Build and Deploy Frontend / build-and-deploy (push) Successful in 6s
This commit is contained in:
67
api/src/Controllers/UsersController.cs
Normal file
67
api/src/Controllers/UsersController.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using agologumApi.Models;
|
||||
using agologumApi.Stores;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UsersController : ControllerBase
|
||||
{
|
||||
private readonly UserStore store_;
|
||||
|
||||
public UsersController(UserStore store)
|
||||
{
|
||||
store_ = store;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<List<User>> getUsers()
|
||||
{
|
||||
return Ok(store_.getUsers());
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public ActionResult<User> getUser(int id)
|
||||
{
|
||||
var user = store_.getUser(id);
|
||||
|
||||
if (user == null)
|
||||
return NotFound();
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult<User> createUser(User user)
|
||||
{
|
||||
var created = store_.createUser(user);
|
||||
|
||||
return CreatedAtAction(
|
||||
nameof(getUser),
|
||||
new { id = created.Id },
|
||||
created
|
||||
);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public ActionResult<User> updateUser(int id, User user)
|
||||
{
|
||||
var updated = store_.updateUser(id, user);
|
||||
|
||||
if (updated == null)
|
||||
return NotFound();
|
||||
|
||||
return Ok(updated);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public ActionResult deleteUser(int id)
|
||||
{
|
||||
var success = store_.deleteUser(id);
|
||||
|
||||
if (!success)
|
||||
return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user