dev postgresql database integration
This commit is contained in:
@@ -1,40 +1,39 @@
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using agologumApi.Models;
|
||||
using agologumApi.Stores;
|
||||
using agologumApi.Services;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UsersController : ControllerBase
|
||||
{
|
||||
private readonly UserStore store_;
|
||||
private readonly UserService service_;
|
||||
|
||||
public UsersController(UserStore store)
|
||||
public UsersController(UserService service)
|
||||
{
|
||||
store_ = store;
|
||||
service_ = service;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<List<User>> getUsers()
|
||||
public async Task<ActionResult<List<User>>> getUsers()
|
||||
{
|
||||
return Ok(store_.getUsers());
|
||||
return Ok(await service_.GetAll());
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
public ActionResult<User> getUser(int id)
|
||||
public async Task<ActionResult<User>> getUser(int id)
|
||||
{
|
||||
var user = store_.getUser(id);
|
||||
var user = await service_.Get(id);
|
||||
|
||||
if (user == null)
|
||||
return NotFound();
|
||||
if (user == null) return NotFound();
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult<User> createUser(User user)
|
||||
public async Task<ActionResult<User>> createUser(User user)
|
||||
{
|
||||
var created = store_.createUser(user);
|
||||
var created = await service_.Create(user);
|
||||
|
||||
return CreatedAtAction(
|
||||
nameof(getUser),
|
||||
@@ -44,23 +43,21 @@ public class UsersController : ControllerBase
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public ActionResult<User> updateUser(int id, User user)
|
||||
public async Task<ActionResult<User>> updateUser(int id, User user)
|
||||
{
|
||||
var updated = store_.updateUser(id, user);
|
||||
var updated = await service_.Update(user);
|
||||
|
||||
if (updated == null)
|
||||
return NotFound();
|
||||
if (updated == null) return NotFound();
|
||||
|
||||
return Ok(updated);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public ActionResult deleteUser(int id)
|
||||
public async Task<ActionResult> deleteUser(int id)
|
||||
{
|
||||
var success = store_.deleteUser(id);
|
||||
var success = await service_.Delete(id);
|
||||
|
||||
if (!success)
|
||||
return NotFound();
|
||||
if (success) return NotFound();
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user