Added Card model, updated database initialization, udated readme.
This commit is contained in:
@@ -1,11 +1,34 @@
|
||||
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())
|
||||
{
|
||||
@@ -14,28 +37,34 @@ if (app.Environment.IsDevelopment())
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
var summaries = new[]
|
||||
app.MapPost("/card", async (CardInput input, ICardModel card) =>
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
var newCard = await card.CreateAsync(input);
|
||||
return Results.Created($"/card/{newCard.Id}", newCard);
|
||||
});
|
||||
|
||||
app.MapGet("/weatherforecast", () =>
|
||||
{
|
||||
var forecast = Enumerable.Range(1, 5).Select(index =>
|
||||
new WeatherForecast
|
||||
(
|
||||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
Random.Shared.Next(-20, 55),
|
||||
summaries[Random.Shared.Next(summaries.Length)]
|
||||
))
|
||||
.ToArray();
|
||||
return forecast;
|
||||
})
|
||||
.WithName("GetWeatherForecast");
|
||||
|
||||
app.Run();
|
||||
|
||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
||||
app.MapGet("/card", async (ICardModel card) =>
|
||||
{
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
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();
|
||||
Reference in New Issue
Block a user