1
|
using Core.Contexts;
|
2
|
using Core.Entities;
|
3
|
using Models.Enums;
|
4
|
using Microsoft.EntityFrameworkCore;
|
5
|
using System;
|
6
|
using System.Collections.Generic;
|
7
|
using System.Linq;
|
8
|
using System.Text;
|
9
|
using System.Threading.Tasks;
|
10
|
|
11
|
namespace Core.Seeding
|
12
|
{
|
13
|
public class Seeder
|
14
|
{
|
15
|
public static void SeedDevelopment(DatabaseContext context)
|
16
|
{
|
17
|
if (!context.Users.Any(u => u.Role == ERole.ADMINISTRATOR))
|
18
|
{
|
19
|
var users = AddUsers(context);
|
20
|
var classes = AddClasses(context);
|
21
|
DummyTags.AddDummyTags(context);
|
22
|
context.SaveChanges();
|
23
|
|
24
|
AddTagInstance(context);
|
25
|
context.SaveChanges();
|
26
|
}
|
27
|
}
|
28
|
|
29
|
private static void AddTagInstance(DatabaseContext context)
|
30
|
{
|
31
|
AddDocumentAndAnnotation(context);
|
32
|
|
33
|
var tag = context.Tags.Where(t => context.SubTags.Any(st => st.Tag == t)).First();
|
34
|
var subtag = context.SubTags.Where(st => st.Tag == tag).First();
|
35
|
var annotation = context.Annotations.First();
|
36
|
AnnotationTag at = new AnnotationTag()
|
37
|
{
|
38
|
Annotation = annotation,
|
39
|
Instance = 1,
|
40
|
Length = 10,
|
41
|
Position = 0,
|
42
|
SubTag = subtag,
|
43
|
Note = "asdasdasd",
|
44
|
Tag = tag
|
45
|
};
|
46
|
context.AnnotationTags.Add(at);
|
47
|
}
|
48
|
|
49
|
private static void AddDocumentAndAnnotation(DatabaseContext context)
|
50
|
{
|
51
|
var adminUser = context.Users.Where(u => u.Role == ERole.ADMINISTRATOR).First();
|
52
|
var normalUser = context.Users.Where(u => u.Role == ERole.ANNOTATOR).First();
|
53
|
|
54
|
// Add documents
|
55
|
DocumentContent dctext = new DocumentContent()
|
56
|
{
|
57
|
Content = "sample document content of TEXT file"
|
58
|
};
|
59
|
context.DocumentContents.Add(dctext);
|
60
|
|
61
|
Document dtext = new()
|
62
|
{
|
63
|
Content = dctext,
|
64
|
Name = "Sample TEXT document",
|
65
|
DateAdded = DateTime.Now,
|
66
|
UserAdded = adminUser,
|
67
|
Length = dctext.Content.Length,
|
68
|
RequiredAnnotations = 3
|
69
|
};
|
70
|
context.Documents.Add(dtext);
|
71
|
|
72
|
DocumentContent dchtml = new DocumentContent()
|
73
|
{
|
74
|
Content = "<!DOCTYPE html><html>sample document content of HTML file</html>"
|
75
|
};
|
76
|
context.DocumentContents.Add(dchtml);
|
77
|
|
78
|
Document dhtml = new()
|
79
|
{
|
80
|
Content = dchtml,
|
81
|
Name = "Sample TEXT document",
|
82
|
DateAdded = DateTime.Now,
|
83
|
UserAdded = adminUser,
|
84
|
Length = dchtml.Content.Length,
|
85
|
RequiredAnnotations = 3
|
86
|
};
|
87
|
context.Documents.Add(dhtml);
|
88
|
|
89
|
// Create user and admin annotation
|
90
|
Annotation annotationAdmin = new Annotation()
|
91
|
{
|
92
|
State = EState.NEW,
|
93
|
DateAssigned = DateTime.Now,
|
94
|
DateLastChanged = DateTime.Now,
|
95
|
Document = dtext,
|
96
|
Note = "sample note",
|
97
|
User = adminUser,
|
98
|
UserAssigned = adminUser,
|
99
|
};
|
100
|
Annotation annotationUser = new Annotation()
|
101
|
{
|
102
|
State = EState.NEW,
|
103
|
DateAssigned = DateTime.Now,
|
104
|
DateLastChanged = DateTime.Now,
|
105
|
Document = dhtml,
|
106
|
Note = "sample note",
|
107
|
User = normalUser,
|
108
|
UserAssigned = adminUser,
|
109
|
};
|
110
|
context.Annotations.Add(annotationUser);
|
111
|
context.Annotations.Add(annotationAdmin);
|
112
|
|
113
|
context.SaveChanges();
|
114
|
}
|
115
|
|
116
|
public static void SeedProduction(DatabaseContext context)
|
117
|
{
|
118
|
if (!context.Users.Any(u => u.Role == ERole.ADMINISTRATOR))
|
119
|
{
|
120
|
User admin = new User()
|
121
|
{
|
122
|
Name = "Admin",
|
123
|
Surname = "Admin",
|
124
|
Role = ERole.ADMINISTRATOR,
|
125
|
Username = "admin",
|
126
|
Password = BCrypt.Net.BCrypt.HashPassword("123a456b789c")
|
127
|
};
|
128
|
context.Users.Add(admin);
|
129
|
context.SaveChanges();
|
130
|
}
|
131
|
}
|
132
|
|
133
|
private static List<Class> AddClasses(DatabaseContext context)
|
134
|
{
|
135
|
List<Class> classes = new List<Class>();
|
136
|
Class class1 = new Class()
|
137
|
{
|
138
|
Name = "Class1",
|
139
|
Description = "Class1 description",
|
140
|
Color = "Class 1 color"
|
141
|
};
|
142
|
context.Classes.Add(class1);
|
143
|
classes.Add(class1);
|
144
|
return classes;
|
145
|
}
|
146
|
|
147
|
private static List<User> AddUsers(DatabaseContext context)
|
148
|
{
|
149
|
List<User> users = new List<User>();
|
150
|
User annotator1 = new User()
|
151
|
{
|
152
|
Username = "annotator1",
|
153
|
Name = "Anno",
|
154
|
Surname = "Tator1",
|
155
|
Password = BCrypt.Net.BCrypt.HashPassword("password"),
|
156
|
Role = ERole.ANNOTATOR
|
157
|
};
|
158
|
context.Users.Add(annotator1);
|
159
|
users.Add(annotator1);
|
160
|
|
161
|
User annotator2 = new User()
|
162
|
{
|
163
|
Username = "annotator2",
|
164
|
Name = "Anno",
|
165
|
Surname = "Tator2",
|
166
|
Password = BCrypt.Net.BCrypt.HashPassword("password"),
|
167
|
Role = ERole.ANNOTATOR
|
168
|
};
|
169
|
context.Users.Add(annotator2);
|
170
|
users.Add(annotator2);
|
171
|
|
172
|
User annotator3 = new User()
|
173
|
{
|
174
|
Username = "annotator3",
|
175
|
Name = "Anno",
|
176
|
Surname = "Tator3",
|
177
|
Password = BCrypt.Net.BCrypt.HashPassword("password"),
|
178
|
Role = ERole.ANNOTATOR
|
179
|
};
|
180
|
context.Users.Add(annotator3);
|
181
|
users.Add(annotator3);
|
182
|
|
183
|
User annotator4 = new User()
|
184
|
{
|
185
|
Username = "annotator4",
|
186
|
Name = "Anno",
|
187
|
Surname = "Tator4",
|
188
|
Password = BCrypt.Net.BCrypt.HashPassword("password"),
|
189
|
Role = ERole.ANNOTATOR
|
190
|
};
|
191
|
context.Users.Add(annotator4);
|
192
|
users.Add(annotator4);
|
193
|
|
194
|
User annotator5 = new User()
|
195
|
{
|
196
|
Username = "annotator5",
|
197
|
Name = "Anno",
|
198
|
Surname = "Tator5",
|
199
|
Password = BCrypt.Net.BCrypt.HashPassword("password"),
|
200
|
Role = ERole.ANNOTATOR
|
201
|
};
|
202
|
context.Users.Add(annotator5);
|
203
|
users.Add(annotator5);
|
204
|
|
205
|
User admin = new User()
|
206
|
{
|
207
|
Username = "admin",
|
208
|
Name = "Admin",
|
209
|
Surname = "User",
|
210
|
Password = BCrypt.Net.BCrypt.HashPassword("admin"),
|
211
|
Role = ERole.ADMINISTRATOR
|
212
|
};
|
213
|
context.Users.Add(admin);
|
214
|
users.Add(admin);
|
215
|
|
216
|
return users;
|
217
|
}
|
218
|
}
|
219
|
}
|