Projekt

Obecné

Profil

Stáhnout (23.9 KB) Statistiky
| Větev: | Tag: | Revize:
1 eff8ec56 Vojtěch Bartička
using Core.Contexts;
2
using Core.Entities;
3
using Models.Annotations;
4
using Models.Enums;
5
using Serilog;
6
using System;
7
using System.Collections.Generic;
8
using System.Linq;
9
using System.Text;
10
using System.Threading.Tasks;
11 6bdb3d95 Vojtěch Bartička
using Microsoft.EntityFrameworkCore;
12 a6675a6d Vojtěch Bartička
using AutoMapper;
13
using Models.Tags;
14 3c185841 Vojtěch Bartička
using Ganss.XSS;
15
using HtmlAgilityPack;
16
using System.Text.RegularExpressions;
17 eff8ec56 Vojtěch Bartička
18
namespace Core.Services.AnnotationService
19
{
20
    public class AnnotationServiceEF : IAnnotationService
21
    {
22
        private readonly DatabaseContext context;
23
        private readonly ILogger logger;
24 a6675a6d Vojtěch Bartička
        private readonly IMapper mapper;
25 eff8ec56 Vojtěch Bartička
26 0a9f9349 Vojtěch Bartička
        private const string TAG_ID_ATTRIBUTE_NAME = "aswi-tag-id";
27
        private const string TAG_INSTANCE_ATTRIBUTE_NAME = "aswi-iag-instance";
28
29 a6675a6d Vojtěch Bartička
        public AnnotationServiceEF(DatabaseContext context, ILogger logger, IMapper mapper)
30 eff8ec56 Vojtěch Bartička
        {
31
            this.context = context;
32
            this.logger = logger;
33 a6675a6d Vojtěch Bartička
            this.mapper = mapper;
34 eff8ec56 Vojtěch Bartička
        }
35
36
        public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid clientUserId)
37
        {
38
            User addingUser = context.Users.Single(u => u.Id == clientUserId);
39
40
            // Check the documents exist
41 153d77a8 Vojtěch Bartička
            var documents = context.Documents.Where(d => request.DocumentIdList.Contains(d.Id)).ToList();
42
            if (documents.Count() != request.DocumentIdList.Count)
43 eff8ec56 Vojtěch Bartička
            {
44 153d77a8 Vojtěch Bartička
                logger.Information($"Received a non-existent Document ID when assigning documents to users");
45
                throw new InvalidOperationException($"{request.DocumentIdList.Count - documents.Count()} of the received documents do not exist");
46 eff8ec56 Vojtěch Bartička
            }
47
48 6bdb3d95 Vojtěch Bartička
            var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList();
49 153d77a8 Vojtěch Bartička
            foreach (var user in users)
50 eff8ec56 Vojtěch Bartička
            {
51 153d77a8 Vojtěch Bartička
                var userAnnotatedDocuments = context.Annotations.Where(a => a.User == user).Select(a => a.Document).ToList();
52
                foreach (var doc in documents)
53 eff8ec56 Vojtěch Bartička
                {
54 153d77a8 Vojtěch Bartička
                    if (userAnnotatedDocuments.Contains(doc))
55 eff8ec56 Vojtěch Bartička
                    {
56 153d77a8 Vojtěch Bartička
                        logger.Information($"User {user.Username} has already been assigned the document {doc.Id}, ignoring");
57
                        continue;
58 eff8ec56 Vojtěch Bartička
                    }
59
60
                    context.Annotations.Add(new Annotation()
61
                    {
62
                        User = user,
63
                        UserAssigned = addingUser,
64
                        DateAssigned = DateTime.Now,
65
                        DateLastChanged = DateTime.Now,
66 153d77a8 Vojtěch Bartička
                        Document = doc,
67 eff8ec56 Vojtěch Bartička
                        State = EState.NEW,
68
                        Note = ""
69
                    });
70
                }
71
            }
72
73
            context.SaveChanges();
74
        }
75 6bdb3d95 Vojtěch Bartička
76
        public AnnotationListResponse GetUserAnnotations(Guid userId)
77
        {
78
            var annotations = context.Annotations.Where(a => a.User.Id == userId).Include(a => a.Document).ToList();
79
            var documentIds = annotations.Select(a => a.Document.Id).ToList();
80
            var documents = context.Documents.Where(d => documentIds.Contains(d.Id));
81
            var infos = new List<AnnotationListInfo>();
82
83
            var annotationsDocuments = annotations.Zip(documents, (a, d) => new { Annotation = a, Document = d });
84
            foreach (var ad in annotationsDocuments)
85
            {
86
                infos.Add(new AnnotationListInfo()
87
                {
88
                    AnnotationId = ad.Annotation.Id,
89
                    DocumentName = ad.Document.Name,
90
                    State = ad.Annotation.State
91
                });
92
            }
93
94
            return new AnnotationListResponse()
95
            {
96
                Annotations = infos
97
            };
98
        }
99 a6675a6d Vojtěch Bartička
100
        public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole)
