using SkyArtShop.Services; using SkyArtShop.Models; using System.Security.Cryptography; using System.Text; using Microsoft.EntityFrameworkCore; using Microsoft.AspNetCore.Identity; var builder = WebApplication.CreateBuilder(args); // Configure URLs based on environment if (builder.Environment.IsProduction()) { // Production: Listen on all interfaces, port 80 and 443 builder.WebHost.UseUrls("http://*:80"); } else { // Development: Listen only on localhost, port 5001 builder.WebHost.UseUrls("http://localhost:5001"); } // Add services to the container builder.Services.AddControllersWithViews(); builder.Services.AddRazorPages(); // Add EF Core (SQLite) for Identity (separate from MongoDB content storage) var identityConnection = builder.Configuration.GetConnectionString("IdentityConnection") ?? "Data Source=identity.db"; builder.Services.AddDbContext(options => options.UseSqlite(identityConnection)); // Add Identity builder.Services.AddIdentity(options => { options.Password.RequireDigit = false; options.Password.RequireLowercase = false; options.Password.RequireUppercase = false; options.Password.RequireNonAlphanumeric = false; options.Password.RequiredLength = 6; }) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); builder.Services.ConfigureApplicationCookie(options => { options.LoginPath = "/admin/login"; options.AccessDeniedPath = "/admin/login"; }); // Configure MongoDB builder.Services.Configure( builder.Configuration.GetSection("MongoDB")); builder.Services.AddSingleton(); builder.Services.AddSingleton(); // Add session support builder.Services.AddDistributedMemoryCache(); builder.Services.AddSession(options => { options.IdleTimeout = TimeSpan.FromHours(2); options.Cookie.HttpOnly = true; options.Cookie.IsEssential = true; }); // Add HttpContextAccessor builder.Services.AddHttpContextAccessor(); var app = builder.Build(); // Initialize database with default data await InitializeDatabase(app.Services); // Configure the HTTP request pipeline if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } // app.UseHttpsRedirection(); // Disabled for HTTP-only development app.UseStaticFiles(); app.UseRouting(); app.UseSession(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); async Task InitializeDatabase(IServiceProvider services) { using var scope = services.CreateScope(); var mongoService = scope.ServiceProvider.GetRequiredService(); var configuration = scope.ServiceProvider.GetRequiredService(); // Apply Identity migrations and seed admin user/role var dbContext = scope.ServiceProvider.GetRequiredService(); await dbContext.Database.EnsureCreatedAsync(); var roleManager = scope.ServiceProvider.GetRequiredService>(); var userManager = scope.ServiceProvider.GetRequiredService>(); const string adminRole = "Admin"; if (!await roleManager.RoleExistsAsync(adminRole)) { await roleManager.CreateAsync(new IdentityRole(adminRole)); } var adminEmail = configuration["AdminUser:Email"] ?? "admin@skyartshop.com"; var existingAdmin = await userManager.FindByEmailAsync(adminEmail); if (existingAdmin == null) { var adminUser = new SkyArtShop.Data.ApplicationUser { UserName = adminEmail, Email = adminEmail, DisplayName = configuration["AdminUser:Name"] ?? "Admin" }; var createResult = await userManager.CreateAsync(adminUser, configuration["AdminUser:Password"] ?? "Admin123!"); if (createResult.Succeeded) { await userManager.AddToRoleAsync(adminUser, adminRole); } } // Initialize site settings var settings = await mongoService.GetAllAsync("SiteSettings"); if (!settings.Any()) { var defaultSettings = new SiteSettings { SiteName = "Sky Art Shop", SiteTagline = "Scrapbooking and Journaling Fun", ContactEmail = "info@skyartshop.com", ContactPhone = "+501 608-0409", FooterText = "© 2035 by Sky Art Shop. Powered and secured by Wix", UpdatedAt = DateTime.UtcNow }; await mongoService.InsertAsync("SiteSettings", defaultSettings); } // Initialize default portfolio categories var categories = await mongoService.GetAllAsync("PortfolioCategories"); if (!categories.Any()) { var defaultCategories = new[] { new PortfolioCategory { Name = "Displays", Slug = "displays", Description = "Creative display projects showcasing our work", DisplayOrder = 1, IsActive = true }, new PortfolioCategory { Name = "Personal Craft Projects", Slug = "personal-craft-projects", Description = "Personal creative projects and handmade crafts", DisplayOrder = 2, IsActive = true }, new PortfolioCategory { Name = "Card Making Projects", Slug = "card-making", Description = "Handmade cards for every occasion", DisplayOrder = 3, IsActive = true }, new PortfolioCategory { Name = "Scrapbook Albums", Slug = "scrapbook-albums", Description = "Preserving memories through creative scrapbooking", DisplayOrder = 4, IsActive = true } }; foreach (var category in defaultCategories) { await mongoService.InsertAsync("PortfolioCategories", category); } } // Initialize About page var pages = await mongoService.GetAllAsync("Pages"); if (!pages.Any(p => p.PageSlug == "about")) { var aboutPage = new Page { PageName = "About", PageSlug = "about", Title = "About Sky Art Shop", Subtitle = "Creating moments, one craft at a time", Content = @"

