API enhancements

Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
Esteban Solano Granados 2022-06-13 18:07:27 +00:00 committed by GitHub
parent 996b7ac9bf
commit bd401b3c93
3 changed files with 30 additions and 18 deletions

View File

@ -5,9 +5,14 @@ using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver; using MongoDB.Driver;
using Models; using Models;
using Swashbuckle.AspNetCore.SwaggerGen;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
string connectionString = builder.Configuration.GetConnectionString("DocumentDbConnection"); string connectionString = builder.Configuration.GetConnectionString("DocumentDbConnection");
string databaseName = builder.Configuration["DocumentDbName"] ?? "BackendMongoDb"; string databaseName = builder.Configuration["DocumentDbName"] ?? "BackendMongoDb";
string collectionName = builder.Configuration["DocumentCollectionName"] ?? "ToDos"; string collectionName = builder.Configuration["DocumentCollectionName"] ?? "ToDos";
@ -16,12 +21,18 @@ builder.Services.AddTransient<MongoClient>((_provider) => new MongoClient(connec
var app = builder.Build(); var app = builder.Build();
app.MapGet("/api/todos", async (MongoClient connection) => { // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/api/todos", async (MongoClient connection) =>
{
try try
{ {
var client = new MongoClient(connectionString); var database = connection.GetDatabase(databaseName);
var database = client.GetDatabase(databaseName);
var collection = database.GetCollection<ToDo>(collectionName); var collection = database.GetCollection<ToDo>(collectionName);
var results = await collection.Find(_ => true).ToListAsync().ConfigureAwait(false); var results = await collection.Find(_ => true).ToListAsync().ConfigureAwait(false);
@ -33,19 +44,21 @@ app.MapGet("/api/todos", async (MongoClient connection) => {
} }
}); });
app.MapGet("/api/todos/{id}", async (string id, MongoClient connection) =>
app.MapGet("/api/todos/{id}", async (string id, MongoClient connection) => { {
try try
{ {
var client = new MongoClient(connectionString); var database = connection.GetDatabase(databaseName);
var database = client.GetDatabase(databaseName);
var collection = database.GetCollection<ToDo>(collectionName); var collection = database.GetCollection<ToDo>(collectionName);
var result = await collection.FindAsync(record => record.Id == id).ConfigureAwait(false);
return result is ToDo todo var result = await collection.FindAsync(record => record.Id == id).ConfigureAwait(false) as ToDo;
? Results.Ok(todo)
: Results.NotFound(); if (result is null)
{
return Results.NotFound();
}
return Results.Created($"/todoitems/{result.Id}", result);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -53,12 +66,11 @@ app.MapGet("/api/todos/{id}", async (string id, MongoClient connection) => {
} }
}); });
app.MapPost("/api/todos", async (ToDo record, MongoClient connection) => { app.MapPost("/api/todos", async (ToDo record, MongoClient connection) =>
{
try try
{ {
var client = new MongoClient(connectionString); var database = connection.GetDatabase(databaseName);
var database = client.GetDatabase(databaseName);
var collection = database.GetCollection<ToDo>(collectionName); var collection = database.GetCollection<ToDo>(collectionName);
await collection.InsertOneAsync(record).ConfigureAwait(false); await collection.InsertOneAsync(record).ConfigureAwait(false);

View File

@ -6,5 +6,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MongoDb.Driver" Version="2.16.0" /> <PackageReference Include="MongoDb.Driver" Version="2.16.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -12,7 +12,6 @@ server {
add_header X-Permitted-Cross-Domain-Policies master-only; add_header X-Permitted-Cross-Domain-Policies master-only;
add_header Referrer-Policy same-origin; add_header Referrer-Policy same-origin;
add_header Expect-CT 'max-age=60'; add_header Expect-CT 'max-age=60';
add_header Feature-Policy "accelerometer none; ambient-light-sensor none; battery none; camera none; gyroscope none;";
location / { location / {
root /usr/share/nginx/html; root /usr/share/nginx/html;