commit 0f8d6304152d20511d43d066a93a1e8b29537e52
Author: Vojtěch Bartička <aitakaitov@gmail.com>
Date:   Sun Apr 17 15:41:37 2022 +0200

    Endpoint for annotation instance deletion - untested

diff --git a/Backend/Backend/Controllers/AnnotationController.cs b/Backend/Backend/Controllers/AnnotationController.cs
index a187467..201468b 100644
--- a/Backend/Backend/Controllers/AnnotationController.cs
+++ b/Backend/Backend/Controllers/AnnotationController.cs
@@ -102,5 +102,34 @@ namespace RestAPI.Controllers
             }
 
         }
+
+        [HttpDelete("/annotation/{annotationId}/{tagInstanceId}")]
+        [ProducesResponseType((int)HttpStatusCode.OK)]
+        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
+        public ActionResult DeleteAnnotationInstance([FromServices] ClientInfo clientInfo, Guid annotationId, Guid tagInstanceId)
+        {
+            if (clientInfo.LoggedUser == null)
+            {
+                logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
+                return Problem();
+            }
+
+            // Take care of - non-admin user requesting not-assigned annotation
+            // non-existent annotation
+            try
+            {
+                annotationService.DeleteAnnotationInstance(annotationId, tagInstanceId, clientInfo.LoggedUser.Id, clientInfo.LoggedUser.Role);
+                return Ok();
+            }
+            catch (InvalidOperationException e)
+            {
+                throw new BadRequestException("Could not find specified annotation");
+            }
+            catch (UnauthorizedAccessException)
+            {
+                return Forbid();
+            }
+
+        }
     }
 }
diff --git a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
index c97e009..e3d929a 100644
--- a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
+++ b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
@@ -244,5 +244,43 @@ namespace Core.Services.AnnotationService
             context.AnnotationTags.Add(annotationTag);
             context.SaveChanges();
         }
+
+        public void DeleteAnnotationInstance(Guid annotationId, Guid tagInstanceId, Guid loggedUserId, ERole userRole)
+        {
+            Annotation annotation = null;
+            try
+            {
+                annotation = context.Annotations
+                   .Where(a => a.Id == annotationId)
+                   .Include(a => a.User)
+                   .Include(a => a.Document).ThenInclude(d => d.Content)
+                   .First();
+
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidOperationException("Could not find annotation");
+            }
+
+
+            if (userRole < ERole.ADMINISTRATOR)
+            {
+                if (annotation.User.Id != loggedUserId)
+                {
+                    throw new UnauthorizedAccessException($"User {loggedUserId} does not have assigned annotation {annotationId}");
+                }
+            }
+
+            if (!context.AnnotationTags.Any(at => at.Id == tagInstanceId))
+            {
+                throw new InvalidOperationException("Could not find tag instance");
+            }
+
+            context.AnnotationTags
+                .Where(at => at.Id == tagInstanceId).ToList()
+                .ForEach(a => context.AnnotationTags.Remove(a));
+            
+            context.SaveChanges();
+        }
     }
 }
diff --git a/Backend/Core/Services/AnnotationService/IAnnotationService.cs b/Backend/Core/Services/AnnotationService/IAnnotationService.cs
index 5946e9c..07dddb1 100644
--- a/Backend/Core/Services/AnnotationService/IAnnotationService.cs
+++ b/Backend/Core/Services/AnnotationService/IAnnotationService.cs
@@ -14,6 +14,7 @@ namespace Core.Services.AnnotationService
         public AnnotationListResponse GetUserAnnotations(Guid userId);
         public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole);
         public void AddAnnotationInstance(Guid annotationId, Guid userId, ERole userRole, AnnotationInstanceAddRequest request);
+        public void DeleteAnnotationInstance(Guid annotationId, Guid tagInstanceId, Guid loggedUserId, ERole userRole);
 
     }
 }
diff --git a/Backend/Models/Tags/TagInstanceInfo.cs b/Backend/Models/Tags/TagInstanceInfo.cs
index 3c5cbcc..cbbc4e6 100644
--- a/Backend/Models/Tags/TagInstanceInfo.cs
+++ b/Backend/Models/Tags/TagInstanceInfo.cs
@@ -8,6 +8,8 @@ namespace Models.Tags
 {
     public class TagInstanceInfo
     {
+        /** For database */
+        public Guid Id { get; set; }
         public string TagName { get; set; }
         public Guid TagId { get; set; }
         public string TagCategoryName { get; set; }
