Projekt

Obecné

Profil

Stáhnout (7.61 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
                Tag = tag
52
            };
53
            context.AnnotationTags.Add(at);
54 ebb51a7f Vojtěch Bartička
        }
55
56 09b22f6f Vojtěch Bartička
        private static void AddDocumentAndAnnotation(DatabaseContext context)
57
        {
58
            var adminUser = context.Users.Where(u => u.Role == ERole.ADMINISTRATOR).First();
59
            var normalUser = context.Users.Where(u => u.Role == ERole.ANNOTATOR).First();
60
61
            // Add documents
62
            DocumentContent dctext = new DocumentContent()
63
            {
64
                Content = "sample document content of TEXT file"
65
            };
66
            context.DocumentContents.Add(dctext);
67
68
            Document dtext = new()
69
            {
70
                Content = dctext,
71
                Name = "Sample TEXT document",
72
                DateAdded = DateTime.Now,
73
                UserAdded = adminUser,
74
                Length = dctext.Content.Length,
75
                RequiredAnnotations = 3
76
            };
77
            context.Documents.Add(dtext);
78
79
            DocumentContent dchtml = new DocumentContent()
80
            {
81
                Content = "<!DOCTYPE html><html>sample document content of HTML file</html>"
82
            };
83
            context.DocumentContents.Add(dchtml);
84
85
            Document dhtml = new()
86
            {
87
                Content = dchtml,
88
                Name = "Sample TEXT document",
89
                DateAdded = DateTime.Now,
90
                UserAdded = adminUser,
91
                Length = dchtml.Content.Length,
92
                RequiredAnnotations = 3
93
            };
94
            context.Documents.Add(dhtml);
95
96
            // Create user and admin annotation
97
            Annotation annotationAdmin = new Annotation()
98
            {
99
                State = EState.NEW,
100
                DateAssigned = DateTime.Now,
101
                DateLastChanged = DateTime.Now,
102
                Document = dtext,
103
                Note = "sample note",
104
                User = adminUser,
105
                UserAssigned = adminUser,
106
            };
107
            Annotation annotationUser = new Annotation()
108
            {
109
                State = EState.NEW,
110
                DateAssigned = DateTime.Now,
111
                DateLastChanged = DateTime.Now,
112
                Document = dhtml,
113
                Note = "sample note",
114
                User = normalUser,
115
                UserAssigned = adminUser,
116
            };
117
            context.Annotations.Add(annotationUser);
118
            context.Annotations.Add(annotationAdmin);
119
120
            context.SaveChanges();
121
        }
122
123 ebb51a7f Vojtěch Bartička
        public static void SeedProduction(DatabaseContext context)
124
        {
125 b8028d82 Vojtěch Bartička
            if (!context.Users.Any(u => u.Role == ERole.ADMINISTRATOR))
126 ebb51a7f Vojtěch Bartička
            {
127 fc0e5b36 Vojtěch Bartička
                User admin = new User()
128
                {
129
                    Name = "Admin",
130
                    Surname = "Admin",
131
                    Role = ERole.ADMINISTRATOR,
132
                    Username = "admin",
133 c99a6c25 Lukáš Vlček
                    Password = BCrypt.Net.BCrypt.HashPassword("123a456b789c")
134 fc0e5b36 Vojtěch Bartička
                };
135 ebb51a7f Vojtěch Bartička
                context.Users.Add(admin);
136 ceb95b98 Vojtěch Bartička
137
                ConfigurationItem configurationItem = new ConfigurationItem()
138
                {
139
                    Key = Constants.Constants.RequiredAnnotationsKey,
140
                    Value = "3"
141
                };
142
                context.ConfigurationItems.Add(configurationItem);
143
144 ebb51a7f Vojtěch Bartička
                context.SaveChanges();
145
            }
146
        }
147 204bd181 Vojtěch Bartička
148
        private static List<Class> AddClasses(DatabaseContext context)
149
        {
150
            List<Class> classes = new List<Class>();
151
            Class class1 = new Class()
152
            {
153
                Name = "Class1",
154
                Description = "Class1 description",
155
                Color = "Class 1 color"
156
            };
157
            context.Classes.Add(class1);
158
            classes.Add(class1);
159
            return classes;
160
        }
161
162
        private static List<User> AddUsers(DatabaseContext context)
163
        {
164
            List<User> users = new List<User>();
165
            User annotator1 = new User()
166
            {
167
                Username = "annotator1",
168
                Name = "Anno",
169
                Surname = "Tator1",
170
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
171
                Role = ERole.ANNOTATOR
172
            };
173
            context.Users.Add(annotator1);
174
            users.Add(annotator1);
175
176
            User annotator2 = new User()
177
            {
178
                Username = "annotator2",
179
                Name = "Anno",
180
                Surname = "Tator2",
181
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
182
                Role = ERole.ANNOTATOR
183
            };
184
            context.Users.Add(annotator2);
185
            users.Add(annotator2);
186
187
            User annotator3 = new User()
188
            {
189
                Username = "annotator3",
190
                Name = "Anno",
191
                Surname = "Tator3",
192
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
193
                Role = ERole.ANNOTATOR
194
            };
195
            context.Users.Add(annotator3);
196
            users.Add(annotator3);
197
198
            User annotator4 = new User()
199
            {
200
                Username = "annotator4",
201
                Name = "Anno",
202
                Surname = "Tator4",
203
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
204
                Role = ERole.ANNOTATOR
205
            };
206
            context.Users.Add(annotator4);
207
            users.Add(annotator4);
208
209
            User annotator5 = new User()
210
            {
211
                Username = "annotator5",
212
                Name = "Anno",
213
                Surname = "Tator5",
214
                Password = BCrypt.Net.BCrypt.HashPassword("password"),
215
                Role = ERole.ANNOTATOR
216
            };
217
            context.Users.Add(annotator5);
218
            users.Add(annotator5);
219
220
            User admin = new User()
221
            {
222
                Username = "admin",
223
                Name = "Admin",
224
                Surname = "User",
225
                Password = BCrypt.Net.BCrypt.HashPassword("admin"),
226
                Role = ERole.ADMINISTRATOR
227
            };
228
            context.Users.Add(admin);
229
            users.Add(admin);
230
231
            return users;
232
        }
233 ebb51a7f Vojtěch Bartička
    }
234 c99a6c25 Lukáš Vlček
}