70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
using Dapper;
|
|
using Npgsql;
|
|
using SDG_Backend_Barracuda.Models;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
builder.Services.AddOpenApi();
|
|
|
|
// Register NpgsqlDataSource
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
|
builder.Services.AddNpgsqlDataSource(connectionString!);
|
|
|
|
// Register Data Tables
|
|
builder.Services.AddScoped<ICardModel, CardModel>();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Ensure the table exists
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var dataSource = scope.ServiceProvider.GetRequiredService<NpgsqlDataSource>();
|
|
await using var connection = await dataSource.OpenConnectionAsync();
|
|
await connection.ExecuteAsync(@"
|
|
CREATE TABLE IF NOT EXISTS ""Card"" (
|
|
""Id"" SERIAL PRIMARY KEY,
|
|
""Value"" TEXT NOT NULL
|
|
);");
|
|
}
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.MapPost("/card", async (CardInput input, ICardModel card) =>
|
|
{
|
|
var newCard = await card.CreateAsync(input);
|
|
return Results.Created($"/card/{newCard.Id}", newCard);
|
|
});
|
|
|
|
app.MapGet("/card", async (ICardModel card) =>
|
|
{
|
|
var result = await card.GetAllAsync();
|
|
return Results.Ok(result);
|
|
});
|
|
|
|
app.MapGet("/card/{id}", async (int id, ICardModel card) =>
|
|
{
|
|
var newCard = await card.GetByIdAsync(id);
|
|
return newCard is not null ? Results.Ok(newCard) : Results.NotFound();
|
|
});
|
|
|
|
app.MapPut("/card/{id}", async (int id, CardInput input, ICardModel card) =>
|
|
{
|
|
var newCard = await card.UpdateAsync(id, input);
|
|
return newCard is not null ? Results.Ok(newCard) : Results.NotFound();
|
|
});
|
|
|
|
app.MapDelete("/card/{id}", async (int id, ICardModel card) =>
|
|
{
|
|
var success = await card.DeleteAsync(id);
|
|
return success ? Results.NoContent() : Results.NotFound();
|
|
});
|
|
|
|
app.Run(); |