first commit
Browse files- src/Database/DatabaseContext.cs +9 -3
- 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
|
20 |
-
: base(
|
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
|
24 |
-
builder.
|
25 |
-
|
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
|
36 |
builder.Services.AddDbContext<DatabaseContext>(options =>
|
37 |
options.UseNpgsql(dataSourceBuilder.Build())
|
38 |
-
.
|
|
|
|
|
|
|
39 |
|
40 |
// Add AutoMapper
|
41 |
builder.Services.AddAutoMapper(typeof(MapperProfile).Assembly);
|
42 |
|
43 |
-
// Register services
|
44 |
-
builder.Services.AddScoped<ICategoryService, CategoryService>()
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
builder.Services.AddScoped<
|
49 |
-
|
50 |
-
builder.Services.AddScoped<
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
builder.Services.AddScoped<
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
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(
|
64 |
{
|
65 |
-
|
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 |
-
|
92 |
-
|
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 |
-
//
|
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 |
-
|
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();
|