Projekt

Obecné

Profil

Stáhnout (7.63 KB) Statistiky
| Větev: | Tag: | Revize:
1 ebb51a7f Vojtěch Bartička
using Core.Contexts;
2
using Core.Entities;
3 cd91f841 Vojtěch Bartička
using Models.Enums;
4 ebb51a7f Vojtěch Bartička
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 b8028d82 Vojtěch Bartička
            if (!context.Users.Any(u => u.Role == ERole.ADMINISTRATOR))
18 ebb51a7f Vojtěch Bartička
            {
19 204bd181 Vojtěch Bartička
                var users = AddUsers(context);
20
                var classes = AddClasses(context);
21
                DummyTags.AddDummyTags(context);
22 ebb51a7f Vojtěch Bartička
                context.SaveChanges();
23 8b2a2c15 Vojtěch Bartička
24 ceb95b98 Vojtěch Bartička
                ConfigurationItem configurationItem = new ConfigurationItem()
25
                {
26
                    Key = Constants.Constants.RequiredAnnotationsKey,
27
                    Value = "3"
28
                };
29
                context.ConfigurationItems.Add(configurationItem);
30
31 8b2a2c15 Vojtěch Bartička
                AddTagInstance(context);
32
                context.SaveChanges();
33 ebb51a7f Vojtěch Bartička
            }
34 8b2a2c15 Vojtěch Bartička
        }
35
36
        private static void AddTagInstance(DatabaseContext context)
37
        {
38 09b22f6f Vojtěch Bartička
            AddDocumentAndAnnotation(context);
39
40 8b2a2c15 Vojtěch Bartička
            var tag = context.Tags.Where(t => context.SubTags.Any(st => st.Tag == t)).First();
41
            var subtag = context.SubTags.Where(st => st.Tag == tag).First();
42
            var annotation = context.Annotations.First();
43
            AnnotationTag at = new AnnotationTag()
44
            {
45
                Annotation = annotation,
46 be4deff8 Vojtěch Bartička
                Instance = Guid.NewGuid(),
47 8b2a2c15 Vojtěch Bartička
                Length = 10,
48
                Position = 0,
49
                SubTag = subtag,
50
                Note = "asdasdasd",
51 59022d41 Lukáš Vlček
                Tag = tag,
52
                SelectedText = "",
53 8b2a2c15 Vojtěch Bartička
            };
54
            context.AnnotationTags.Add(at);
55 ebb51a7f Vojtěch Bartička
        }
56
57 09b22f6f Vojtěch Bartička
        private static void AddDocumentAndAnnotation(DatabaseContext context)
58
        {
59
            var adminUser = context.Users.Where(u => u.Role == ERole.ADMINISTRATOR).First();
60
            var normalUser = context.Users.Where(u => u.Role == ERole.ANNOTATOR).First();
61
62
            // Add documents
63
            DocumentContent dctext = new DocumentContent()
64
            {
65
                Content = "sample document content of TEXT file"
66
            };
67
            context.DocumentContents.Add(dctext);
68
69
            Document dtext = new()
70
            {
71
                Content = dctext,
72
                Name = "Sample TEXT document",
73
                DateAdded = DateTime.Now,
74
                UserAdded = adminUser,
75
                Length = dctext.Content.Length,
76
                RequiredAnnotations = 3
77
            };
78
            context.Documents.Add(dtext);
79
80
            DocumentContent dchtml = new DocumentContent()
81
            {
82
                Content = "<!DOCTYPE html><html>sample document content of HTML file</html>"
83
            };
84
            context.DocumentContents.Add(dchtml);
85
86
            Document dhtml = new()
87
            {
88
                Content = dchtml,
89
                Name = "Sample TEXT document",
90
                DateAdded = DateTime.Now,
91
                UserAdded = adminUser,
92
                Length = dchtml.Content.Length,
93
                RequiredAnnotations = 3
94
            };
95
            context.Documents.Add(dhtml);
96
97
            // Create user and admin annotation
98
            Annotation annotationAdmin = new Annotation()
99
            {
100
                State = EState.NEW,
101
                DateAssigned = DateTime.Now,
102
                DateLastChanged = DateTime.Now,
103
                Document = dtext,
104
                Note = "sample note",
105
                User = adminUser,
106
                UserAssigned = adminUser,
107
            };
108
            Annotation annotationUser = new Annotation()
109
            {
110
                State = EState.NEW,
111
                DateAssigned = DateTime.Now,
112
                DateLastChanged = DateTime.Now,
113
                Document = dhtml,
114
                Note = "sample note",
115
                User = normalUser,
116
                UserAssigned = adminUser,
117
            };
118
            context.Annotations.Add(annotationUser);
119
            context.Annotations.Add(annotationAdmin);
120
121
            context.SaveChanges();
122
        }