Our Story

Sky Art Shop specializes in scrapbooking, journaling, cardmaking, and collaging stationery. We are passionate about helping people express their creativity and preserve their memories.

Our mission is to promote mental health and wellness through creative art activities. We believe that crafting is more than just a hobby—it's a therapeutic journey that brings joy, mindfulness, and self-expression.

What We Offer

  • Washi tape in various designs and patterns
  • Unique stickers for journaling and scrapbooking
  • High-quality journals and notebooks
  • Card making supplies and kits
  • Scrapbooking materials and embellishments
  • Collage papers and ephemera
", IsActive = true, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow }; await mongoService.InsertAsync("Pages", aboutPage); } // Initialize menu items var menuItems = await mongoService.GetAllAsync("MenuItems"); if (!menuItems.Any()) { var defaultMenuItems = new[] { new MenuItem { Label = "Home", Url = "/", DisplayOrder = 1, IsActive = true }, new MenuItem { Label = "Shop", Url = "/Shop", DisplayOrder = 2, IsActive = true }, new MenuItem { Label = "Top Sellers", Url = "/#top-sellers", DisplayOrder = 3, IsActive = true }, new MenuItem { Label = "Promotion", Url = "/#promotion", DisplayOrder = 4, IsActive = true }, new MenuItem { Label = "Portfolio", Url = "/Portfolio", DisplayOrder = 5, IsActive = true }, new MenuItem { Label = "Blog", Url = "/Blog", DisplayOrder = 6, IsActive = true }, new MenuItem { Label = "About", Url = "/About", DisplayOrder = 7, IsActive = true }, new MenuItem { Label = "Instagram", Url = "#instagram", DisplayOrder = 8, IsActive = true }, new MenuItem { Label = "Contact", Url = "/Contact", DisplayOrder = 9, IsActive = true }, new MenuItem { Label = "My Wishlist", Url = "#wishlist", DisplayOrder = 10, IsActive = true } }; foreach (var item in defaultMenuItems) { await mongoService.InsertAsync("MenuItems", item); } } // Initialize homepage sections var sections = await mongoService.GetAllAsync("HomepageSections"); if (!sections.Any()) { var defaultSections = new[] { new HomepageSection { SectionType = "hero", Title = "Scrapbooking and Journaling Fun", Subtitle = "Explore the world of creativity and self-expression.", ButtonText = "Shop Now", ButtonUrl = "/Shop", ImageUrl = "/assets/images/hero-craft.jpg", DisplayOrder = 0, IsActive = true }, new HomepageSection { SectionType = "inspiration", Title = "Our Inspiration", Content = @"

Sky Art Shop specializes in scrapbooking, journaling, cardmaking, and collaging stationery. We aim to promote mental health through creative art activities.

Our offerings include washi tape, stickers, journals, and more.

", ImageUrl = "/assets/images/craft-supplies.jpg", DisplayOrder = 1, IsActive = true }, new HomepageSection { SectionType = "promotion", Title = "Special Offers", Content = @"", DisplayOrder = 2, IsActive = true } }; foreach (var section in defaultSections) { await mongoService.InsertAsync("HomepageSections", section); } } }