mfoud444 commited on
Commit
a949060
·
1 Parent(s): 8e86c56

first commit

Browse files
Files changed (2) hide show
  1. src/Database/DatabaseContext.cs +9 -3
  2. src/Program.cs +34 -30
src/Database/DatabaseContext.cs CHANGED
@@ -16,8 +16,8 @@ namespace Backend_Teamwork.src.Database
16
  public DbSet<User> User { get; set; }
17
  public DbSet<Booking> Booking { get; set; }
18
 
19
- public DatabaseContext(DbContextOptions option)
20
- : base(option) { }
21
 
22
  protected override void OnModelCreating(ModelBuilder modelBuilder)
23
  {
@@ -46,5 +46,11 @@ namespace Backend_Teamwork.src.Database
46
  }
47
  );
48
  }
 
 
 
 
 
 
49
  }
50
- }
 
16
  public DbSet<User> User { get; set; }
17
  public DbSet<Booking> Booking { get; set; }
18
 
19
+ public DatabaseContext(DbContextOptions<DatabaseContext> options)
20
+ : base(options) { }
21
 
22
  protected override void OnModelCreating(ModelBuilder modelBuilder)
23
  {
 
46
  }
47
  );
48
  }
49
+
50
+ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
51
+ {
52
+ optionsBuilder.ConfigureWarnings(w =>
53
+ w.Ignore(Microsoft.EntityFrameworkCore.Diagnostics.CoreEventId.ManyServiceProvidersCreatedWarning));
54
+ }
55
  }
56
+ }
src/Program.cs CHANGED
@@ -13,17 +13,16 @@ using Backend_Teamwork.src.Services.workshop;
13
  using Backend_Teamwork.src.Utils;
14
  using Microsoft.AspNetCore.Authentication.JwtBearer;
15
  using Microsoft.EntityFrameworkCore;
16
- using Microsoft.Extensions.Logging;
17
  using Microsoft.IdentityModel.Tokens;
18
  using Npgsql;
19
  using static Backend_Teamwork.src.Entities.User;
20
 
21
  var builder = WebApplication.CreateBuilder(args);
22
 
23
- // Configure logging as a singleton to prevent multiple service providers from being created
24
- builder.Services.AddSingleton<ILoggerFactory, LoggerFactory>();
25
- var loggerFactory = builder.Services.BuildServiceProvider().GetRequiredService<ILoggerFactory>();
26
-
27
 
28
  // Database configuration