123
124 ebb51a7f Vojtěch Bartička
        public static void SeedProduction(DatabaseContext context)
125
        {
126 b8028d82 Vojtěch Bartička
            if (!context.Users.Any(u => u.Role == ERole.ADMINISTRATOR))
127 ebb51a7f Vojtěch Bartička
            {
128 fc0e5b36 Vojtěch Bartička
                User admin = new User()
129
                {
130
                    Name = "Admin",
131
                    Surname = "Admin",
132
                    Role = ERole.ADMINISTRATOR,
133
                    Username = "admin",
134 fabfbe32 Jaroslav Hrubý
                    Password = BCrypt.Net.BCrypt.HashPassword("admin")
135 fc0e5b36 Vojtěch Bartička
                };
136 ebb51a7f Vojtěch Bartička
                context.Users.Add(admin);
137 ceb95b98 Vojtěch Bartička
138
                ConfigurationItem configurationItem = new ConfigurationItem()
139
                {
140
                    Key = Constants.Constants.RequiredAnnotationsKey,
141
                    Value = "3"
142
                };
143
                context.ConfigurationItems.Add(configurationItem);
144
145 ebb51a7f Vojtěch Bartička
                context.SaveChanges();
146
            }
147
        }
148 204bd181 Vojtěch Bartička
149
        private static List<Class> AddClasses(DatabaseContext context)
150
        {
151
            List<Class> classes = new List<Class>();
152
            Class class1 = new Class()
153
            {
154
                Name = "Class1",
155
                Description = "Class1 description",
156
                Color = "Class 1 color"
157
            };
158
            context.Classes.Add(class1);
159
            classes.Add(class1);
160
            return classes;
161
        }
162
163
        private static List<User> AddUsers(DatabaseContext context)
164
        {
165
            List<User> users = new List<User>();
166
            User annotator1 = new User()
167
            {
168
                Username = "annotator1",
169 cfd82637 Lukáš Vlček
                Name = "Karel",
170
                Surname = "Ann",
171 204bd181 Vojtěch Bartička
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
172
                Role = ERole.ANNOTATOR
173
            };
174
            context.Users.Add(annotator1);
175
            users.Add(annotator1);
176
177
            User annotator2 = new User()
178
            {
179
                Username = "annotator2",
180 cfd82637 Lukáš Vlček
                Name = "Luděk",
181
                Surname = "Ann",
182 204bd181 Vojtěch Bartička
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
183
                Role = ERole.ANNOTATOR
184
            };
185
            context.Users.Add(annotator2);
186
            users.Add(annotator2);
187
188
            User annotator3 = new User()
189
            {
190
                Username = "annotator3",
191 cfd82637 Lukáš Vlček
                Name = "Josef",
192
                Surname = "Ann",
193 204bd181 Vojtěch Bartička
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
194
                Role = ERole.ANNOTATOR
195
            };
196
            context.Users.Add(annotator3);
197
            users.Add(annotator3);
198
199
            User annotator4 = new User()
200
            {
201
                Username = "annotator4",
202 cfd82637 Lukáš Vlček
                Name = "Lucie",
203
                Surname = "Ann",
204 204bd181 Vojtěch Bartička
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
205
                Role = ERole.ANNOTATOR
206
            };
207
            context.Users.Add(annotator4);
208
            users.Add(annotator4);
209
210
            User annotator5 = new User()
211
            {
212
                Username = "annotator5",
213 cfd82637 Lukáš Vlček
                Name = "Jana",
214
                Surname = "Ann",
215 204bd181 Vojtěch Bartička
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
216
                Role = ERole.ANNOTATOR
217
            };
218
            context.Users.Add(annotator5);
219
            users.Add(annotator5);
220
221
            User admin = new User()
222
            {
223
                Username = "admin",
224
                Name = "Admin",
225
                Surname = "User",
226
                Password = BCrypt.Net.BCrypt.HashPassword("admin"),
227
                Role = ERole.ADMINISTRATOR
228
            };
229
            context.Users.Add(admin);
230
            users.Add(admin);
231
232
            return users;
233
        }
234 ebb51a7f Vojtěch Bartička
    }
235 c99a6c25 Lukáš Vlček
}