101
        {
102 c2a89232 Vojtěch Bartička
            var annotation = context.Annotations
103
                .Where(a => a.Id == annotationId)
104
                .Include(a => a.User)
105
                .Include(a => a.Document).ThenInclude(d => d.Content)
106
                .First();
107 3c185841 Vojtěch Bartička
108 a6675a6d Vojtěch Bartička
            if (userRole < ERole.ADMINISTRATOR)
109
            {
110
                if (annotation.User.Id != userId)
111
                {
112
                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
113
                }
114
            }
115
116 c2a89232 Vojtěch Bartička
            var documentContent = context.Documents.Where(d => d.Id == annotation.Document.Id).Select(d => d.Content).First();
117 a6675a6d Vojtěch Bartička
118 c2a89232 Vojtěch Bartička
            var tags = context.AnnotationTags.Where(at => at.Annotation.Id == annotationId)
119
                .Include(at => at.Tag).ThenInclude(t => t.Category)
120
                .Include(at => at.SubTag)
121
                .ToList();
122
123 0a9f9349 Vojtěch Bartička
            List<TagInstanceInfo> tagInstanceInfos = new();
124 a6675a6d Vojtěch Bartička
            foreach (var tag in tags)
125
            {
126
                var tagInstance = mapper.Map<TagInstanceInfo>(tag);
127 0a9f9349 Vojtěch Bartička
                tagInstanceInfos.Add(tagInstance);
128 a6675a6d Vojtěch Bartička
            }
129
130 0a9f9349 Vojtěch Bartička
            var docToRender = PreprocessHTML(documentContent.Content, tags);
131
132
            // We probably cannot use AutoMapper since we are dealing with too many different entities
133
            AnnotationInfo annotationInfo = new()
134
            {
135
                SourceDocumentContent = documentContent.Content,
136
                DocumentToRender = docToRender,
137
                TagStartPositions = TagStartPositions.ToArray(),
138
                TagLengths = TagStartLengths.ToArray(),
139
                Note = annotation.Note,
140
                State = annotation.State,
141
                Type = IsHtml(documentContent.Content) ? EDocumentType.HTML : EDocumentType.TEXT,
142
                TagInstances = tagInstanceInfos
143
            };
144
145 a6675a6d Vojtěch Bartička
            return annotationInfo;
146
        }
147
148 0a9f9349 Vojtěch Bartička
        private List<int> TagStartPositions = new();
149
        private List<int> TagStartLengths = new();
150
        private List<int> TagClosingPositions = new();
151
        private List<int> TagClosingLengths = new();
152
        private Dictionary<HtmlNode, HtmlNode> NodeDict = new();
153
154
        private string PreprocessHTML(string htmlSource, List<AnnotationTag> tags)
