-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (103 loc) · 3.58 KB
/
Program.cs
File metadata and controls
120 lines (103 loc) · 3.58 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
using AppWithReact.Models;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddOpenApi();
// Configure MySQL Database
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseMySql(connectionString, ServerVersion.Parse("8.0.0")));
// Add CORS policy
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowReactApp",
builder =>
{
builder.WithOrigins("http://localhost:3001")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseCors("AllowReactApp");
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.MapControllers();
// Populate Database if empty
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<AppDbContext>();
// Use EnsureCreated instead of migrations
context.Database.EnsureCreated();
Console.WriteLine("✓ Database created successfully!");
// Add sample data if empty
if (!context.TodoLists.Any())
{
// Create default todo lists
var personalList = new TodoList
{
Name = "Personal Tasks",
Description = "Daily personal tasks and reminders",
CreatedAt = DateTime.UtcNow
};
var workList = new TodoList
{
Name = "Work Projects",
Description = "Office work and project tasks",
CreatedAt = DateTime.UtcNow
};
context.TodoLists.AddRange(personalList, workList);
await context.SaveChangesAsync();
// Add sample items to personal list
context.TodoItems.AddRange(
new TodoItem
{
Title = "Buy groceries",
TodoListId = personalList.Id,
CreatedAt = DateTime.UtcNow,
DueDate = DateTime.UtcNow.AddDays(1)
},
new TodoItem
{
Title = "Call dentist",
TodoListId = personalList.Id,
CreatedAt = DateTime.UtcNow
}
);
// Add sample items to work list
context.TodoItems.AddRange(
new TodoItem
{
Title = "Finish project report",
Description = "Complete the Q4 project analysis",
TodoListId = workList.Id,
CreatedAt = DateTime.UtcNow,
},
new TodoItem
{
Title = "Team meeting",
TodoListId = workList.Id,
CreatedAt = DateTime.UtcNow,
}
);
await context.SaveChangesAsync();
Console.WriteLine("✓ Added sample todo lists with items!");
}
}
catch (Exception ex)
{
Console.WriteLine($"✗ Error creating database: {ex.Message}");
if (ex.InnerException != null)
Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
}
}
app.Run();