Projekt

Obecné

Profil

Stáhnout (5.28 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 a = context.Annotations.Where(a => a.Id == annotationId).Include(a => a.User).Include(a => a.Document).Include(a => a.Document.Content).ToList();
97
            if (a.Count == 0)
98
            {
99
                throw new ArgumentException($"Annotation {annotationId} does not exist");
100
            }
101
            Annotation annotation = a.ElementAt(0);
102

    
103
            if (userRole < ERole.ADMINISTRATOR)
104
            {
105
                if (annotation.User.Id != userId)
106
                {
107
                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
108
                }
109
            }
110

    
111
            var documentContent = context.Documents.Where(d => d.Id == annotation.Document.Id).Select(d => d.Content).ToList()[0];
112

    
113
            // We probably cannot use AutoMapper since we are dealing with too many different entities
114
            AnnotationInfo annotationInfo = new()
115
            {
116
                DocumentText = documentContent.Content,
117
                Note = annotation.Note,
118
                State = annotation.State,
119
                Type = IsHtml(documentContent.Content) ? EDocumentType.HTML : EDocumentType.TEXT
120
            };
121

    
122
            var tags = context.AnnotationTags.Where(at => at.Annotation.Id == annotationId).Include(at => at.Tag).Include(at => at.SubTag).Include(at => at.Tag.Category).ToList();
123
            foreach (var tag in tags)
124
            {
125
                var tagInstance = mapper.Map<TagInstanceInfo>(tag);
126
                annotationInfo.TagInstances.Add(tagInstance);
127
            }
128

    
129
            return annotationInfo;
130
        }
131

    
132
        // TODO temporary
133
        private bool IsHtml(string text)
134
        {
135
            return text.Contains("<!DOCTYPE html>");
136
        }
137
    }
138
}
(1-1/2)