1 |
055ef5b5
|
Vojtěch Bartička
|
using AutoMapper;
|
2 |
|
|
using Core.Contexts;
|
3 |
9190add5
|
Vojtěch Bartička
|
using Core.Entities;
|
4 |
|
|
using Models.Enums;
|
5 |
055ef5b5
|
Vojtěch Bartička
|
using Models.Tags;
|
6 |
9190add5
|
Vojtěch Bartička
|
using Models.Users;
|
7 |
055ef5b5
|
Vojtěch Bartička
|
using Serilog;
|
8 |
|
|
using System;
|
9 |
|
|
using System.Collections.Generic;
|
10 |
|
|
using System.Text;
|
11 |
|
|
using System.Threading.Tasks;
|
12 |
9190add5
|
Vojtěch Bartička
|
using Microsoft.EntityFrameworkCore;
|
13 |
|
|
using System.Linq;
|
14 |
055ef5b5
|
Vojtěch Bartička
|
|
15 |
|
|
namespace Core.Services.TagService
|
16 |
|
|
{
|
17 |
|
|
public class TagServiceEF : ITagService
|
18 |
|
|
{
|
19 |
|
|
private readonly DatabaseContext databaseContext;
|
20 |
|
|
private readonly ILogger logger;
|
21 |
|
|
private readonly IMapper mapper;
|
22 |
|
|
|
23 |
|
|
public TagServiceEF(DatabaseContext databaseContext, ILogger logger, IMapper mapper)
|
24 |
|
|
{
|
25 |
|
|
this.databaseContext = databaseContext;
|
26 |
|
|
this.logger = logger;
|
27 |
|
|
this.mapper = mapper;
|
28 |
|
|
}
|
29 |
|
|
|
30 |
2c8d08af
|
Vojtěch Bartička
|
public TagTreeResponse GetTagTree(ERole userRole)
|
31 |
055ef5b5
|
Vojtěch Bartička
|
{
|
32 |
ecb333b6
|
Vojtěch Bartička
|
// Prefetch from DB
|
33 |
d65a2d79
|
Vojtěch Bartička
|
var tagCategories = databaseContext.TagCategories
|
34 |
|
|
.Select(tc => tc)
|
35 |
|
|
.OrderBy(tc => tc.Name)
|
36 |
|
|
.ToList();
|
37 |
|
|
|
38 |
|
|
var tags = databaseContext.Tags
|
39 |
|
|
.Select(tc => tc)
|
40 |
|
|
.ToList();
|
41 |
|
|
|
42 |
|
|
var subTags = databaseContext.SubTags
|
43 |
|
|
.Select(tc => tc)
|
44 |
|
|
.ToList();
|
45 |
ecb333b6
|
Vojtěch Bartička
|
|
46 |
055ef5b5
|
Vojtěch Bartička
|
var tagCategoriesDTOs = new List<TagCategoryInfo>();
|
47 |
|
|
foreach (var tagCategory in tagCategories)
|
48 |
|
|
{
|
49 |
2c8d08af
|
Vojtěch Bartička
|
if (tagCategory.DisabledForAnnotators && userRole == ERole.ANNOTATOR)
|
50 |
|
|
{
|
51 |
|
|
continue;
|
52 |
|
|
}
|
53 |
|
|
|
54 |
055ef5b5
|
Vojtěch Bartička
|
var tagCategoryDTO = mapper.Map<TagCategoryInfo>(tagCategory);
|
55 |
d65a2d79
|
Vojtěch Bartička
|
var categoryTags = tags
|
56 |
|
|
.Where(t => t.Category == tagCategory)
|
57 |
|
|
.OrderBy(t => t.Name);
|
58 |
|
|
|
59 |
055ef5b5
|
Vojtěch Bartička
|
var tagDTOs = new List<TagInfo>();
|
60 |
|
|
|
61 |
ecb333b6
|
Vojtěch Bartička
|
foreach (var tag in categoryTags)
|
62 |
055ef5b5
|
Vojtěch Bartička
|
{
|
63 |
|
|
var tagDTO = mapper.Map<TagInfo>(tag);
|
64 |
d65a2d79
|
Vojtěch Bartička
|
var tagSubTags = subTags
|
65 |
|
|
.Where(st => st.Tag == tag)
|
66 |
|
|
.OrderBy(st => st.Name)
|
67 |
|
|
.ToList();
|
68 |
|
|
|
69 |
055ef5b5
|
Vojtěch Bartička
|
var subTagDTOs = new List<SubTagInfo>();
|
70 |
|
|
|
71 |
ecb333b6
|
Vojtěch Bartička
|
if (tagSubTags.Count() != 0)
|
72 |
055ef5b5
|
Vojtěch Bartička
|
{
|
73 |
ecb333b6
|
Vojtěch Bartička
|
foreach (var subTag in tagSubTags)
|
74 |
055ef5b5
|
Vojtěch Bartička
|
{
|
75 |
|
|
subTagDTOs.Add(mapper.Map<SubTagInfo>(subTag));
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
tagDTO.SubTags = subTagDTOs;
|
79 |
|
|
tagDTOs.Add(tagDTO);
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
tagCategoryDTO.Tags = tagDTOs;
|
83 |
|
|
tagCategoriesDTOs.Add(tagCategoryDTO);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
return new TagTreeResponse()
|
87 |
|
|
{
|
88 |
|
|
TagCategories = tagCategoriesDTOs
|
89 |
|
|
};
|
90 |
|
|
}
|
91 |
addbcd33
|
Vojtěch Bartička
|
|
92 |
|
|
public void CreateCategory(CreateCategoryRequest request)
|
93 |
|
|
{
|
94 |
|
|
if (request.Color == "" || request.Name == "")
|
95 |
|
|
{
|
96 |
|
|
throw new InvalidOperationException("Category name or color empty");
|
97 |
|
|
}
|
98 |
|
|
|
99 |
1a65a2c2
|
Vojtěch Bartička
|
CategoryNameUnusedElseThrow(request.Name);
|
100 |
addbcd33
|
Vojtěch Bartička
|
|
101 |
2c8d08af
|
Vojtěch Bartička
|
databaseContext.TagCategories.Add(new TagCategory()
|
102 |
addbcd33
|
Vojtěch Bartička
|
{
|
103 |
|
|
Name = request.Name,
|
104 |
|
|
Color = request.Color,
|
105 |
2c8d08af
|
Vojtěch Bartička
|
Description = request.Description,
|
106 |
|
|
DisabledForAnnotators = request.DisabledForAnnotators
|
107 |
addbcd33
|
Vojtěch Bartička
|
});
|
108 |
|
|
|
109 |
|
|
databaseContext.SaveChanges();
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
public void DeleteCategory(Guid categoryId)
|
113 |
|
|
{
|
114 |
1a65a2c2
|
Vojtěch Bartička
|
CategoryExistsElseThrow(categoryId);
|
115 |
addbcd33
|
Vojtěch Bartička
|
|
116 |
|
|
var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
|
117 |
|
|
databaseContext.TagCategories.Remove(category);
|
118 |
|
|
databaseContext.SaveChanges();
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
public void UpdateCategory(ModifyCategoryRequest request, Guid categoryId)
|
122 |
|
|
{
|
123 |
1a65a2c2
|
Vojtěch Bartička
|
CategoryExistsElseThrow(categoryId);
|
124 |
addbcd33
|
Vojtěch Bartička
|
var category = databaseContext.TagCategories.First(tc => tc.Id == categoryId);
|
125 |
|
|
|
126 |
42361f51
|
Vojtěch Bartička
|
if (request.Name != null && request.Name != category.Name)
|
127 |
addbcd33
|
Vojtěch Bartička
|
{
|
128 |
1a65a2c2
|
Vojtěch Bartička
|
CategoryNameUnusedElseThrow(request.Name);
|
129 |
addbcd33
|
Vojtěch Bartička
|
category.Name = request.Name;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
if (request.Description != null)
|
133 |
|
|
{
|
134 |
|
|
category.Description = request.Description;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
if (request.Color != null)
|
138 |
|
|
{
|
139 |
|
|
if (request.Color == "")
|
140 |
|
|
{
|
141 |
|
|
throw new InvalidOperationException("Empty color");
|
142 |
|
|
}
|
143 |
|
|
category.Color = request.Color;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
2c8d08af
|
Vojtěch Bartička
|
if (request.DisabledForAnnotators != null)
|
147 |
|
|
{
|
148 |
|
|
category.DisabledForAnnotators = (bool) request.DisabledForAnnotators;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
addbcd33
|
Vojtěch Bartička
|
databaseContext.SaveChanges();
|
152 |
|
|
}
|
153 |
|
|
|
154 |
162db5f6
|
Vojtěch Bartička
|
public void CreateTag(CreateTagRequest request)
|
155 |
|
|
{
|
156 |
1a65a2c2
|
Vojtěch Bartička
|
CategoryExistsElseThrow(request.CategoryId);
|
157 |
addbcd33
|
Vojtěch Bartička
|
|
158 |
1a65a2c2
|
Vojtěch Bartička
|
TagNameUnusedElseThrow(request.Name);
|
159 |
162db5f6
|
Vojtěch Bartička
|
|
160 |
|
|
if (request.Color == "")
|
161 |
|
|
{
|
162 |
|
|
throw new InvalidOperationException("Color empty");
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
var category = databaseContext.TagCategories.First(tc => tc.Id == request.CategoryId);
|
166 |
|
|
|
167 |
|
|
databaseContext.Tags.Add(new Entities.Tag()
|
168 |
|
|
{
|
169 |
|
|
Name = request.Name,
|
170 |
|
|
Color = request.Color,
|
171 |
|
|
Description = request.Description,
|
172 |
|
|
Category = category
|
173 |
|
|
});
|
174 |
|
|
|
175 |
|
|
databaseContext.SaveChanges();
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
public void DeleteTag(Guid tagId)
|
179 |
|
|
{
|
180 |
1a65a2c2
|
Vojtěch Bartička
|
TagExistsElseThrow(tagId);
|
181 |
162db5f6
|
Vojtěch Bartička
|
var tag = databaseContext.Tags.First(t => t.Id == tagId);
|
182 |
|
|
databaseContext.Tags.Remove(tag);
|
183 |
1a65a2c2
|
Vojtěch Bartička
|
databaseContext.SaveChanges();
|
184 |
162db5f6
|
Vojtěch Bartička
|
}
|
185 |
|
|
|
186 |
|
|
public void UpdateTag(ModifyTagRequest request, Guid tagId)
|
187 |
|
|
{
|
188 |
1a65a2c2
|
Vojtěch Bartička
|
TagExistsElseThrow(tagId);
|
189 |
162db5f6
|
Vojtěch Bartička
|
var tag = databaseContext.Tags.First(t => t.Id == tagId);
|
190 |
|
|
|
191 |
42361f51
|
Vojtěch Bartička
|
if (request.Name != null && request.Name != tag.Name)
|
192 |
162db5f6
|
Vojtěch Bartička
|
{
|
193 |
1a65a2c2
|
Vojtěch Bartička
|
TagNameUnusedElseThrow(request.Name);
|
194 |
162db5f6
|
Vojtěch Bartička
|
tag.Name = request.Name;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
if (request.Color != null)
|
198 |
|
|
{
|
199 |
|
|
if (request.Color == "")
|
200 |
|
|
{
|
201 |
|
|
throw new InvalidOperationException("Color empty");
|
202 |
|
|
}
|
203 |
|
|
tag.Color = request.Color;
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
if (request.Description != null)
|
207 |
|
|
{
|
208 |
|
|
tag.Description = request.Description;
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
databaseContext.SaveChanges();
|
212 |
|
|
}
|
213 |
addbcd33
|
Vojtěch Bartička
|
|
214 |
1a65a2c2
|
Vojtěch Bartička
|
public void CreateSubTag(CreateSubTagRequest request)
|
215 |
|
|
{
|
216 |
|
|
if (request.Name == "")
|
217 |
|
|
{
|
218 |
|
|
throw new InvalidOperationException("Subtag name empty");
|
219 |
|
|
}
|
220 |
addbcd33
|
Vojtěch Bartička
|
|
221 |
1a65a2c2
|
Vojtěch Bartička
|
SubTagNameUnusedElseThrow(request.Name);
|
222 |
9190add5
|
Vojtěch Bartička
|
|
223 |
1a65a2c2
|
Vojtěch Bartička
|
TagExistsElseThrow(request.TagId);
|
224 |
|
|
var tag = databaseContext.Tags.First(t => t.Id == request.TagId);
|
225 |
addbcd33
|
Vojtěch Bartička
|
|
226 |
1a65a2c2
|
Vojtěch Bartička
|
databaseContext.SubTags.Add(new Entities.SubTag()
|
227 |
|
|
{
|
228 |
|
|
Name = request.Name,
|
229 |
|
|
Description = request.Description,
|
230 |
|
|
Tag = tag
|
231 |
|
|
});
|
232 |
addbcd33
|
Vojtěch Bartička
|
|
233 |
1a65a2c2
|
Vojtěch Bartička
|
databaseContext.SaveChanges();
|
234 |
|
|
}
|
235 |
addbcd33
|
Vojtěch Bartička
|
|
236 |
1a65a2c2
|
Vojtěch Bartička
|
public void DeleteSubTag(Guid subtagId)
|
237 |
|
|
{
|
238 |
|
|
SubTagExistsElseThrow(subtagId);
|
239 |
|
|
var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
|
240 |
|
|
databaseContext.SubTags.Remove(subtag);
|
241 |
|
|
databaseContext.SaveChanges();
|
242 |
|
|
}
|
243 |
addbcd33
|
Vojtěch Bartička
|
|
244 |
1a65a2c2
|
Vojtěch Bartička
|
public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId)
|
245 |
|
|
{
|
246 |
|
|
SubTagExistsElseThrow(subtagId);
|
247 |
|
|
var subtag = databaseContext.SubTags.First(st => st.Id == subtagId);
|
248 |
|
|
|
249 |
42361f51
|
Vojtěch Bartička
|
if (request.Name != null && request.Name != subtag.Name)
|
250 |
1a65a2c2
|
Vojtěch Bartička
|
{
|
251 |
|
|
SubTagNameUnusedElseThrow(request.Name);
|
252 |
|
|
subtag.Name = request.Name;
|
253 |
9190add5
|
Vojtěch Bartička
|
}
|
254 |
1a65a2c2
|
Vojtěch Bartička
|
|
255 |
|
|
if (request.Description != null)
|
256 |
|
|
{
|
257 |
|
|
subtag.Description = request.Description;
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
databaseContext.SaveChanges();
|
261 |
|
|
}
|
262 |
|
|
|
263 |
f2275185
|
Vojtěch Bartička
|
public void AddNoteToTagInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request)
|
264 |
9190add5
|
Vojtěch Bartička
|
{
|
265 |
|
|
Annotation annotation = null;
|
266 |
|
|
try
|
267 |
|
|
{
|
268 |
|
|
annotation = databaseContext.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
|
269 |
|
|
}
|
270 |
|
|
catch (Exception)
|
271 |
|
|
{
|
272 |
|
|
throw new InvalidOperationException("Annotation not found");
|
273 |
|
|
}
|
274 |
1a65a2c2
|
Vojtěch Bartička
|
|
275 |
9190add5
|
Vojtěch Bartička
|
if (user.Role < ERole.ADMINISTRATOR && annotation.User.Id != user.Id)
|
276 |
|
|
{
|
277 |
|
|
throw new UnauthorizedAccessException("User does not have access to this annotation");
|
278 |
|
|
}
|
279 |
1a65a2c2
|
Vojtěch Bartička
|
|
280 |
9190add5
|
Vojtěch Bartička
|
AnnotationTag occurence = null;
|
281 |
|
|
try
|
282 |
|
|
{
|
283 |
|
|
occurence = databaseContext.AnnotationTags.Include(at => at.Annotation)
|
284 |
|
|
.First(at => at.Annotation.Id == annotationId && at.Id == occurrenceId);
|
285 |
|
|
}
|
286 |
|
|
catch (Exception)
|
287 |
|
|
{
|
288 |
|
|
throw new InvalidOperationException("The annotation does not have a tag with specified id");
|
289 |
|
|
}
|
290 |
1a65a2c2
|
Vojtěch Bartička
|
|
291 |
9190add5
|
Vojtěch Bartička
|
occurence.Note = request.Note;
|
292 |
|
|
databaseContext.SaveChanges();
|
293 |
|
|
}
|
294 |
1a65a2c2
|
Vojtěch Bartička
|
|
295 |
|
|
private void CategoryExistsElseThrow(Guid categoryId)
|
296 |
addbcd33
|
Vojtěch Bartička
|
{
|
297 |
|
|
if (!databaseContext.TagCategories.Any(tc => tc.Id == categoryId))
|
298 |
|
|
{
|
299 |
|
|
throw new InvalidOperationException("Category does not exist");
|
300 |
|
|
}
|
301 |
|
|
}
|
302 |
162db5f6
|
Vojtěch Bartička
|
|
303 |
1a65a2c2
|
Vojtěch Bartička
|
private void CategoryNameUnusedElseThrow(string name)
|
304 |
162db5f6
|
Vojtěch Bartička
|
{
|
305 |
|
|
if (databaseContext.TagCategories.Any(tc => tc.Name == name))
|
306 |
|
|
{
|
307 |
|
|
throw new InvalidOperationException("Category name already used");
|
308 |
|
|
}
|
309 |
|
|
}
|
310 |
|
|
|
311 |
1a65a2c2
|
Vojtěch Bartička
|
private void TagExistsElseThrow(Guid tagId)
|
312 |
162db5f6
|
Vojtěch Bartička
|
{
|
313 |
|
|
if (!databaseContext.Tags.Any(t => t.Id == tagId))
|
314 |
|
|
{
|
315 |
|
|
throw new InvalidOperationException("Tag does not exist");
|
316 |
|
|
}
|
317 |
|
|
}
|
318 |
|
|
|
319 |
1a65a2c2
|
Vojtěch Bartička
|
private void TagNameUnusedElseThrow(string name)
|
320 |
162db5f6
|
Vojtěch Bartička
|
{
|
321 |
|
|
if (databaseContext.Tags.Any(t => t.Name == name))
|
322 |
|
|
{
|
323 |
|
|
throw new InvalidOperationException("Tag name already used");
|
324 |
|
|
}
|
325 |
|
|
}
|
326 |
1a65a2c2
|
Vojtěch Bartička
|
|
327 |
|
|
private void SubTagExistsElseThrow(Guid subtagId)
|
328 |
|
|
{
|
329 |
|
|
if (!databaseContext.SubTags.Any(st => st.Id == subtagId))
|
330 |
|
|
{
|
331 |
|
|
throw new InvalidOperationException("Subtag does not exist");
|
332 |
|
|
}
|
333 |
|
|
}
|
334 |
|
|
|
335 |
|
|
private void SubTagNameUnusedElseThrow(string name)
|
336 |
|
|
{
|
337 |
|
|
if (databaseContext.SubTags.Any(st => st.Name == name))
|
338 |
|
|
{
|
339 |
|
|
throw new InvalidOperationException("Subtag name already used");
|
340 |
|
|
}
|
341 |
|
|
}
|
342 |
055ef5b5
|
Vojtěch Bartička
|
}
|
343 |
|
|
}
|