-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
303 lines (256 loc) · 13.2 KB
/
Program.cs
File metadata and controls
303 lines (256 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using ECommerce.API.Data;
using ECommerce.API.Helpers;
using ECommerce.API.Interfaces;
using ECommerce.API.Middleware;
using ECommerce.API.Models;
using ECommerce.API.Repositories;
using ECommerce.API.Services;
using FluentAssertions.Common;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using System.Text;
using EmailService = ECommerce.API.Services.EmailService;
namespace ECommerce.API
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve;
});
builder.Services.AddEndpointsApiExplorer();
// Configure Swagger
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Chocolate E-Commerce API",
Version = "v1",
Description = "API for managing a chocolate e-commerce store",
Contact = new OpenApiContact
{
Name = "Eliezer Kibet",
Email = "elieserkibet@gmail.com"
}
});
// Add JWT authentication to Swagger
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
});
// Register DbContext as scoped (for regular operations)
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Create a custom DbContextFactory that doesn't depend on singleton-scoped mismatch
builder.Services.AddScoped<IDbContextFactory<ApplicationDbContext>>(serviceProvider =>
{
// Get the options from the service provider
var options = serviceProvider.GetRequiredService<DbContextOptions<ApplicationDbContext>>();
return new CustomDbContextFactory(options);
});
// Then update the Analytics service registration
builder.Services.AddScoped<IAnalyticsService, AnalyticsService>();
// Add Identity services
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
// Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// User settings
options.User.RequireUniqueEmail = true;
options.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Register repositories
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
// Register services
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddScoped<ICartService, CartService>();
builder.Services.AddScoped<IEmailService, EmailService>();
// Add AutoMapper
builder.Services.AddAutoMapper(typeof(MappingProfiles));
// Configure static files for image uploads
builder.Services.AddDirectoryBrowser();
// Configure CORS - FIXED: Remove duplicate CORS configuration
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowNextJS", policy =>
{
policy.WithOrigins("http://localhost:3000", "https://localhost:3000")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
// Keep your existing policy as well
options.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddScoped<IReviewService, ReviewService>();
builder.Services.AddTransient<CartPatchService>();
builder.Services.AddScoped<IShippingAddressService, ShippingAddressService>();
builder.Services.AddScoped<ICouponService, CouponService>();
builder.Services.AddScoped<IPromotionService, PromotionService>();
builder.Services.AddScoped<ISearchService, SearchService>();
// Configure JWT Authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "Chocolate E-Commerce",
ValidAudience = "Customers",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
"CHOCOLATEECOMMERCESUPERSECRETAPIKEYTHATNOONEKNOWSANDSHOUDLNOTSHARE"))
};
// Add this for debugging
options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
Console.WriteLine($"JWT Authentication failed: {context.Exception.Message}");
return Task.CompletedTask;
},
OnTokenValidated = context =>
{
Console.WriteLine("JWT Token validated successfully");
return Task.CompletedTask;
}
};
});
// Add authorization
builder.Services.AddAuthorization();
// FIXED: Build the app AFTER all services are configured
var app = builder.Build();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Chocolate API v1");
c.RoutePrefix = "swagger"; // This ensures swagger is available at /swagger
});
// Add redirect from root to Swagger
app.MapGet("/", () => Results.Redirect("/swagger"));
}
app.UseHttpsRedirection();
// Configure static files for uploads
app.UseStaticFiles();
// Create uploads directory if it doesn't exist
var uploadsPath = Path.Combine(builder.Environment.WebRootPath ??
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"), "uploads");
if (!Directory.Exists(uploadsPath))
{
Directory.CreateDirectory(uploadsPath);
}
// Configure directory browsing for uploads folder
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = new PhysicalFileProvider(uploadsPath),
RequestPath = "/uploads"
});
// FIXED: Use CORS middleware in correct order
app.UseCors("AllowNextJS");
// Guest cart middleware should be before authentication
app.UseGuestCart();
// Authentication and Authorization middleware
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
// Seed the database
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<ApplicationDbContext>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
await DataSeeder.SeedData(context, roleManager, userManager);
// Execute raw SQL to add missing columns if they don't exist
context.Database.ExecuteSqlRaw(@"
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'ShippingName')
ALTER TABLE Orders ADD ShippingName NVARCHAR(MAX) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'ShippingAddressLine1')
ALTER TABLE Orders ADD ShippingAddressLine1 NVARCHAR(MAX) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'ShippingAddressLine2')
ALTER TABLE Orders ADD ShippingAddressLine2 NVARCHAR(MAX) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'ShippingCity')
ALTER TABLE Orders ADD ShippingCity NVARCHAR(MAX) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'ShippingState')
ALTER TABLE Orders ADD ShippingState NVARCHAR(MAX) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'ShippingZipCode')
ALTER TABLE Orders ADD ShippingZipCode NVARCHAR(MAX) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'ShippingCountry')
ALTER TABLE Orders ADD ShippingCountry NVARCHAR(MAX) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'ShippingPhoneNumber')
ALTER TABLE Orders ADD ShippingPhoneNumber NVARCHAR(MAX) NULL;
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID('Orders') AND name = 'PaymentMethod')
ALTER TABLE Orders ADD PaymentMethod NVARCHAR(MAX) NULL;
");
Console.WriteLine("Database schema updated with shipping columns");
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred during database seeding");
}
}
// Run the application
await app.RunAsync();
}
}
}