155 3c185841 Vojtěch Bartička
        {
156
            var docOriginal = new HtmlDocument();
157
            docOriginal.LoadHtml(htmlSource);
158
            var docToEdit = new HtmlDocument();
159
            docToEdit.LoadHtml(htmlSource);
160
161
            var descendantsOriginal = docOriginal.DocumentNode.DescendantsAndSelf();
162
            var descendantsToEdit = docToEdit.DocumentNode.DescendantsAndSelf();
163
164 0a9f9349 Vojtěch Bartička
            int currentId = 0;
165 3c185841 Vojtěch Bartička
166 0a9f9349 Vojtěch Bartička
            FillNodeDict(descendantsOriginal, descendantsToEdit);
167
            AssignIdsToOriginalDocument(descendantsOriginal, ref currentId);
168 3c185841 Vojtěch Bartička
169 0a9f9349 Vojtěch Bartička
            var cssNode = GenerateCSS(docToEdit, tags, new());
170
            docToEdit.DocumentNode.ChildNodes.Add(cssNode);
171 3c185841 Vojtěch Bartička
172 0a9f9349 Vojtěch Bartička
            WrapTextInSpan(descendantsOriginal, docToEdit);
173 3c185841 Vojtěch Bartička
174 0a9f9349 Vojtěch Bartička
            int descendantsCount = descendantsToEdit.Count();
175
            foreach (var tag in tags)
176 3c185841 Vojtěch Bartička
            {
177 0a9f9349 Vojtěch Bartička
                int i = 0;
178
                List<HtmlNode> addedForSelection = new();
179
                while (i < descendantsCount)
180 3c185841 Vojtěch Bartička
                {
181 0a9f9349 Vojtěch Bartička
                    for (; i < descendantsCount; i++)
182
                    {
183
                        var node = descendantsToEdit.ElementAt(i);
184
                        if (!node.Name.Contains("#text") || addedForSelection.Contains(node) || addedForSelection.Contains(node.ParentNode) ||
185
                            node.ParentNode.Name == "style")
186
                        {
187
                            continue;
188
                        }
189
190
                        var start = TagStartPositions[node.ParentNode.GetAttributeValue(TAG_ID_ATTRIBUTE_NAME, -1)];
191
                        var end = TagClosingPositions[node.ParentNode.GetAttributeValue(TAG_ID_ATTRIBUTE_NAME, -1)];
192
193
                        int selectionStart = tag.Position;
194
                        int selectionEnd = tag.Position + tag.Length;
195
196
                        if (selectionStart < end && selectionEnd > start)
197
                        {
198
                            if (selectionStart <= start && selectionEnd >= end)
199
                            {
200
                                addedForSelection.Add(SolveFullFill(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
201
                            }
202
                            else if (selectionStart <= start)
203
                            {
204
                                addedForSelection.AddRange(SolveRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
205
                            }
206
                            else if (selectionEnd >= end)
207
                            {
208
                                addedForSelection.AddRange(SolveLeftGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
209
                            }
210
                            else
211
                            {
212
                                addedForSelection.AddRange(SolveLeftRightGap(node, selectionStart, selectionEnd, start, end, docToEdit, tag));
213
                            }
214
                            break;
215
                        }
216
                    }
217 3c185841 Vojtěch Bartička
                }
218
219
            }
220
221
            string docToRender = docToEdit.DocumentNode.OuterHtml;
222
            HtmlSanitizer sanitizer = new HtmlSanitizer();
223
            sanitizer.AllowedAttributes.Clear();
224 0a9f9349 Vojtěch Bartička
            sanitizer.AllowedAttributes.Add(TAG_ID_ATTRIBUTE_NAME);
225
            sanitizer.AllowedAttributes.Add(TAG_INSTANCE_ATTRIBUTE_NAME);
226 be4deff8 Vojtěch Bartička
            if (sanitizer.AllowedTags.Contains("script"))
227
            {
228
                sanitizer.AllowedTags.Remove("script");
229
            }
230 3c185841 Vojtěch Bartička
231
            docToRender = sanitizer.Sanitize(docToRender);
232
233 0a9f9349 Vojtěch Bartička
            return docToRender;
234
        }
235
236
        private HtmlNode SolveFullFill(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit, AnnotationTag tag)
237
        {
238
            // full fill
239
            string textSelected = node.InnerText;
240
241
            var parentNode = node.ParentNode;
242
            int nodeIndex = parentNode.ChildNodes.IndexOf(node);
243
            parentNode.ChildNodes.RemoveAt(nodeIndex);
244
245
            EPosition markerPosition = EPosition.MARK_NONE;
246
            if (selectionEnd == end && selectionStart == start)
247
            {
248
                markerPosition = EPosition.MARK_LEFT_RIGHT;
249
            }
250
251
            HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TAG_ID_ATTRIBUTE_NAME, TagStartPositions.Count,
252
                TAG_INSTANCE_ATTRIBUTE_NAME, tag.Instance, start, markerPosition);
253
            parentNode.ChildNodes.Insert(nodeIndex, spanSelected);
254
255
            return spanSelected;
256
        }
257
258
        private List<HtmlNode> SolveRightGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
259
                                             AnnotationTag tag)
260
        {
261
            // partial fill, end gap
262
            string text = node.InnerText;
263
            string textAfter = text.Substring(Math.Min(selectionStart - start + tag.Length, text.Length));
264
            string textSelected = text.Substring(0, selectionEnd - start);
265
266
            var parentNode = node.ParentNode;
267
            int nodeIndex = parentNode.ChildNodes.IndexOf(node);
268
            parentNode.ChildNodes.RemoveAt(nodeIndex);
269
270
            int spanSelectedStart = start;
271
            int spanAfterStart = start + textSelected.Length;
272
273
            HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TAG_ID_ATTRIBUTE_NAME, TagStartPositions.Count,
274
                TAG_INSTANCE_ATTRIBUTE_NAME, tag.Instance, spanSelectedStart, EPosition.MARK_RIGHT);
275
            parentNode.ChildNodes.Insert(nodeIndex, spanSelected);
276
277
            HtmlNode spanAfter = CreateSpan(docToEdit, textAfter, TAG_ID_ATTRIBUTE_NAME, TagStartPositions.Count,
278
                TAG_INSTANCE_ATTRIBUTE_NAME, null, spanAfterStart);
279
            parentNode.ChildNodes.Insert(nodeIndex + 1, spanAfter);
280
281
            return new() { spanSelected, spanAfter };
282
        }
283
284
        private List<HtmlNode> SolveLeftGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
285
                                             AnnotationTag tag)
