diff --git a/api/.gitignore b/api/.gitignore index 01e74fd..1d9f974 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -25,6 +25,7 @@ bld/ project.lock.json project.fragment.lock.json artifacts/ +[Mm]igrations/ # NuGet Packages *.nupkg diff --git a/api/Program.cs b/api/Program.cs index d0e4ce9..302b215 100644 --- a/api/Program.cs +++ b/api/Program.cs @@ -1,12 +1,16 @@ using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.EntityFrameworkCore; -using agologumApi.Stores; +using agologumApi.Services; var builder = WebApplication.CreateBuilder(args); +builder.Services.AddDbContext(options => + options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); + builder.Services.AddControllers(); -builder.Services.AddSingleton(); +builder.Services.AddScoped(); // configuration for behind my nginx proxy builder.Services.Configure(options => diff --git a/api/agologum-api.csproj b/api/agologum-api.csproj index 525cfa3..77eaeef 100644 --- a/api/agologum-api.csproj +++ b/api/agologum-api.csproj @@ -9,6 +9,16 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/api/appsettings.json b/api/appsettings.json index 822760b..1059c4a 100644 --- a/api/appsettings.json +++ b/api/appsettings.json @@ -5,6 +5,9 @@ "Microsoft.AspNetCore": "Warning" } }, + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Port=5432;Database=devTest1;Username=devuser;Password=admin5" + }, "AllowedHosts": "*", "https_port": 443 } diff --git a/api/src/Controllers/UsersController.cs b/api/src/Controllers/UsersController.cs index 38cb664..8e7ed76 100644 --- a/api/src/Controllers/UsersController.cs +++ b/api/src/Controllers/UsersController.cs @@ -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> getUsers() + public async Task>> getUsers() { - return Ok(store_.getUsers()); + return Ok(await service_.GetAll()); } [HttpGet("{id:int}")] - public ActionResult getUser(int id) + public async Task> 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 createUser(User user) + public async Task> 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 updateUser(int id, User user) + public async Task> 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 deleteUser(int id) { - var success = store_.deleteUser(id); + var success = await service_.Delete(id); - if (!success) - return NotFound(); + if (success) return NotFound(); return NoContent(); } diff --git a/api/src/Stores/UserStore.cs b/api/src/Stores/UserStore.cs deleted file mode 100644 index 456bf33..0000000 --- a/api/src/Stores/UserStore.cs +++ /dev/null @@ -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 users_ = new(); - private int nextId_ = 1; - - public List 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; - } - -} \ No newline at end of file diff --git a/client/src/App.vue b/client/src/App.vue index efc0210..cd279a6 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -5,4 +5,4 @@ import index from './pages/index.vue' \ No newline at end of file +