Projekt

Obecné

Profil

Stáhnout (5.21 KB) Statistiky
| Větev: | Tag: | Revize:
1
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
using Microsoft.EntityFrameworkCore;
12
using AutoMapper;
13
using Models.Tags;
14

    
15
namespace Core.Services.AnnotationService
16
{
17
    public class AnnotationServiceEF : IAnnotationService
18
    {
19
        private readonly DatabaseContext context;
20
        private readonly ILogger logger;
21
        private readonly IMapper mapper;
22

    
23
        public AnnotationServiceEF(DatabaseContext context, ILogger logger, IMapper mapper)
24
        {
25
            this.context = context;
26
            this.logger = logger;
27
            this.mapper = mapper;
28
        }
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
            var documents = context.Documents.Where(d => request.DocumentIdList.Contains(d.Id)).ToList();
36
            if (documents.Count() != request.DocumentIdList.Count)
37
            {
38
                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
            }
41

    
42
            var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList();
43
            foreach (var user in users)
44
            {
45
                var userAnnotatedDocuments = context.Annotations.Where(a => a.User == user).Select(a => a.Document).ToList();
46
                foreach (var doc in documents)
47
                {
48
                    if (userAnnotatedDocuments.Contains(doc))
49
                    {
50
                        logger.Information($"User {user.Username} has already been assigned the document {doc.Id}, ignoring");
51
                        continue;
52
                    }
53

    
54
                    context.Annotations.Add(new Annotation()
55
                    {
56
                        User = user,
57
                        UserAssigned = addingUser,
58
                        DateAssigned = DateTime.Now,
59
                        DateLastChanged = DateTime.Now,
60
                        Document = doc,
61
                        State = EState.NEW,
62
                        Note = ""
63
                    });
64
                }
65
            }
66

    
67
            context.SaveChanges();
68
        }
69

    
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

    
94
        public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole)
95
        {
96
            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
            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
            var documentContent = context.Documents.Where(d => d.Id == annotation.Document.Id).Select(d => d.Content).First();
111

    
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
            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
            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
    }
141
}
(1-1/2)