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 |
eff8ec56
|
Vojtěch Bartička
|
|
15 |
|
|
namespace Core.Services.AnnotationService
|
16 |
|
|
{
|
17 |
|
|
public class AnnotationServiceEF : IAnnotationService
|
18 |
|
|
{
|
19 |
|
|
private readonly DatabaseContext context;
|
20 |
|
|
private readonly ILogger logger;
|
21 |
a6675a6d
|
Vojtěch Bartička
|
private readonly IMapper mapper;
|
22 |
eff8ec56
|
Vojtěch Bartička
|
|
23 |
a6675a6d
|
Vojtěch Bartička
|
public AnnotationServiceEF(DatabaseContext context, ILogger logger, IMapper mapper)
|
24 |
eff8ec56
|
Vojtěch Bartička
|
{
|
25 |
|
|
this.context = context;
|
26 |
|
|
this.logger = logger;
|
27 |
a6675a6d
|
Vojtěch Bartička
|
this.mapper = mapper;
|
28 |
eff8ec56
|
Vojtěch Bartička
|
}
|
29 |
|
|
|
30 |
|
|
public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid clientUserId)
|
31 |
|
|
{
|
32 |
|
|
User addingUser = context.Users.Single(u => u.Id == clientUserId);
|
33 |
|
|
|
34 |
|
|
// Check the documents exist
|
35 |
153d77a8
|
Vojtěch Bartička
|
var documents = context.Documents.Where(d => request.DocumentIdList.Contains(d.Id)).ToList();
|
36 |
|
|
if (documents.Count() != request.DocumentIdList.Count)
|
37 |
eff8ec56
|
Vojtěch Bartička
|
{
|
38 |
153d77a8
|
Vojtěch Bartička
|
logger.Information($"Received a non-existent Document ID when assigning documents to users");
|
39 |
|
|
throw new InvalidOperationException($"{request.DocumentIdList.Count - documents.Count()} of the received documents do not exist");
|
40 |
eff8ec56
|
Vojtěch Bartička
|
}
|
41 |
|
|
|
42 |
6bdb3d95
|
Vojtěch Bartička
|
var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList();
|
43 |
153d77a8
|
Vojtěch Bartička
|
foreach (var user in users)
|
44 |
eff8ec56
|
Vojtěch Bartička
|
{
|
45 |
153d77a8
|
Vojtěch Bartička
|
var userAnnotatedDocuments = context.Annotations.Where(a => a.User == user).Select(a => a.Document).ToList();
|
46 |
|
|
foreach (var doc in documents)
|
47 |
eff8ec56
|
Vojtěch Bartička
|
{
|
48 |
153d77a8
|
Vojtěch Bartička
|
if (userAnnotatedDocuments.Contains(doc))
|
49 |
eff8ec56
|
Vojtěch Bartička
|
{
|
50 |
153d77a8
|
Vojtěch Bartička
|
logger.Information($"User {user.Username} has already been assigned the document {doc.Id}, ignoring");
|
51 |
|
|
continue;
|
52 |
eff8ec56
|
Vojtěch Bartička
|
}
|
53 |
|
|
|
54 |
|
|
context.Annotations.Add(new Annotation()
|
55 |
|
|
{
|
56 |
|
|
User = user,
|
57 |
|
|
UserAssigned = addingUser,
|
58 |
|
|
DateAssigned = DateTime.Now,
|
59 |
|
|
DateLastChanged = DateTime.Now,
|
60 |
153d77a8
|
Vojtěch Bartička
|
Document = doc,
|
61 |
eff8ec56
|
Vojtěch Bartička
|
State = EState.NEW,
|
62 |
|
|
Note = ""
|
63 |
|
|
});
|
64 |
|
|
}
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
context.SaveChanges();
|
68 |
|
|
}
|
69 |
6bdb3d95
|
Vojtěch Bartička
|
|
70 |
|
|
public AnnotationListResponse GetUserAnnotations(Guid userId)
|
71 |
|
|
{
|
72 |
|
|
var annotations = context.Annotations.Where(a => a.User.Id == userId).Include(a => a.Document).ToList();
|
73 |
|
|
var documentIds = annotations.Select(a => a.Document.Id).ToList();
|
74 |
|
|
var documents = context.Documents.Where(d => documentIds.Contains(d.Id));
|
75 |
|
|
var infos = new List<AnnotationListInfo>();
|
76 |
|
|
|
77 |
|
|
var annotationsDocuments = annotations.Zip(documents, (a, d) => new { Annotation = a, Document = d });
|
78 |
|
|
foreach (var ad in annotationsDocuments)
|
79 |
|
|
{
|
80 |
|
|
infos.Add(new AnnotationListInfo()
|
81 |
|
|
{
|
82 |
|
|
AnnotationId = ad.Annotation.Id,
|
83 |
|
|
DocumentName = ad.Document.Name,
|
84 |
|
|
State = ad.Annotation.State
|
85 |
|
|
});
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
return new AnnotationListResponse()
|
89 |
|
|
{
|
90 |
|
|
Annotations = infos
|
91 |
|
|
};
|
92 |
|
|
}
|
93 |
a6675a6d
|
Vojtěch Bartička
|
|
94 |
|
|
public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole)
|
95 |
|
|
{
|
96 |
c2a89232
|
Vojtěch Bartička
|
var annotation = context.Annotations
|
97 |
|
|
.Where(a => a.Id == annotationId)
|
98 |
|
|
.Include(a => a.User)
|
99 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
100 |
|
|
.First();
|
101 |
|
|
|
102 |
a6675a6d
|
Vojtěch Bartička
|
if (userRole < ERole.ADMINISTRATOR)
|
103 |
|
|
{
|
104 |
|
|
if (annotation.User.Id != userId)
|
105 |
|
|
{
|
106 |
|
|
throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
c2a89232
|
Vojtěch Bartička
|
var documentContent = context.Documents.Where(d => d.Id == annotation.Document.Id).Select(d => d.Content).First();
|
111 |
a6675a6d
|
Vojtěch Bartička
|
|
112 |
|
|
// We probably cannot use AutoMapper since we are dealing with too many different entities
|
113 |
|
|
AnnotationInfo annotationInfo = new()
|
114 |
|
|
{
|
115 |
|
|
DocumentText = documentContent.Content,
|
116 |
|
|
Note = annotation.Note,
|
117 |
|
|
State = annotation.State,
|
118 |
|
|
Type = IsHtml(documentContent.Content) ? EDocumentType.HTML : EDocumentType.TEXT
|
119 |
|
|
};
|
120 |
|
|
|
121 |
c2a89232
|
Vojtěch Bartička
|
var tags = context.AnnotationTags.Where(at => at.Annotation.Id == annotationId)
|
122 |
|
|
.Include(at => at.Tag).ThenInclude(t => t.Category)
|
123 |
|
|
.Include(at => at.SubTag)
|
124 |
|
|
.ToList();
|
125 |
|
|
|
126 |
a6675a6d
|
Vojtěch Bartička
|
foreach (var tag in tags)
|
127 |
|
|
{
|
128 |
|
|
var tagInstance = mapper.Map<TagInstanceInfo>(tag);
|
129 |
|
|
annotationInfo.TagInstances.Add(tagInstance);
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
return annotationInfo;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
// TODO temporary
|
136 |
|
|
private bool IsHtml(string text)
|
137 |
|
|
{
|
138 |
|
|
return text.Contains("<!DOCTYPE html>");
|
139 |
|
|
}
|
140 |
eff8ec56
|
Vojtěch Bartička
|
}
|
141 |
|
|
}
|