mirror of
https://github.com/docker/awesome-compose.git
synced 2025-04-29 07:38:02 +02:00
Frontend+Backend fixes
Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
parent
718df2a62d
commit
996b7ac9bf
10 changed files with 128 additions and 42 deletions
|
@ -8,18 +8,21 @@ using Models;
|
|||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
string connectionString = File.ReadAllText("/run/secrets/db-connection");
|
||||
string connectionString = builder.Configuration.GetConnectionString("DocumentDbConnection");
|
||||
string databaseName = builder.Configuration["DocumentDbName"] ?? "BackendMongoDb";
|
||||
string collectionName = builder.Configuration["DocumentCollectionName"] ?? "ToDos";
|
||||
|
||||
builder.Services.AddTransient<MongoClient>((_provider) => new MongoClient(connectionString));
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapGet("/api", async (MongoClient connection) => {
|
||||
app.MapGet("/api/todos", async (MongoClient connection) => {
|
||||
try
|
||||
{
|
||||
var client = new MongoClient(connectionString);
|
||||
var database = client.GetDatabase("BackendMongoDb");
|
||||
var database = client.GetDatabase(databaseName);
|
||||
|
||||
var collection = database.GetCollection<ToDo>("ToDos");
|
||||
var collection = database.GetCollection<ToDo>(collectionName);
|
||||
var results = await collection.Find(_ => true).ToListAsync().ConfigureAwait(false);
|
||||
|
||||
return Results.Ok(results);
|
||||
|
@ -30,4 +33,41 @@ app.MapGet("/api", async (MongoClient connection) => {
|
|||
}
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
||||
app.MapGet("/api/todos/{id}", async (string id, MongoClient connection) => {
|
||||
try
|
||||
{
|
||||
var client = new MongoClient(connectionString);
|
||||
var database = client.GetDatabase(databaseName);
|
||||
|
||||
var collection = database.GetCollection<ToDo>(collectionName);
|
||||
var result = await collection.FindAsync(record => record.Id == id).ConfigureAwait(false);
|
||||
|
||||
return result is ToDo todo
|
||||
? Results.Ok(todo)
|
||||
: Results.NotFound();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Results.Problem(detail: ex.ToString());
|
||||
}
|
||||
});
|
||||
|
||||
app.MapPost("/api/todos", async (ToDo record, MongoClient connection) => {
|
||||
try
|
||||
{
|
||||
var client = new MongoClient(connectionString);
|
||||
var database = client.GetDatabase(databaseName);
|
||||
|
||||
var collection = database.GetCollection<ToDo>(collectionName);
|
||||
await collection.InsertOneAsync(record).ConfigureAwait(false);
|
||||
|
||||
return Results.Created($"/api/todos/{record.Id}", record);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Results.Problem(detail: ex.ToString());
|
||||
}
|
||||
});
|
||||
|
||||
app.Run();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue