feature/client-template #2
1
api/.gitignore
vendored
1
api/.gitignore
vendored
@@ -25,6 +25,7 @@ bld/
|
||||
project.lock.json
|
||||
project.fragment.lock.json
|
||||
artifacts/
|
||||
[Mm]igrations/
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
|
||||
@@ -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<AppDbContext>(options =>
|
||||
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddSingleton<UserStore>();
|
||||
builder.Services.AddScoped<UserService>();
|
||||
|
||||
// configuration for behind my nginx proxy
|
||||
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||
|
||||
@@ -9,6 +9,16 @@
|
||||
|
||||
<ItemGroup>
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Host=localhost;Port=5432;Database=devTest1;Username=devuser;Password=admin5"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"https_port": 443
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,4 +5,4 @@ import index from './pages/index.vue'
|
||||
|
||||
<template>
|
||||
<router-view /> <!-- Routed components appear here -->
|
||||
</template>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user