commit 8c9ce202126023970c18a0168e5753f920f8f475
Author: Vojtěch Bartička <aitakaitov@gmail.com>
Date:   Sun May 15 16:51:06 2022 +0200

    Fixed issues with database OR mapping

diff --git a/Backend/Core/Contexts/DatabaseContext.cs b/Backend/Core/Contexts/DatabaseContext.cs
index c45d7d8..0bb44f9 100644
--- a/Backend/Core/Contexts/DatabaseContext.cs
+++ b/Backend/Core/Contexts/DatabaseContext.cs
@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
 using Microsoft.Extensions.Configuration;
 using System;
 using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations.Schema;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -36,5 +37,9 @@ namespace Core.Contexts
             optionsBuilder.UseNpgsql(_configuration["ConnectionString"], b => b.MigrationsAssembly("RestAPI"));
             //optionsBuilder.UseNpgsql("Host=localhost:5432;Database=dbo;Username=myuser;Password=password");
         }
+
+        protected override void OnModelCreating(ModelBuilder modelBuilder)
+        {
+        }
     }
 }
diff --git a/Backend/Core/Entities/Annotation.cs b/Backend/Core/Entities/Annotation.cs
index dc1e52e..5da30e4 100644
--- a/Backend/Core/Entities/Annotation.cs
+++ b/Backend/Core/Entities/Annotation.cs
@@ -39,5 +39,7 @@ namespace Core.Entities
         public EModified ModifiedType { get; set; } = EModified.NONE;
 
         public ICollection<Class> Classes { get; set; } = new List<Class>();
+
+        public ICollection<FinalAnnotation> FinalAnnotations { get; set; } = new List<FinalAnnotation>();
     }
 }
diff --git a/Backend/Core/Entities/FinalAnnotation.cs b/Backend/Core/Entities/FinalAnnotation.cs
index d6d555b..6eba8a9 100644
--- a/Backend/Core/Entities/FinalAnnotation.cs
+++ b/Backend/Core/Entities/FinalAnnotation.cs
@@ -1,5 +1,6 @@
 ﻿using System;
 using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations.Schema;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
diff --git a/Backend/Core/Entities/FinalAnnotationTag.cs b/Backend/Core/Entities/FinalAnnotationTag.cs
index 9fc2fbe..dc3b38e 100644
--- a/Backend/Core/Entities/FinalAnnotationTag.cs
+++ b/Backend/Core/Entities/FinalAnnotationTag.cs
@@ -8,9 +8,8 @@ namespace Core.Entities
 {
     public class FinalAnnotationTag : AnnotationTagGeneric
     {
-        // May cause problems?
         public FinalAnnotation Annotation { get; set; }
         public bool IsFinal { get; set; } = false;
-        public List<User> Users { get; set; } = new();
+        public ICollection<User> Users { get; set; } = new List<User>();
     }
 }
diff --git a/Backend/Core/Entities/User.cs b/Backend/Core/Entities/User.cs
index 4e60155..30db07b 100644
--- a/Backend/Core/Entities/User.cs
+++ b/Backend/Core/Entities/User.cs
@@ -20,5 +20,7 @@ namespace Core.Entities
         public string Surname { get; set; }
 
         public ERole Role { get; set; }
+
+        public ICollection<FinalAnnotationTag> FinalAnnotationTags { get; set; } = new List<FinalAnnotationTag>();
     }
 }
\ No newline at end of file
diff --git a/Backend/Core/MapperProfiles/TagProfileEF.cs b/Backend/Core/MapperProfiles/TagProfileEF.cs
index 7b26d24..fe651e2 100644
--- a/Backend/Core/MapperProfiles/TagProfileEF.cs
+++ b/Backend/Core/MapperProfiles/TagProfileEF.cs
@@ -23,6 +23,8 @@ namespace Core.MapperProfiles
             CreateMap<AnnotationTag, FinalAnnotationTag>()
                 .ForMember(fat => fat.Id, opt => opt.Ignore())
                 .ForMember(fat => fat.Annotation, opt => opt.Ignore());
+
+            CreateMap<FinalAnnotationTag, TagInstanceInfo>();
         }
     }
 }
