Revize ceb95b98
Přidáno uživatelem Vojtěch Bartička před téměř 3 roky(ů)
Backend/Backend/Controllers/DocumentController.cs | ||
---|---|---|
127 | 127 |
|
128 | 128 |
return Ok(); |
129 | 129 |
} |
130 |
|
|
131 |
[HttpGet("/documents/requiredAnnotations/global")] |
|
132 |
[Authorize(Models.Enums.ERole.ADMINISTRATOR)] |
|
133 |
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GetRequiredAnnotationsGlobalResponse))] |
|
134 |
[ProducesResponseType((int)HttpStatusCode.Forbidden)] |
|
135 |
public ActionResult<GetRequiredAnnotationsGlobalResponse> GetRequiredAnnotationsGlobal([FromServices] ClientInfo clientInfo) |
|
136 |
{ |
|
137 |
if (clientInfo.LoggedUser == null) |
|
138 |
{ |
|
139 |
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents"); |
|
140 |
return Problem(); |
|
141 |
} |
|
142 |
|
|
143 |
try |
|
144 |
{ |
|
145 |
return Ok(new GetRequiredAnnotationsGlobalResponse() |
|
146 |
{ |
|
147 |
RequiredAnnotationsGlobal = documentService.GetRequiredAnnotationsGlobal() |
|
148 |
}); |
|
149 |
|
|
150 |
} |
|
151 |
catch (InvalidOperationException e) |
|
152 |
{ |
|
153 |
throw new BadRequestException(e.Message); |
|
154 |
} |
|
155 |
catch (Exception e) |
|
156 |
{ |
|
157 |
throw new InternalErrorException(e.Message); |
|
158 |
} |
|
159 |
} |
|
130 | 160 |
} |
131 | 161 |
|
Backend/Core/Constants/Constants.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
|
|
7 |
namespace Core.Constants |
|
8 |
{ |
|
9 |
public class Constants |
|
10 |
{ |
|
11 |
public static string RequiredAnnotationsKey { get; } = "RequiredAnnotations"; |
|
12 |
} |
|
13 |
} |
Backend/Core/Contexts/DatabaseContext.cs | ||
---|---|---|
23 | 23 |
public DbSet<User> Users { get; set; } |
24 | 24 |
public DbSet<Annotation> Annotations { get; set; } |
25 | 25 |
public DbSet<Class> Classes { get; set; } |
26 |
//public DbSet<AnnotationClass> AnnotationsClasses { get; set; } |
|
27 | 26 |
public DbSet<AnnotationTag> AnnotationTags { get; set; } |
28 | 27 |
public DbSet<Tag> Tags { get; set; } |
29 | 28 |
public DbSet<SubTag> SubTags { get; set; } |
30 | 29 |
public DbSet<TagCategory> TagCategories { get; set; } |
30 |
public DbSet<ConfigurationItem> ConfigurationItems { get; set; } |
|
31 | 31 |
|
32 | 32 |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
33 | 33 |
{ |
Backend/Core/Entities/ConfigurationItem.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
|
|
7 |
namespace Core.Entities |
|
8 |
{ |
|
9 |
public class ConfigurationItem : BaseEntity |
|
10 |
{ |
|
11 |
public string Value { get; set; } |
|
12 |
public string Key { get; set; } |
|
13 |
} |
|
14 |
} |
Backend/Core/Seeding/Seeder.cs | ||
---|---|---|
21 | 21 |
DummyTags.AddDummyTags(context); |
22 | 22 |
context.SaveChanges(); |
23 | 23 |
|
24 |
ConfigurationItem configurationItem = new ConfigurationItem() |
|
25 |
{ |
|
26 |
Key = Constants.Constants.RequiredAnnotationsKey, |
|
27 |
Value = "3" |
|
28 |
}; |
|
29 |
context.ConfigurationItems.Add(configurationItem); |
|
30 |
|
|
24 | 31 |
AddTagInstance(context); |
25 | 32 |
context.SaveChanges(); |
26 | 33 |
} |
... | ... | |
126 | 133 |
Password = BCrypt.Net.BCrypt.HashPassword("123a456b789c") |
127 | 134 |
}; |
128 | 135 |
context.Users.Add(admin); |
136 |
|
|
137 |
ConfigurationItem configurationItem = new ConfigurationItem() |
|
138 |
{ |
|
139 |
Key = Constants.Constants.RequiredAnnotationsKey, |
|
140 |
Value = "3" |
|
141 |
}; |
|
142 |
context.ConfigurationItems.Add(configurationItem); |
|
143 |
|
|
129 | 144 |
context.SaveChanges(); |
130 | 145 |
} |
131 | 146 |
} |
Backend/Core/Services/DocumentService/DocumentServiceEF.cs | ||
---|---|---|
12 | 12 |
using AutoMapper; |
13 | 13 |
using Models.Users; |
14 | 14 |
using Ganss.XSS; |
15 |
using Core.StaticConfig; |
|
16 | 15 |
|
17 | 16 |
namespace Core.Services.DocumentService |
18 | 17 |
{ |
... | ... | |
41 | 40 |
public void AddDocuments(DocumentAddRequest request, Guid userId) |
42 | 41 |
{ |
43 | 42 |
User user = databaseContext.Users.Single(u => u.Id == userId); |
44 |
|
|
45 |
var users = databaseContext.Users.Select(u => u).ToList(); |
|
43 |
int requiredAnnotations = int.Parse(databaseContext.ConfigurationItems.Single(ci => ci.Key == Constants.Constants.RequiredAnnotationsKey).Value); |
|
46 | 44 |
|
47 | 45 |
foreach (var documentInfo in request.Documents) |
48 | 46 |
{ |
... | ... | |
50 | 48 |
{ |
51 | 49 |
// TODO hardcoded UTF-8 - maybe do something smarter |
52 | 50 |
var documentContent = Encoding.UTF8.GetString(Convert.FromBase64String(documentInfo.Content)); |
53 |
SaveDocument(documentContent, user, documentInfo.Name, users);
|
|
51 |
SaveDocument(documentContent, user, documentInfo.Name, requiredAnnotations);
|
|
54 | 52 |
} |
55 | 53 |
else if (documentInfo.Format == Models.Enums.EAddDocumentFormat.ZIP) |
56 | 54 |
{ |
57 | 55 |
var (names, contents) = UnzipDocuments(documentInfo.Content); |
58 | 56 |
for (int i = 0; i < names.Count; i++) |
59 | 57 |
{ |
60 |
SaveDocument(contents[i], user, names[i], users);
|
|
58 |
SaveDocument(contents[i], user, names[i], requiredAnnotations);
|
|
61 | 59 |
} |
62 | 60 |
} |
63 | 61 |
} |
... | ... | |
92 | 90 |
return (names, contents); |
93 | 91 |
} |
94 | 92 |
|
95 |
private void SaveDocument(string content, User userAdded, string documentName, List<User> users)
|
|
93 |
private void SaveDocument(string content, User userAdded, string documentName, int requiredAnnotations)
|
|
96 | 94 |
{ |
97 | 95 |
DocumentContent documentContent = new DocumentContent() |
98 | 96 |
{ |
... | ... | |
106 | 104 |
UserAdded = userAdded, |
107 | 105 |
Name = documentName, |
108 | 106 |
Length = documentContent.Content.Length, |
109 |
RequiredAnnotations = GlobalConfiguration.RequiredAnnotations
|
|
107 |
RequiredAnnotations = requiredAnnotations
|
|
110 | 108 |
}; |
111 | 109 |
|
112 | 110 |
databaseContext.DocumentContents.Add(documentContent); |
... | ... | |
167 | 165 |
|
168 | 166 |
public void SetRequiredAnnotationsGlobal(int requiredAnnotations) |
169 | 167 |
{ |
170 |
GlobalConfiguration.RequiredAnnotations = requiredAnnotations; |
|
168 |
var requiredAnnotationsItem = databaseContext.ConfigurationItems.Single(ci => ci.Key == Constants.Constants.RequiredAnnotationsKey); |
|
169 |
requiredAnnotationsItem.Value = requiredAnnotations.ToString(); |
|
170 |
databaseContext.SaveChanges(); |
|
171 | 171 |
} |
172 | 172 |
|
173 |
public int GetRequiredAnnotationsGlobal() |
|
174 |
{ |
|
175 |
var requiredAnnotationsItem = databaseContext.ConfigurationItems.Single(ci => ci.Key == Constants.Constants.RequiredAnnotationsKey); |
|
176 |
return int.Parse(requiredAnnotationsItem.Value); |
|
177 |
} |
|
173 | 178 |
} |
174 | 179 |
} |
Backend/Core/Services/DocumentService/IDocumentService.cs | ||
---|---|---|
18 | 18 |
public void SetRequiredAnnotationsForDocuments(SetRequiredAnnotationsRequest request); |
19 | 19 |
|
20 | 20 |
public void SetRequiredAnnotationsGlobal(int requiredAnnotations); |
21 |
|
|
22 |
public int GetRequiredAnnotationsGlobal(); |
|
21 | 23 |
} |
22 | 24 |
} |
Backend/Core/StaticConfig/GlobalConfiguration.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
|
|
7 |
namespace Core.StaticConfig |
|
8 |
{ |
|
9 |
public class GlobalConfiguration |
|
10 |
{ |
|
11 |
// Default count of required annotations |
|
12 |
private static Mutex RequiredAnnotationsMutex = new Mutex(); |
|
13 |
private static int requiredAnnotations = 3; |
|
14 |
public static int RequiredAnnotations |
|
15 |
{ |
|
16 |
get { return requiredAnnotations; } |
|
17 |
set |
|
18 |
{ |
|
19 |
RequiredAnnotationsMutex.WaitOne(); |
|
20 |
requiredAnnotations = value; |
|
21 |
RequiredAnnotationsMutex.ReleaseMutex(); |
|
22 |
} |
|
23 |
} |
|
24 |
} |
|
25 |
} |
Backend/Models/Documents/GetRequiredAnnotationsGlobalResponse.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Threading.Tasks; |
|
6 |
|
|
7 |
namespace Models.Documents |
|
8 |
{ |
|
9 |
public class GetRequiredAnnotationsGlobalResponse |
|
10 |
{ |
|
11 |
public int RequiredAnnotationsGlobal { get; set; } |
|
12 |
} |
|
13 |
} |
Také k dispozici: Unified diff
Added endpoint to get global required annotations, moved configuration to database