2022-06-11 01:26:09 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MongoDB.Bson;
|
|
|
|
|
using MongoDB.Bson.Serialization.Attributes;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using Models;
|
2022-06-13 18:07:27 +00:00
|
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
2022-06-11 01:26:09 +00:00
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
2022-06-13 18:07:27 +00:00
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
|
2022-06-13 17:28:56 +00:00
|
|
|
|
string connectionString = builder.Configuration.GetConnectionString("DocumentDbConnection");
|
|
|
|
|
string databaseName = builder.Configuration["DocumentDbName"] ?? "BackendMongoDb";
|
|
|
|
|
string collectionName = builder.Configuration["DocumentCollectionName"] ?? "ToDos";
|
|
|
|
|
|
2022-06-11 01:26:09 +00:00
|
|
|
|
builder.Services.AddTransient<MongoClient>((_provider) => new MongoClient(connectionString));
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2022-06-13 18:07:27 +00:00
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.MapGet("/api/todos", async (MongoClient connection) =>
|
|
|
|
|
{
|
2022-06-11 01:26:09 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-13 18:07:27 +00:00
|
|
|
|
var database = connection.GetDatabase(databaseName);
|
2022-06-13 17:28:56 +00:00
|
|
|
|
var collection = database.GetCollection<ToDo>(collectionName);
|
2022-06-11 01:26:09 +00:00
|
|
|
|
var results = await collection.Find(_ => true).ToListAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return Results.Ok(results);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return Results.Problem(detail: ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-13 18:07:27 +00:00
|
|
|
|
app.MapGet("/api/todos/{id}", async (string id, MongoClient connection) =>
|
|
|
|
|
{
|
2022-06-13 17:28:56 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-13 18:07:27 +00:00
|
|
|
|
var database = connection.GetDatabase(databaseName);
|
2022-06-13 17:28:56 +00:00
|
|
|
|
var collection = database.GetCollection<ToDo>(collectionName);
|
|
|
|
|
|
2022-06-13 18:07:27 +00:00
|
|
|
|
var result = await collection.FindAsync(record => record.Id == id).ConfigureAwait(false) as ToDo;
|
|
|
|
|
|
|
|
|
|
if (result is null)
|
|
|
|
|
{
|
|
|
|
|
return Results.NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Results.Created($"/todoitems/{result.Id}", result);
|
2022-06-13 17:28:56 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return Results.Problem(detail: ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-06-13 18:07:27 +00:00
|
|
|
|
app.MapPost("/api/todos", async (ToDo record, MongoClient connection) =>
|
|
|
|
|
{
|
2022-06-13 17:28:56 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-13 18:07:27 +00:00
|
|
|
|
var database = connection.GetDatabase(databaseName);
|
2022-06-13 17:28:56 +00:00
|
|
|
|
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();
|