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