diff --git a/.gitignore b/.gitignore index 6769715..ceb0516 100644 --- a/.gitignore +++ b/.gitignore @@ -404,4 +404,13 @@ ASALocalRun/ # Local History for Visual Studio .localhistory/ - +# Environment files +.env +*.env.* +.env.local + +# App configuration secrets +appsettings.Development.json +appsettings.Staging.json +appsettings.Production.json +Secrets.json diff --git a/CommBank-Server/CommBank.csproj b/CommBank-Server/CommBank.csproj index 983cc88..95b03df 100644 --- a/CommBank-Server/CommBank.csproj +++ b/CommBank-Server/CommBank.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable CommBank_Server @@ -9,6 +9,8 @@ + + diff --git a/CommBank-Server/Models/Goal.cs b/CommBank-Server/Models/Goal.cs index 77ff1ad..b75e073 100644 --- a/CommBank-Server/Models/Goal.cs +++ b/CommBank-Server/Models/Goal.cs @@ -11,6 +11,8 @@ public class Goal public string? Name { get; set; } + public string? Icon { get; set; } // Icon added + public UInt64 TargetAmount { get; set; } = 0; public DateTime TargetDate { get; set; } @@ -27,4 +29,5 @@ public class Goal [BsonRepresentation(BsonType.ObjectId)] public string? UserId { get; set; } + } \ No newline at end of file diff --git a/CommBank-Server/Program.cs b/CommBank-Server/Program.cs index a88e560..819ddda 100644 --- a/CommBank-Server/Program.cs +++ b/CommBank-Server/Program.cs @@ -1,19 +1,37 @@ using CommBank.Models; using CommBank.Services; using MongoDB.Driver; +using DotNetEnv; var builder = WebApplication.CreateBuilder(args); -builder.Services.AddControllers(); +// Load .env file ONCE +Env.Load(); + +// Add environment variables to configuration +builder.Configuration.AddEnvironmentVariables(); +builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("Secrets.json"); +// Optional: Keep Secrets.json for fallback +builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("Secrets.json", optional: true); -var mongoClient = new MongoClient(builder.Configuration.GetConnectionString("CommBank")); -var mongoDatabase = mongoClient.GetDatabase("CommBank"); +// Get connection string (priority: env var -> Secrets.json -> appsettings.json) +var connectionString = Environment.GetEnvironmentVariable("MONGODB_URI") + ?? builder.Configuration.GetConnectionString("CommBank"); + +if (string.IsNullOrEmpty(connectionString)) +{ + throw new InvalidOperationException("MongoDB connection string is not configured. Set MONGODB_URI environment variable."); +} +var mongoClient = new MongoClient(connectionString); +var mongoDatabase = mongoClient.GetDatabase("commbank"); // FIXED: Use actual database name + +// Register services IAccountsService accountsService = new AccountsService(mongoDatabase); IAuthService authService = new AuthService(mongoDatabase); IGoalsService goalsService = new GoalsService(mongoDatabase); @@ -44,10 +62,6 @@ } app.UseHttpsRedirection(); - app.UseAuthorization(); - app.MapControllers(); - -app.Run(); - +app.Run(); \ No newline at end of file diff --git a/CommBank-Server/Secrets.json b/CommBank-Server/Secrets.json index 0e5bf94..31dac95 100644 --- a/CommBank-Server/Secrets.json +++ b/CommBank-Server/Secrets.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "CommBank": "{CONNECTION_STRING}" + "Commbank": [] } } \ No newline at end of file