add crud to api
All checks were successful
Build and Deploy Frontend / build-and-deploy (push) Successful in 6s

This commit is contained in:
2026-03-10 22:49:14 -05:00
parent 728258465d
commit 9b6a4c75b9
12 changed files with 172 additions and 61 deletions

View 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();
}
}

10
api/src/Models/User.cs Normal file
View File

@@ -0,0 +1,10 @@
namespace agologumApi.Models;
public class User {
public int Id { get; set; }
public string Name { get; set; } = "";
public string Email { get; set; } = "";
};

View File

@@ -0,0 +1,46 @@
// temporary state management
// this will eventually change into a service that uses EnitityFramework for database interactions
using agologumApi.Models;
namespace agologumApi.Stores;
public class UserStore {
private readonly List<User> users_ = new();
private int nextId_ = 1;
public List<User> getUsers() {
return users_;
}
public User? getUser(int id) {
return users_.FirstOrDefault(x => x.Id == id);
}
public User createUser(User user) {
user.Id = nextId_++;
users_.Add(user);
return user;
}
public User? updateUser(int id, User updated) {
var existing = users_.FirstOrDefault(x => x.Id == id);
if(existing == null) return null;
existing.Name = updated.Name;
existing.Email = updated.Email;
return existing;
}
public bool deleteUser(int id) {
var existing = users_.FirstOrDefault(x => x.Id == id);
if(existing == null) return false;
users_.Remove(existing);
return true;
}
}