286
        {
287
            // partial fill, start gap
288
            string text = node.InnerText;
289
            string textBefore = text.Substring(0, selectionStart - start);
290
            string textSelected = text.Substring(selectionStart - start, Math.Min(tag.Length, text.Length - textBefore.Length));
291
292
            var parentNode = node.ParentNode;
293
            int nodeIndex = parentNode.ChildNodes.IndexOf(node);
294
            parentNode.ChildNodes.RemoveAt(nodeIndex);
295
296
            int spanBeforeStart = start;
297
            int spanSelectedStart = start + textBefore.Length;
298
299
            HtmlNode spanBefore = CreateSpan(docToEdit, textBefore, TAG_ID_ATTRIBUTE_NAME, TagStartPositions.Count,
300
                TAG_INSTANCE_ATTRIBUTE_NAME, null, spanBeforeStart);
301
            parentNode.ChildNodes.Insert(nodeIndex, spanBefore);
302
303
            HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TAG_ID_ATTRIBUTE_NAME, TagStartPositions.Count,
304
                TAG_INSTANCE_ATTRIBUTE_NAME, tag.Instance, spanSelectedStart, EPosition.MARK_LEFT);
305
            parentNode.ChildNodes.Insert(nodeIndex + 1, spanSelected);
306
307
            return new() { spanSelected, spanBefore };
