commit f227518579ac43f903c6ae96b1127e8bbb691cbc
Author: Vojtěch Bartička <aitakaitov@gmail.com>
Date:   Fri Apr 22 13:40:24 2022 +0200

    Endpoint for adding Notes to annotations in AnnotationController

diff --git a/Backend/Backend/Controllers/AnnotationController.cs b/Backend/Backend/Controllers/AnnotationController.cs
index 07bd3e9..d45af4f 100644
--- a/Backend/Backend/Controllers/AnnotationController.cs
+++ b/Backend/Backend/Controllers/AnnotationController.cs
@@ -153,12 +153,12 @@ namespace RestAPI.Controllers
             // non-existent annotation
             try
             {
-                tagService.AddAnnotationInstance(annotationId, occurenceId, clientInfo.LoggedUser, request);
+                tagService.AddNoteToTagInstance(annotationId, occurenceId, clientInfo.LoggedUser, request);
                 return Ok();
             }
             catch (InvalidOperationException e)
             {
-                throw new BadRequestException("Could not find specified annotation");
+                throw new BadRequestException(e.Message);
             }
             catch (UnauthorizedAccessException)
             {
@@ -166,5 +166,35 @@ namespace RestAPI.Controllers
             }
 
         }
+
+        [HttpPost("/annotation/{annotationId}/note")]
+        [ProducesResponseType((int)HttpStatusCode.OK)]
+        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
+        public ActionResult AddNoteToTagOccurence([FromServices] ClientInfo clientInfo, Guid annotationId, [FromBody] AddNoteToAnnotationRequest request)
+        {
+            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.AddNoteToAnnotation(annotationId, clientInfo.LoggedUser.Id, clientInfo.LoggedUser.Role, request);
+                return Ok();
+            }
+            catch (InvalidOperationException e)
+            {
+                throw new BadRequestException(e.Message);
+            }
+            catch (UnauthorizedAccessException)
+            {
+                return Forbid();
+            }
+
+        }
+
     }
 }
diff --git a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
index d3b1ec1..6886c4f 100644
--- a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
+++ b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
@@ -97,6 +97,28 @@ namespace Core.Services.AnnotationService
             };
         }
 
+        public void AddNoteToAnnotation(Guid annotationId, Guid userId, ERole userRole, AddNoteToAnnotationRequest request)
+        {
+            Annotation annotation = null;
+            try
+            {
+                annotation = context.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
+            }
+            catch (Exception)
+            {
+                throw new InvalidOperationException("Annotation not found");
+            }
+
+            if (userRole < ERole.ADMINISTRATOR && annotation.User.Id != userId)
+            {
+                throw new UnauthorizedAccessException("User does not have access to this annotation");
+            }
+
+            annotation.Note = request.Note;
+            context.SaveChanges();
+        }
+
+
         public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole)
         {
             var annotation = context.Annotations
@@ -227,7 +249,7 @@ namespace Core.Services.AnnotationService
             if (sanitizer.AllowedTags.Contains("script"))
             {
                 sanitizer.AllowedTags.Remove("script");
-            }   
+            }
             if (!sanitizer.AllowedTags.Contains("style"))
             {
                 sanitizer.AllowedTags.Add("style");
diff --git a/Backend/Core/Services/AnnotationService/IAnnotationService.cs b/Backend/Core/Services/AnnotationService/IAnnotationService.cs
index 07dddb1..6a225d5 100644
--- a/Backend/Core/Services/AnnotationService/IAnnotationService.cs
+++ b/Backend/Core/Services/AnnotationService/IAnnotationService.cs
@@ -15,6 +15,6 @@ namespace Core.Services.AnnotationService
         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);
-
+        public void AddNoteToAnnotation(Guid annotationId, Guid userId, ERole userRole, AddNoteToAnnotationRequest request);
     }
 }
diff --git a/Backend/Core/Services/TagService/ITagService.cs b/Backend/Core/Services/TagService/ITagService.cs
index 8c63cff..12e55d6 100644
--- a/Backend/Core/Services/TagService/ITagService.cs
+++ b/Backend/Core/Services/TagService/ITagService.cs
@@ -22,6 +22,6 @@ namespace Core.Services.TagService
         public void CreateSubTag(CreateSubTagRequest request);
         public void DeleteSubTag(Guid subtagId);
         public void UpdateSubTag(ModifySubTagRequest request, Guid subtagId);
-        public void AddAnnotationInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request);
+        public void AddNoteToTagInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request);
     }
 }
diff --git a/Backend/Core/Services/TagService/TagServiceEF.cs b/Backend/Core/Services/TagService/TagServiceEF.cs
index 6b20bfd..9f8e5ee 100644
--- a/Backend/Core/Services/TagService/TagServiceEF.cs
+++ b/Backend/Core/Services/TagService/TagServiceEF.cs
@@ -233,7 +233,7 @@ namespace Core.Services.TagService
             databaseContext.SaveChanges();
         }
 
-        public void AddAnnotationInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request)
+        public void AddNoteToTagInstance(Guid annotationId, Guid occurrenceId, User user, AddNoteToTagOccurenceRequest request)
         {
             Annotation annotation = null;
             try
diff --git a/Backend/Models/Annotations/AddNoteToAnnotationRequest.cs b/Backend/Models/Annotations/AddNoteToAnnotationRequest.cs
new file mode 100644
index 0000000..5ee599c
--- /dev/null
+++ b/Backend/Models/Annotations/AddNoteToAnnotationRequest.cs
@@ -0,0 +1,13 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Models.Annotations
+{
+    public class AddNoteToAnnotationRequest
+    {
+        public string Note { get; set; }
+    }
+}
