dev postgresql database integration
This commit is contained in:
1
api/.gitignore
vendored
1
api/.gitignore
vendored
@@ -25,6 +25,7 @@ bld/
|
|||||||
project.lock.json
|
project.lock.json
|
||||||
project.fragment.lock.json
|
project.fragment.lock.json
|
||||||
artifacts/
|
artifacts/
|
||||||
|
[Mm]igrations/
|
||||||
|
|
||||||
# NuGet Packages
|
# NuGet Packages
|
||||||
*.nupkg
|
*.nupkg
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
|
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
using agologumApi.Stores;
|
using agologumApi.Services;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||||
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddSingleton<UserStore>();
|
builder.Services.AddScoped<UserService>();
|
||||||
|
|
||||||
// configuration for behind my nginx proxy
|
// configuration for behind my nginx proxy
|
||||||
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||||
|
|||||||
@@ -9,6 +9,16 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.3" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.5" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.5">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.5">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.5" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.5" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,9 @@
|
|||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"DefaultConnection": "Host=localhost;Port=5432;Database=devTest1;Username=devuser;Password=admin5"
|
||||||
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"https_port": 443
|
"https_port": 443
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,39 @@
|
|||||||
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using agologumApi.Models;
|
using agologumApi.Models;
|
||||||
using agologumApi.Stores;
|
using agologumApi.Services;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class UsersController : ControllerBase
|
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]
|
[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}")]
|
[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)
|
if (user == null) return NotFound();
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
return Ok(user);
|
return Ok(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[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(
|
return CreatedAtAction(
|
||||||
nameof(getUser),
|
nameof(getUser),
|
||||||
@@ -44,23 +43,21 @@ public class UsersController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{id}")]
|
[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)
|
if (updated == null) return NotFound();
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
return Ok(updated);
|
return Ok(updated);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpDelete("{id}")]
|
[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)
|
if (success) return NotFound();
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
return NoContent();
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,4 +5,4 @@ import index from './pages/index.vue'
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<router-view /> <!-- Routed components appear here -->
|
<router-view /> <!-- Routed components appear here -->
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user