Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 153d77a8

Přidáno uživatelem Vojtěch Bartička před asi 2 roky(ů)

Database access optimizations

Zobrazit rozdíly:

Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
27 27
            User addingUser = context.Users.Single(u => u.Id == clientUserId);
28 28

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

  
39
            foreach (Guid userId in request.UserIdList)
37
            var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList(); 
38
            foreach (var user in users)
40 39
            {
41
                var user = context.Users.Single(u => u.Id == userId);
42
                var userAnnotatedDocuments = context.Annotations.Where(a => a.User.Id == userId).Select(a => a.Document.Id);
43

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

  
51
                    var document = context.Documents.Single(d => d.Id == docId);
52

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

Také k dispozici: Unified diff