29
  var dataSourceBuilder = new NpgsqlDataSourceBuilder(
@@ -32,27 +31,35 @@ var dataSourceBuilder = new NpgsqlDataSourceBuilder(
32
  dataSourceBuilder.MapEnum<UserRole>();
33
  dataSourceBuilder.MapEnum<Status>();
34
 
35
- // Configure DbContext to use the logger factory
36
  builder.Services.AddDbContext<DatabaseContext>(options =>
37
  options.UseNpgsql(dataSourceBuilder.Build())
38
- .UseLoggerFactory(loggerFactory));
 
 
 
39
 
40
  // Add AutoMapper
41
  builder.Services.AddAutoMapper(typeof(MapperProfile).Assembly);
42
 
43
- // Register services
44
- builder.Services.AddScoped<ICategoryService, CategoryService>()
45
- .AddScoped<CategoryRepository>();
46
- builder.Services.AddScoped<IArtworkService, ArtworkService>()
47
- .AddScoped<ArtworkRepository>();
48
- builder.Services.AddScoped<IUserService, UserService>()
49
- .AddScoped<UserRepository>();
50
- builder.Services.AddScoped<IOrderService, OrderService>()
51
- .AddScoped<OrderRepository>();
52
- builder.Services.AddScoped<IWorkshopService, WorkshopService>()
53
- .AddScoped<WorkshopRepository>();
54
- builder.Services.AddScoped<IBookingService, BookingService>()
55
- .AddScoped<BookingRepository>();
 
 
 
 
 
56
 
57
  // Configure Authentication
58
  builder.Services.AddAuthentication(options =>
@@ -60,9 +67,9 @@ builder.Services.AddAuthentication(options =>
60
  options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
61
  options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
62
  })
63
- .AddJwtBearer(Options =>
64
  {
65
- Options.TokenValidationParameters = new TokenValidationParameters
66
  {
67
  ValidateIssuer = true,
68
  ValidateAudience = true,
@@ -88,8 +95,8 @@ builder.Services.AddCors(options =>
88
  {
89
  options.AddPolicy("AllowAll",
90
  policyBuilder => policyBuilder.AllowAnyOrigin()
91
- .AllowAnyHeader()
92
- .AllowAnyMethod());
93
  });
94
 
95
  // Configure Controllers
@@ -104,7 +111,7 @@ var app = builder.Build();
104
  // Configure timestamp format
105
  AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
106
 
107
- // Test database connection
108
  // using (var scope = app.Services.CreateScope())
109
  // {
110
  // var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
@@ -119,9 +126,6 @@ AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
119
  // {
120
  // Console.WriteLine("Unable to connect to the database.");
121
  // }
122
-
123
-
124
-
125
  // }
126
  // catch (Exception ex)
127
  // {
@@ -138,7 +142,7 @@ if (app.Environment.IsDevelopment())
138
 
139
  app.UseHttpsRedirection();
140
  app.UseRouting();
141
- // app.UseCors("AllowAll"); // Add this line to enable CORS
142
 
143
  app.UseMiddleware<ErrorHandlerMiddleware>();
144
  app.UseAuthentication();
@@ -147,4 +151,4 @@ app.UseAuthorization();
147
  app.MapControllers();
148
  app.MapGet("/", () => "Server is running");
149
 
150
- app.Run();
 
13
  using Backend_Teamwork.src.Utils;
14
  using Microsoft.AspNetCore.Authentication.JwtBearer;
15
  using Microsoft.EntityFrameworkCore;
 
16
  using Microsoft.IdentityModel.Tokens;
17
  using Npgsql;
18
  using static Backend_Teamwork.src.Entities.User;
19
 
20
  var builder = WebApplication.CreateBuilder(args);
21
 
22
+ // Configure logging
23
+ builder.Logging.ClearProviders();
24
+ builder.Logging.AddConsole();
25
+ builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
26
 
27
  // Database configuration
28
  var dataSourceBuilder = new NpgsqlDataSourceBuilder(
 
31
  dataSourceBuilder.MapEnum<UserRole>();
32
  dataSourceBuilder.MapEnum<Status>();
33
 
34
+ // Configure DbContext with warning suppression
35
  builder.Services.AddDbContext<DatabaseContext>(options =>
36
  options.UseNpgsql(dataSourceBuilder.Build())
37
+ .ConfigureWarnings(w =>
38
+ {
39
+ w.Ignore(Microsoft.EntityFrameworkCore.Diagnostics.CoreEventId.ManyServiceProvidersCreatedWarning);
40
+ }));
41
 
42
  // Add AutoMapper
43
  builder.Services.AddAutoMapper(typeof(MapperProfile).Assembly);
44
 
45
+ // Register services and repositories
46
+ builder.Services.AddScoped<ICategoryService, CategoryService>();
47
+ builder.Services.AddScoped<CategoryRepository>();
48
+
49
+ builder.Services.AddScoped<IArtworkService, ArtworkService>();
50
+ builder.Services.AddScoped<ArtworkRepository>();
51
+
52
+ builder.Services.AddScoped<IUserService, UserService>();
53
+ builder.Services.AddScoped<UserRepository>();
54
+
55
+ builder.Services.AddScoped<IOrderService, OrderService>();
56
+ builder.Services.AddScoped<OrderRepository>();
57
+
58
+ builder.Services.AddScoped<IWorkshopService, WorkshopService>();
59
+ builder.Services.AddScoped<WorkshopRepository>();
60
+
61
+ builder.Services.AddScoped<IBookingService, BookingService>();
62
+ builder.Services.AddScoped<BookingRepository>();
63
 
64
  // Configure Authentication
65
  builder.Services.AddAuthentication(options =>
 
67
  options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
68
  options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
69
  })
70
+ .AddJwtBearer(options =>
71
  {
72
+ options.TokenValidationParameters = new TokenValidationParameters
73
  {
74
  ValidateIssuer = true,
75
  ValidateAudience = true,
 
95
  {
96
  options.AddPolicy("AllowAll",
97
  policyBuilder => policyBuilder.AllowAnyOrigin()
98
+ .AllowAnyHeader()
99
+ .AllowAnyMethod());
100
  });
101
 
102
  // Configure Controllers
 
111
  // Configure timestamp format
112
  AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
113
 
114
+ // Optional: Database connection test and migration (uncomment if needed)
115
  // using (var scope = app.Services.CreateScope())
116
  // {
117
  // var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
 
126
  // {
127
  // Console.WriteLine("Unable to connect to the database.");
128
  // }
 
 
 
129
  // }
130
  // catch (Exception ex)
131
  // {
 
142
 
143
  app.UseHttpsRedirection();
144
  app.UseRouting();
145
+ app.UseCors("AllowAll");
146
 
147
  app.UseMiddleware<ErrorHandlerMiddleware>();
148
  app.UseAuthentication();
 
151
  app.MapControllers();
152
  app.MapGet("/", () => "Server is running");
153
 
154
+ app.Run();