From 426dcd9403192b7e7f40ad01bafb57f7e4f9071f Mon Sep 17 00:00:00 2001 From: Jelena Petrovic Date: Thu, 27 Oct 2022 01:15:21 +0200 Subject: omogucena komunikacija sa bazom #4 --- Backend/Api/Api/Api.csproj | 3 +-- Backend/Api/Api/Database/DatabaseConnection.cs | 10 ++++++++++ Backend/Api/Api/Interfaces/IDatabaseConnection.cs | 8 ++++++++ Backend/Api/Api/Program.cs | 14 ++++++++++++++ Backend/Api/Api/appsettings.json | 8 +++++++- 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 Backend/Api/Api/Database/DatabaseConnection.cs create mode 100644 Backend/Api/Api/Interfaces/IDatabaseConnection.cs (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Api.csproj b/Backend/Api/Api/Api.csproj index 8732b1d..f8d2732 100644 --- a/Backend/Api/Api/Api.csproj +++ b/Backend/Api/Api/Api.csproj @@ -7,14 +7,13 @@ + - - diff --git a/Backend/Api/Api/Database/DatabaseConnection.cs b/Backend/Api/Api/Database/DatabaseConnection.cs new file mode 100644 index 0000000..560eaee --- /dev/null +++ b/Backend/Api/Api/Database/DatabaseConnection.cs @@ -0,0 +1,10 @@ +using Api.Interfaces; + +namespace Api.Database +{ + public class DatabaseConnection : IDatabaseConnection + { + public string ConnectionString { get; set; } = String.Empty; + public string DatabaseName { get; set; } = String.Empty; + } +} diff --git a/Backend/Api/Api/Interfaces/IDatabaseConnection.cs b/Backend/Api/Api/Interfaces/IDatabaseConnection.cs new file mode 100644 index 0000000..ca5e320 --- /dev/null +++ b/Backend/Api/Api/Interfaces/IDatabaseConnection.cs @@ -0,0 +1,8 @@ +namespace Api.Interfaces +{ + public interface IDatabaseConnection + { + string ConnectionString { get; set; } + string DatabaseName { get; set; } + } +} diff --git a/Backend/Api/Api/Program.cs b/Backend/Api/Api/Program.cs index df2434c..2dfd84d 100644 --- a/Backend/Api/Api/Program.cs +++ b/Backend/Api/Api/Program.cs @@ -1,7 +1,21 @@ +using Api.Database; +using Api.Interfaces; +using Microsoft.Extensions.Options; +using MongoDB.Driver; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. +builder.Services.Configure( + builder.Configuration.GetSection("DatabaseSettings")); + +builder.Services.AddSingleton(sp => + sp.GetRequiredService>().Value); + +builder.Services.AddSingleton(s => + new MongoClient(builder.Configuration.GetValue("DatabaseSettings:ConnectionString"))); + builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); diff --git a/Backend/Api/Api/appsettings.json b/Backend/Api/Api/appsettings.json index 10f68b8..c2cbe28 100644 --- a/Backend/Api/Api/appsettings.json +++ b/Backend/Api/Api/appsettings.json @@ -5,5 +5,11 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "DatabaseSettings": { + + "ConnectionString": "mongodb://127.0.0.1:27017/", + "DatabaseName": "Odyssey" + + } } -- cgit v1.2.3