Revize fc0e5b36
Přidáno uživatelem Vojtěch Bartička před asi 3 roky(ů)
Backend/Backend/Program.cs | ||
---|---|---|
18 | 18 |
builder.Services.AddEndpointsApiExplorer(); |
19 | 19 |
builder.Services.AddSwaggerGen(); |
20 | 20 |
|
21 |
// Database |
|
21 | 22 |
builder.Services.AddDbContext<DatabaseContext>(); |
22 | 23 |
|
23 | 24 |
var app = builder.Build(); |
24 | 25 |
|
26 |
// Enable DateTime in PostgreSQL |
|
25 | 27 |
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); |
26 | 28 |
|
27 | 29 |
|
... | ... | |
45 | 47 |
using (var scope = app.Services.CreateScope()) |
46 | 48 |
{ |
47 | 49 |
var context = scope.ServiceProvider.GetService<DatabaseContext>(); |
50 |
if (context == null) |
|
51 |
{ |
|
52 |
app.Logger.LogCritical("Could not initialize database context, shutting down."); |
|
53 |
Environment.Exit(101); |
|
54 |
} |
|
48 | 55 |
|
49 | 56 |
// In development we seed dummy data |
50 | 57 |
if (app.Environment.IsDevelopment()) |
Backend/Backend/RestAPI.csproj | ||
---|---|---|
24 | 24 |
<ProjectReference Include="..\Core\Core.csproj" /> |
25 | 25 |
</ItemGroup> |
26 | 26 |
|
27 |
<ItemGroup> |
|
28 |
<Folder Include="Controllers\" /> |
|
29 |
</ItemGroup> |
|
30 |
|
|
27 | 31 |
</Project> |
Backend/Core/Seeding/Seeder.cs | ||
---|---|---|
16 | 16 |
{ |
17 | 17 |
if (!context.Documents.Any()) |
18 | 18 |
{ |
19 |
DocumentContent content = new DocumentContent() { Content = "Dummy content" }; |
|
19 |
DocumentContent content = new DocumentContent() |
|
20 |
{ |
|
21 |
Content = "Dummy content" |
|
22 |
}; |
|
20 | 23 |
context.DocumentContents.Add(content); |
21 | 24 |
|
22 |
User user = new User() { Username = "testuser", Name = "Test", Surname = "User", Password = "password", Role = ERole.ANNOTATOR }; |
|
25 |
User user = new User() |
|
26 |
{ |
|
27 |
Username = "testuser", |
|
28 |
Name = "Test", |
|
29 |
Surname = "User", |
|
30 |
Password = "password", |
|
31 |
Role = ERole.ANNOTATOR |
|
32 |
}; |
|
23 | 33 |
context.Users.Add(user); |
24 | 34 |
|
25 |
Document document = new Document() { Content = content, Name = "Test document", DateAdded = DateTime.Now, Length = 10, RequiredAnnotations = 3, UserAdded = user }; |
|
35 |
Document document = new Document() |
|
36 |
{ |
|
37 |
Content = content, |
|
38 |
Name = "Test document", |
|
39 |
DateAdded = DateTime.Now, |
|
40 |
Length = 10, |
|
41 |
RequiredAnnotations = 3, |
|
42 |
UserAdded = user |
|
43 |
}; |
|
26 | 44 |
context.Documents.Add(document); |
27 | 45 |
context.SaveChanges(); |
28 | 46 |
|
... | ... | |
38 | 56 |
}; |
39 | 57 |
context.Annotations.Add(annotation); |
40 | 58 |
|
41 |
Class clss = new Class() { Color = "rgb(...)", Description = "Dummy description", Name = "Dummy class name" }; |
|
59 |
Class clss = new Class() |
|
60 |
{ |
|
61 |
Color = "rgb(...)", |
|
62 |
Description = "Dummy description", |
|
63 |
Name = "Dummy class name" |
|
64 |
}; |
|
42 | 65 |
context.Classes.Add(clss); |
43 | 66 |
|
44 | 67 |
annotation.Classes.Add(clss); |
45 | 68 |
|
46 |
TagCategory category = new TagCategory() { Name = "Dummy category", Color = "rgb(...)", Description = "Dummy category description" }; |
|
69 |
TagCategory category = new TagCategory() |
|
70 |
{ |
|
71 |
Name = "Dummy category", |
|
72 |
Color = "rgb(...)", |
|
73 |
Description = "Dummy category description" |
|
74 |
}; |
|
47 | 75 |
context.TagCategories.Add(category); |
48 | 76 |
|
49 |
Tag tag = new Tag() { Name = "Dummy tag", Category = category, Description = "Dummy tag", Color = "rgb(...)" }; |
|
77 |
Tag tag = new Tag() |
|
78 |
{ |
|
79 |
Name = "Dummy tag", |
|
80 |
Category = category, |
|
81 |
Description = "Dummy tag", |
|
82 |
Color = "rgb(...)" |
|
83 |
}; |
|
50 | 84 |
context.Tags.Add(tag); |
51 |
SubTag subTag = new SubTag() { Description = "Dummy subtag", Name = "Dummy subtag", Tag = tag }; |
|
85 |
SubTag subTag = new SubTag() |
|
86 |
{ |
|
87 |
Description = "Dummy subtag", |
|
88 |
Name = "Dummy subtag", |
|
89 |
Tag = tag |
|
90 |
}; |
|
52 | 91 |
context.SubTags.Add(subTag); |
53 | 92 |
|
54 |
AnnotationTag annotationTag = new AnnotationTag() { Annotation = annotation, Instance = 0, Length = 1, Position = 0, Tag = tag, Note = "Dummy note" }; |
|
93 |
AnnotationTag annotationTag = new AnnotationTag() |
|
94 |
{ |
|
95 |
Annotation = annotation, |
|
96 |
Instance = 0, |
|
97 |
Length = 1, |
|
98 |
Position = 0, |
|
99 |
Tag = tag, |
|
100 |
Note = "Dummy note" |
|
101 |
}; |
|
55 | 102 |
context.AnnotationTags.Add(annotationTag); |
56 | 103 |
|
57 | 104 |
context.SaveChanges(); |
... | ... | |
62 | 109 |
{ |
63 | 110 |
if (!context.Documents.Any()) |
64 | 111 |
{ |
65 |
User admin = new User() { Name = "Admin", Surname = "Admin", Role = ERole.ADMINISTRATOR, Username = "admin", Password = "123a456b789c" }; |
|
112 |
User admin = new User() |
|
113 |
{ |
|
114 |
Name = "Admin", |
|
115 |
Surname = "Admin", |
|
116 |
Role = ERole.ADMINISTRATOR, |
|
117 |
Username = "admin", |
|
118 |
Password = "123a456b789c" |
|
119 |
}; |
|
66 | 120 |
context.Users.Add(admin); |
67 | 121 |
context.SaveChanges(); |
68 | 122 |
} |
Backend/Core/Services/UserService/UserServiceEF.cs | ||
---|---|---|
49 | 49 |
} |
50 | 50 |
return u; |
51 | 51 |
} |
52 |
catch (Exception ex)
|
|
52 |
catch (Exception) |
|
53 | 53 |
{ |
54 | 54 |
_logger.Information($"No user with username {username} found."); |
55 | 55 |
return null; |
... | ... | |
65 | 65 |
return null; |
66 | 66 |
} |
67 | 67 |
|
68 |
User user = new User() { Username = username, Name = name, Surname = surname, Password = BCrypt.Net.BCrypt.HashPassword(password), Role = role }; |
|
68 |
User user = new User() |
|
69 |
{ |
|
70 |
Username = username, |
|
71 |
Name = name, |
|
72 |
Surname = surname, |
|
73 |
Password = BCrypt.Net.BCrypt.HashPassword(password), |
|
74 |
Role = role |
|
75 |
}; |
|
69 | 76 |
_databaseContext.Users.Add(user); |
70 | 77 |
_databaseContext.SaveChanges(); |
71 | 78 |
return user; |
... | ... | |
79 | 86 |
User user = _databaseContext.Users.First(u => u.Username == username); |
80 | 87 |
return user; |
81 | 88 |
} |
82 |
catch (InvalidOperationException ex)
|
|
89 |
catch (InvalidOperationException) |
|
83 | 90 |
{ |
84 | 91 |
_logger.Warning($"No user with the username {username} found."); |
85 | 92 |
return null; |
... | ... | |
94 | 101 |
User user = _databaseContext.Users.First(u => u.Id == id); |
95 | 102 |
return user; |
96 | 103 |
} |
97 |
catch (InvalidOperationException ex)
|
|
104 |
catch (InvalidOperationException) |
|
98 | 105 |
{ |
99 | 106 |
_logger.Warning($"No user with the GUID {id} found."); |
100 | 107 |
return null; |
Také k dispozici: Unified diff
Changed formatting, added comments, database check