308
        }
309
310
        private List<HtmlNode> SolveLeftRightGap(HtmlNode node, int selectionStart, int selectionEnd, int start, int end, HtmlDocument docToEdit,
311
                                                 AnnotationTag tag)
312
        {
313
            // partial fill, start gap end gap
314
            string text = node.InnerText;
315
            string textBefore = text.Substring(0, selectionStart - start);
316
            string textAfter = text.Substring(selectionStart - start + tag.Length);
317
            string textSelected = text.Substring(selectionStart - start, tag.Length);
318
319
            var parentNode = node.ParentNode;
320
            int nodeIndex = parentNode.ChildNodes.IndexOf(node);
321
            parentNode.ChildNodes.RemoveAt(nodeIndex);
322
323
            int spanBeforeStart = start;
324
            int spanSelectedStart = start + textBefore.Length;
325
            int spanAfterStart = start + textBefore.Length + textSelected.Length;
326
327
            HtmlNode spanBefore = CreateSpan(docToEdit, textBefore, TAG_ID_ATTRIBUTE_NAME, TagStartPositions.Count,
328
                TAG_INSTANCE_ATTRIBUTE_NAME, null, spanBeforeStart);
329
            parentNode.ChildNodes.Insert(nodeIndex, spanBefore);
330
331
            HtmlNode spanSelected = CreateSpan(docToEdit, textSelected, TAG_ID_ATTRIBUTE_NAME, TagStartPositions.Count,
332
                TAG_INSTANCE_ATTRIBUTE_NAME, tag.Instance, spanSelectedStart, EPosition.MARK_LEFT_RIGHT);
333
            parentNode.ChildNodes.Insert(nodeIndex + 1, spanSelected);
334
335
            HtmlNode spanAfter = CreateSpan(docToEdit, textAfter, TAG_ID_ATTRIBUTE_NAME, TagStartPositions.Count,
336
                TAG_INSTANCE_ATTRIBUTE_NAME, null, spanAfterStart);
337
            parentNode.ChildNodes.Insert(nodeIndex + 2, spanAfter);
338
339
            return new() { spanSelected, spanBefore, spanAfter };
340
        }
341
342
        private HtmlNode CreateSpan(HtmlDocument doc, string text, string tagIdAttributeName, int tagId, string tagInstanceAttributeName,
343
                                            Guid? instanceId, int startPosition, EPosition position = EPosition.MARK_NONE)
344
        {
345
            HtmlNode span = doc.CreateElement("span");
346
            span.InnerHtml = text;
347
            TagStartPositions.Add(startPosition);
348
            TagStartLengths.Add(0);
349
            TagClosingPositions.Add(startPosition + text.Length);
350
            TagClosingLengths.Add(0);
351
            span.Attributes.Add(tagIdAttributeName, tagId.ToString());
352
353
            if (instanceId != null)
354
            {
355
                span.AddClass("annotation");
356
                span.Attributes.Add(tagInstanceAttributeName, instanceId.Value.ToString());
357
358
                if (position == EPosition.MARK_LEFT || position == EPosition.MARK_LEFT_RIGHT)
359
                {
360
                    span.Attributes.Add("start", "1");
361
                }
362
                if (position == EPosition.MARK_RIGHT || position == EPosition.MARK_LEFT_RIGHT)
363
                {
364
                    span.Attributes.Add("end", "1");
365
                }
366
            }
367
368
            return span;
369
        }
370
371
        private enum EPosition
372
        {
373
            MARK_LEFT = 5,
374
            MARK_RIGHT = 3,
375
            MARK_LEFT_RIGHT = 2,
376
            MARK_NONE = 0
377
        }
378
379
        private void WrapTextInSpan(IEnumerable<HtmlNode> descendantsOriginal, HtmlDocument docToEdit)
