Repository navigation
Expand file tree
/
Copy pathDatabaseSetup.cs
More file actions
191 lines (166 loc) · 8.55 KB
/
Copy pathDatabaseSetup.cs
File metadata and controls
191 lines (166 loc) · 8.55 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
using System;
using System.Data.SQLite;
using System.Security.Cryptography;
using System.Text;
namespace PreschoolManagement.Database
{
public static class DatabaseSetup
{
public static void InitializeDatabase(DatabaseManager dbManager)
{
CreateTables(dbManager);
InsertSampleData(dbManager);
}
private static void CreateTables(DatabaseManager dbManager)
{
// Create Users table
string createUsersTable = @"
CREATE TABLE IF NOT EXISTS Users (
UserId INTEGER PRIMARY KEY AUTOINCREMENT,
Username TEXT UNIQUE NOT NULL,
PasswordHash TEXT NOT NULL,
FirstName TEXT NOT NULL,
LastName TEXT NOT NULL,
Email TEXT,
Phone TEXT,
Role TEXT NOT NULL,
CreatedDate DATETIME DEFAULT CURRENT_TIMESTAMP
);";
dbManager.ExecuteNonQuery(createUsersTable);
// Create Children table
string createChildrenTable = @"
CREATE TABLE IF NOT EXISTS Children (
ChildId INTEGER PRIMARY KEY AUTOINCREMENT,
FirstName TEXT NOT NULL,
LastName TEXT NOT NULL,
BirthDate DATETIME NOT NULL,
Gender TEXT,
SpecialNotes TEXT,
GroupId INTEGER,
CreatedDate DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (GroupId) REFERENCES Groups(GroupId)
);";
dbManager.ExecuteNonQuery(createChildrenTable);
// Create Groups table
string createGroupsTable = @"
CREATE TABLE IF NOT EXISTS Groups (
GroupId INTEGER PRIMARY KEY AUTOINCREMENT,
GroupName TEXT NOT NULL,
TeacherId INTEGER,
Capacity INTEGER,
Description TEXT,
CreatedDate DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (TeacherId) REFERENCES Users(UserId)
);";
dbManager.ExecuteNonQuery(createGroupsTable);
// Create ParentChild relationships table
string createParentChildTable = @"
CREATE TABLE IF NOT EXISTS ParentChild (
ParentId INTEGER NOT NULL,
ChildId INTEGER NOT NULL,
Relationship TEXT,
IsEmergencyContact INTEGER DEFAULT 0,
PRIMARY KEY (ParentId, ChildId),
FOREIGN KEY (ParentId) REFERENCES Users(UserId),
FOREIGN KEY (ChildId) REFERENCES Children(ChildId)
);";
dbManager.ExecuteNonQuery(createParentChildTable);
// Create SystemLogs table
string createLogsTable = @"
CREATE TABLE IF NOT EXISTS SystemLogs (
LogId INTEGER PRIMARY KEY AUTOINCREMENT,
EventType TEXT NOT NULL,
UserId INTEGER,
Description TEXT NOT NULL,
LogDate DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (UserId) REFERENCES Users(UserId)
);";
dbManager.ExecuteNonQuery(createLogsTable);
}
private static void InsertSampleData(DatabaseManager dbManager)
{
// Hash function for passwords
string HashPassword(string password)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
return Convert.ToBase64String(bytes);
}
}
// Add sample users
string insertUserSql = @"
INSERT INTO Users (Username, PasswordHash, FirstName, LastName, Email, Phone, Role)
VALUES (@Username, @PasswordHash, @FirstName, @LastName, @Email, @Phone, @Role);";
// Admin user
dbManager.ExecuteNonQuery(insertUserSql,
new SQLiteParameter("@Username", "admin"),
new SQLiteParameter("@PasswordHash", HashPassword("admin")),
new SQLiteParameter("@FirstName", "Admin"),
new SQLiteParameter("@LastName", "System"),
new SQLiteParameter("@Email", "admin@preschool.com"),
new SQLiteParameter("@Phone", "123456789"),
new SQLiteParameter("@Role", "Administrator"));
// Teacher user
dbManager.ExecuteNonQuery(insertUserSql,
new SQLiteParameter("@Username", "teacher"),
new SQLiteParameter("@PasswordHash", HashPassword("teacher")),
new SQLiteParameter("@FirstName", "Anna"),
new SQLiteParameter("@LastName", "Kowalska"),
new SQLiteParameter("@Email", "anna@preschool.com"),
new SQLiteParameter("@Phone", "987654321"),
new SQLiteParameter("@Role", "Teacher"));
// Parent user
dbManager.ExecuteNonQuery(insertUserSql,
new SQLiteParameter("@Username", "parent"),
new SQLiteParameter("@PasswordHash", HashPassword("parent")),
new SQLiteParameter("@FirstName", "Jan"),
new SQLiteParameter("@LastName", "Nowak"),
new SQLiteParameter("@Email", "jan@example.com"),
new SQLiteParameter("@Phone", "555666777"),
new SQLiteParameter("@Role", "Parent"));
// Add sample group
string insertGroupSql = @"
INSERT INTO Groups (GroupName, TeacherId, Capacity, Description)
VALUES (@GroupName, @TeacherId, @Capacity, @Description);";
int teacherId = Convert.ToInt32(dbManager.ExecuteScalar("SELECT UserId FROM Users WHERE Username = 'teacher'"));
dbManager.ExecuteNonQuery(insertGroupSql,
new SQLiteParameter("@GroupName", "Słoneczka"),
new SQLiteParameter("@TeacherId", teacherId),
new SQLiteParameter("@Capacity", 15),
new SQLiteParameter("@Description", "Grupa dzieci 4-5 lat"));
// Add sample child
string insertChildSql = @"
INSERT INTO Children (FirstName, LastName, BirthDate, Gender, SpecialNotes, GroupId)
VALUES (@FirstName, @LastName, @BirthDate, @Gender, @SpecialNotes, @GroupId);";
int groupId = Convert.ToInt32(dbManager.ExecuteScalar("SELECT GroupId FROM Groups WHERE GroupName = 'Słoneczka'"));
dbManager.ExecuteNonQuery(insertChildSql,
new SQLiteParameter("@FirstName", "Zosia"),
new SQLiteParameter("@LastName", "Nowak"),
new SQLiteParameter("@BirthDate", DateTime.Parse("2019-05-15")),
new SQLiteParameter("@Gender", "Dziewczynka"),
new SQLiteParameter("@SpecialNotes", "Lubi rysować"),
new SQLiteParameter("@GroupId", groupId));
// Link parent to child
int parentId = Convert.ToInt32(dbManager.ExecuteScalar("SELECT UserId FROM Users WHERE Username = 'parent'"));
int childId = Convert.ToInt32(dbManager.ExecuteScalar("SELECT ChildId FROM Children WHERE FirstName = 'Zosia' AND LastName = 'Nowak'"));
string insertParentChildSql = @"
INSERT INTO ParentChild (ParentId, ChildId, Relationship, IsEmergencyContact)
VALUES (@ParentId, @ChildId, @Relationship, @IsEmergencyContact);";
dbManager.ExecuteNonQuery(insertParentChildSql,
new SQLiteParameter("@ParentId", parentId),
new SQLiteParameter("@ChildId", childId),
new SQLiteParameter("@Relationship", "Father"),
new SQLiteParameter("@IsEmergencyContact", 1));
// Add initial system log
string insertLogSql = @"
INSERT INTO SystemLogs (EventType, UserId, Description)
VALUES (@EventType, @UserId, @Description);";
int adminId = Convert.ToInt32(dbManager.ExecuteScalar("SELECT UserId FROM Users WHERE Username = 'admin'"));
dbManager.ExecuteNonQuery(insertLogSql,
new SQLiteParameter("@EventType", "System"),
new SQLiteParameter("@UserId", adminId),
new SQLiteParameter("@Description", "System initialized with sample data"));
}
}
}