diff --git a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
index ce47835..64171d4 100644
--- a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
+++ b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
@@ -50,7 +50,11 @@ namespace Core.Services.AnnotationService
             var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList();
             foreach (var user in users)
             {
-                var userAnnotatedDocuments = context.Annotations.Where(a => a.User == user).Select(a => a.Document).ToList();
+                var userAnnotatedDocuments = context.Annotations
+                    .Where(a => !(a is FinalAnnotation))
+                    .Where(a => a.User == user)
+                    .Select(a => a.Document)
+                    .ToList();
                 foreach (var doc in documents)
                 {
                     if (userAnnotatedDocuments.Contains(doc))
@@ -78,9 +82,11 @@ namespace Core.Services.AnnotationService
         public AnnotationListResponse GetUserAnnotations(Guid userId)
         {
             var annotations = context.Annotations
+                .Where(a => !(a is FinalAnnotation))
                 .Where(a => a.User.Id == userId)
                 .Include(a => a.Document)
                 .ToList();
+
             var infos = new List<AnnotationListInfo>();
             foreach (var annotation in annotations)
             {
@@ -133,6 +139,7 @@ namespace Core.Services.AnnotationService
             if (!isFinal)
             {
                 annotation = context.Annotations
+                .Where(a => !(a is FinalAnnotation))
                 .Where(a => a.Id == annotationId)
                 .Include(a => a.User)
                 .Include(a => a.Document).ThenInclude(d => d.Content)
@@ -158,12 +165,22 @@ namespace Core.Services.AnnotationService
 
             var documentContent = context.Documents.Where(d => d.Id == annotation.Document.Id).Select(d => d.Content).First();
 
-            var tags = context.AnnotationTags.Where(at => at.Annotation.Id == annotationId)
-                .Include(at => at.Tag)
+            IQueryable<AnnotationTagGeneric> tagsQuery;
+            List<AnnotationTagGeneric> tags;
+            if (!isFinal)
+            {
+                tagsQuery = context.AnnotationTags.Where(at => at.Annotation.Id == annotationId);
+            }
+            else
+            {
+                tagsQuery = context.FinalAnnotationTags.Where(at => at.Annotation.Id == annotationId);
+            }
+
+            tags = tagsQuery.Include(at => at.Tag)
                 .ThenInclude(t => t.Category)
                 .Include(at => at.SubTag)
                 .OrderBy(at => at.Position)
-                .Select(at => at as AnnotationTagGeneric)
+                .Select(at => at)
                 .ToList();
 
             List<TagInstanceInfo> tagInstanceInfos = new();
@@ -259,6 +276,7 @@ namespace Core.Services.AnnotationService
             if (!isFinal)
             {
                 annotation = context.Annotations
+                    .Where(a => !(a is FinalAnnotation))
                     .Where(a => a.Id == annotationId)
                     .Include(a => a.User)
                     .Include(a => a.Document).ThenInclude(d => d.Content)
@@ -389,6 +407,7 @@ namespace Core.Services.AnnotationService
                 if (!isFinal)
                 {
                     annotation = context.Annotations
+                    .Where(a => !(a is FinalAnnotation))
                     .Where(a => a.Id == annotationId)
                     .Include(a => a.User)
                     .Include(a => a.Document).ThenInclude(d => d.Content)
@@ -462,6 +481,7 @@ namespace Core.Services.AnnotationService
                 if (!isFinal)
                 {
                     annotation = context.Annotations
+                    .Where(a => !(a is FinalAnnotation))
                     .Where(a => a.Id == annotationId)
                     .Include(a => a.User)
                     .Include(a => a.Document).ThenInclude(d => d.Content)
@@ -524,6 +544,7 @@ namespace Core.Services.AnnotationService
                 if (!isFinal)
                 {
                     annotation = context.Annotations
+                    .Where(a => !(a is FinalAnnotation))
                    .Where(a => a.Id == annotationId)
                    .Include(a => a.User)
                    .Include(a => a.Document).ThenInclude(d => d.Content)
@@ -568,6 +589,7 @@ namespace Core.Services.AnnotationService
             }
 
             var annotations = context.Annotations
+                .Where(a => !(a is FinalAnnotation))
                 .Include(a => a.Document)
                 .Where(a => a.Document == document && a.State == EState.DONE)
                 .ToList();
diff --git a/Backend/Core/Services/DocumentService/DocumentServiceEF.cs b/Backend/Core/Services/DocumentService/DocumentServiceEF.cs
index 33758f8..9db3556 100644
--- a/Backend/Core/Services/DocumentService/DocumentServiceEF.cs
+++ b/Backend/Core/Services/DocumentService/DocumentServiceEF.cs
@@ -137,15 +137,20 @@ namespace Core.Services.DocumentService
             List<DocumentListInfo> documentInfos = new List<DocumentListInfo>();
             foreach (var document in documents.GetRange(firstIndex, pageSize))
             {
-                var annotatingUsers = databaseContext.Annotations.Where(a => a.Document == document).Select(a => a.User).ToList();
+                var annotatingUsers = databaseContext.Annotations
+                    .Where(a => !(a is FinalAnnotation))
+                    .Where(a => a.Document == document)
+                    .Select(a => a.User).ToList();
+
                 List<DocumentUserInfo> annotatingUsersDto = new();
 
                 // Include annotation state
                 foreach (var annotatingUser in annotatingUsers)
                 {
                     var annotation = databaseContext.Annotations
+                        .Where(a => !(a is FinalAnnotation))
                         .Include(a => a.Document)
-                        .Single(a => a.Document == document && a.User == annotatingUser);
+                        .Single(a => a.Document == document && a.User == annotatingUser && !(a is FinalAnnotation));
                     var dui = mapper.Map<DocumentUserInfo>(annotatingUser);
                     dui.State = annotation.State;
                     annotatingUsersDto.Add(dui);
diff --git a/Backend/Core/Services/TagService/TagServiceEF.cs b/Backend/Core/Services/TagService/TagServiceEF.cs
index 1504567..e036d7b 100644
--- a/Backend/Core/Services/TagService/TagServiceEF.cs
+++ b/Backend/Core/Services/TagService/TagServiceEF.cs
@@ -279,7 +279,9 @@ namespace Core.Services.TagService
             {
                 if (!isFinal)
                 {
-                    annotation = databaseContext.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
+                    annotation = databaseContext.Annotations
+                        .Where(a => !(a is FinalAnnotation))
+                        .Include(a => a.User).First(a => a.Id == annotationId);
                 }
                 else
                 {
diff --git a/Backend/Core/Services/UserService/UserServiceEF.cs b/Backend/Core/Services/UserService/UserServiceEF.cs
index c9f2a50..30b389e 100644
--- a/Backend/Core/Services/UserService/UserServiceEF.cs
+++ b/Backend/Core/Services/UserService/UserServiceEF.cs
@@ -186,7 +186,11 @@ namespace Core.Services
             foreach (var user in users)
             {
                 var userInfo = _mapper.Map<UserInfo>(user);
-                userInfo.AssignedDocumentsCount = _databaseContext.Annotations.Include(a => a.User).Where(a => a.User == user).Count();
+                userInfo.AssignedDocumentsCount = _databaseContext.Annotations
+                    .Include(a => a.User)
+                    .Where(a => !(a is FinalAnnotation))
+                    .Where(a => a.User == user)
+                    .Count();
                 userList.Users.Add(userInfo);
             }
 
diff --git a/Backend/Models/Tags/TagInstanceInfo.cs b/Backend/Models/Tags/TagInstanceInfo.cs
index f1a063c..410fb5a 100644
--- a/Backend/Models/Tags/TagInstanceInfo.cs
+++ b/Backend/Models/Tags/TagInstanceInfo.cs
@@ -1,4 +1,5 @@
 ﻿using Models.Enums;
+using Models.Users;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -26,5 +27,8 @@ namespace Models.Tags
 
         public ETagSentiment? Sentiment { get; set; }
         public string SelectedText { get; set; }
+
+        public List<UserInfo>? Users { get; set; }
+        public bool? IsFinal { get; set; }
     }
 }