380
        {
381
            foreach (var node in descendantsOriginal)
382
            {
383
                var originalNode = node;
384
                var toEditNode = NodeDict[node];
385
386
                if (originalNode.Name.Contains("#"))
387
                {
388
                    continue;
389
                }
390
                else
391
                {
392
                    bool onlyText = true;
393
                    bool onlySubtags = true;
394
395
                    foreach (var child in node.ChildNodes)
396
                    {
397
                        if (child.Name.Contains("#"))
398
                        {
399
                            onlySubtags = false;
400
                        }
401
                        else
402
                        {
403
                            onlyText = false;
404
                        }
405
                    }
406
407
                    if (onlyText || onlySubtags)
408
                    {
409
                        continue;
410
                    }
411
                    else
412
                    {
413
414
                        foreach (var child in node.ChildNodes)
415
                        {
416
                            if (child.Name.Contains("#text"))
417
                            {
418
                                HtmlNode coveringSpan = docToEdit.CreateElement("span");
419
                                coveringSpan.InnerHtml = child.InnerHtml;
420
                                TagStartPositions.Add(child.InnerStartIndex);
421
                                TagStartLengths.Add(0);
422
                                TagClosingPositions.Add(child.InnerStartIndex + child.InnerLength);
423
                                TagClosingLengths.Add(0);
424
                                coveringSpan.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, (TagStartPositions.Count - 1).ToString());
425
426
                                var parent = NodeDict[node];
427
                                var index = parent.ChildNodes.IndexOf(NodeDict[child]);
428
429
                                parent.ChildNodes.RemoveAt(index);
430
                                parent.ChildNodes.Insert(index, coveringSpan);
431
                            }
432
                        }
433
                    }
434
                }
435
            }
436
        }
437
438
        private HtmlNode GenerateCSS(HtmlDocument docToEdit, List<AnnotationTag> tags, List<int> paddings)
439
        {
440
            HtmlNode style = docToEdit.CreateElement("style");
441
442
            string inner = "span.annotation {border-bottom: 2px solid;}";
443
            inner += "span {line-height: 30px}\n";
444
445
            // TODO temporary
446
            int lastPadding = 1;
447
            foreach (var tag in tags)
448
            {
449
                inner += $"span[{TAG_INSTANCE_ATTRIBUTE_NAME}=\"{tag.Instance}\"] {{ border-color:{tag.Tag.Color}; padding-bottom: {lastPadding}px }}";
450
                lastPadding += 2;
451
            }
452
453
            inner += "span[end=\"1\"] {border-end-end-radius: 0px; border-right: 1px solid darkgray}\n";
454
            inner += "span[start=\"1\"] {border-end-start-radius: 0px; border-left: 1px solid darkgray}\n";
455
456
            style.InnerHtml = inner;
457
            return style;
458
        }
459
460
        private void AssignIdsToOriginalDocument(IEnumerable<HtmlNode> descendantsOriginal, ref int currentId)
461
        {
462
            foreach (var node in descendantsOriginal)
463
            {
464
                var originalNode = node;
465
                var toEditNode = NodeDict[node];
466
467
                if (originalNode.Name.Contains("#"))
468
                {
469
                    continue;
470
                }
471
                else
472
                {
473
                    TagStartPositions.Add(originalNode.OuterStartIndex);
474
                    TagStartLengths.Add(originalNode.InnerStartIndex - originalNode.OuterStartIndex);
475
                    currentId = TagStartPositions.Count - 1;
476
                    toEditNode.Attributes.Add(TAG_ID_ATTRIBUTE_NAME, currentId.ToString());
477
478
                    TagClosingPositions.Add(originalNode.InnerStartIndex + originalNode.InnerLength);
479
                    TagClosingLengths.Add((originalNode.OuterStartIndex + originalNode.OuterLength) - (originalNode.InnerStartIndex + originalNode.InnerLength));
480
                }
481
            }
482
        }
483
484
        private void FillNodeDict(IEnumerable<HtmlNode> descendantsOriginal, IEnumerable<HtmlNode> descendantsToEdit)
