Projekt

Obecné

Profil

Stáhnout (3.33 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

    
13
namespace Core.Services.AnnotationService
14
{
15
    public class AnnotationServiceEF : IAnnotationService
16
    {
17
        private readonly DatabaseContext context;
18
        private readonly ILogger logger;
19

    
20
        public AnnotationServiceEF(DatabaseContext context, ILogger logger)
21
        {
22
            this.context = context;
23
            this.logger = logger;
24
        }
25

    
26
        public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid clientUserId)
27
        {
28
            User addingUser = context.Users.Single(u => u.Id == clientUserId);
29

    
30
            // Check the documents exist
31
            var documents = context.Documents.Where(d => request.DocumentIdList.Contains(d.Id)).ToList();
32
            if (documents.Count() != request.DocumentIdList.Count)
33
            {
34
                logger.Information($"Received a non-existent Document ID when assigning documents to users");
35
                throw new InvalidOperationException($"{request.DocumentIdList.Count - documents.Count()} of the received documents do not exist");
36
            }
37

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

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

    
63
            context.SaveChanges();
64
        }
65

    
66
        public AnnotationListResponse GetUserAnnotations(Guid userId)
67
        {
68
            var annotations = context.Annotations.Where(a => a.User.Id == userId).Include(a => a.Document).ToList();
69
            var documentIds = annotations.Select(a => a.Document.Id).ToList();
70
            var documents = context.Documents.Where(d => documentIds.Contains(d.Id));
71
            var infos = new List<AnnotationListInfo>();
72

    
73
            var annotationsDocuments = annotations.Zip(documents, (a, d) => new { Annotation = a, Document = d });
74
            foreach (var ad in annotationsDocuments)
75
            {
76
                infos.Add(new AnnotationListInfo()
77
                {
78
                    AnnotationId = ad.Annotation.Id,
79
                    DocumentName = ad.Document.Name,
80
                    State = ad.Annotation.State
81
                });
82
            }
83

    
84
            return new AnnotationListResponse()
85
            {
86
                Annotations = infos
87
            };
88
        }
89
    }
90
}
(1-1/2)