485
        {
486
            var zipped = descendantsOriginal.Zip(descendantsToEdit, (orig, toEdit) => new
487
            {
488
                Original = orig,
489
                ToEdit = toEdit
490
            });
491
            foreach (var node in zipped)
492
            {
493
                var originalNode = node.Original;
494
                var toEditNode = node.ToEdit;
495
                NodeDict.Add(originalNode, toEditNode);
496
            }
497 3c185841 Vojtěch Bartička
        }
498
499 a6675a6d Vojtěch Bartička
        // TODO temporary
500
        private bool IsHtml(string text)
501
        {
502 0a9f9349 Vojtěch Bartička
            return text.Contains("<html>");
503 a6675a6d Vojtěch Bartička
        }
504 be4deff8 Vojtěch Bartička
505
        public void AddAnnotationInstance(Guid annotationId, Guid userId, ERole userRole, AnnotationInstanceAddRequest request)
506
        {
507
            var annotation = context.Annotations
508
               .Where(a => a.Id == annotationId)
509
               .Include(a => a.User)
510
               .Include(a => a.Document).ThenInclude(d => d.Content)
511
               .First();
512
513
            if (userRole < ERole.ADMINISTRATOR)
514
            {
515
                if (annotation.User.Id != userId)
516
                {
517
                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
518
                }
519
            }
520
521
            AnnotationTag annotationTag = new()
522
            {
523
                Annotation = annotation,
524
                Instance = request.InstanceId == null ? Guid.NewGuid() : request.InstanceId.Value,
525
                Length = request.Length,
526
                Position = request.Position,
527
                Note = ""
528
            };
529
530
            if (request.Type == ETagType.TAG)
531
            {
532
                annotationTag.Tag = context.Tags.Where(t => t.Id == request.Id).Single();
533
                annotationTag.SubTag = null;
534
            }
535
            else if (request.Type == ETagType.SUBTAG)
536
            {
537
                var subTag = context.SubTags.Where(st => st.Id == request.Id).Include(st => st.Tag).Single();
538
                annotationTag.SubTag = subTag;
539
                annotationTag.Tag = subTag.Tag;
540
            }
541
            else
542
            {
543
                throw new ArgumentException($"Unknown tag type {request.Type}");
544
            }
545
546 abf94f21 Lukáš Vlček
            context.AnnotationTags.Add(annotationTag);
547 be4deff8 Vojtěch Bartička
            context.SaveChanges();
548
        }
549 0f8d6304 Vojtěch Bartička
550
        public void DeleteAnnotationInstance(Guid annotationId, Guid tagInstanceId, Guid loggedUserId, ERole userRole)
551
        {
552
            Annotation annotation = null;
553
            try
554
            {
555
                annotation = context.Annotations
556
                   .Where(a => a.Id == annotationId)
557
                   .Include(a => a.User)
558
                   .Include(a => a.Document).ThenInclude(d => d.Content)
559
                   .First();
560
561
            }
562
            catch (Exception ex)
563
            {
564
                throw new InvalidOperationException("Could not find annotation");
565
            }
566
567
568
            if (userRole < ERole.ADMINISTRATOR)
569
            {
570
                if (annotation.User.Id != loggedUserId)
571
                {
572
                    throw new UnauthorizedAccessException($"User {loggedUserId} does not have assigned annotation {annotationId}");
573
                }
574
            }
575
576
            if (!context.AnnotationTags.Any(at => at.Id == tagInstanceId))
577
            {
578
                throw new InvalidOperationException("Could not find tag instance");
579
            }
580
581
            context.AnnotationTags
582
                .Where(at => at.Id == tagInstanceId).ToList()
583
                .ForEach(a => context.AnnotationTags.Remove(a));
584 0a9f9349 Vojtěch Bartička
585 0f8d6304 Vojtěch Bartička
            context.SaveChanges();
586
        }
587 eff8ec56 Vojtěch Bartička
    }
588
}