commit be4deff84566ef77ef9eed0649c91de0cde01d21
Author: Vojtěch Bartička <aitakaitov@gmail.com>
Date:   Sun Apr 17 14:38:23 2022 +0200

    Minor fixes, added annotation instance endpoint - untested

diff --git a/Backend/Backend/Controllers/AnnotationController.cs b/Backend/Backend/Controllers/AnnotationController.cs
index b5cb02a..ccfe949 100644
--- a/Backend/Backend/Controllers/AnnotationController.cs
+++ b/Backend/Backend/Controllers/AnnotationController.cs
@@ -73,5 +73,34 @@ namespace RestAPI.Controllers
             }
 
         }
+
+        [HttpPost("/annotation/{annotationId}")]
+        [ProducesResponseType((int)HttpStatusCode.OK)]
+        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
+        public ActionResult PostAnnotation([FromServices] ClientInfo clientInfo, [FromQuery] Guid annotationId, [FromBody] AnnotationInstanceAddRequest 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.AddAnnotationInstance(annotationId, clientInfo.LoggedUser.Id, clientInfo.LoggedUser.Role, request);
+                return Ok();
+            }
+            catch (InvalidOperationException e)
+            {
+                throw new BadRequestException("Could not find specified annotation");
+            }
+            catch (UnauthorizedAccessException)
+            {
+                return Forbid();
+            }
+
+        }
     }
 }
diff --git a/Backend/Backend/Migrations/20220417114429_V3.Designer.cs b/Backend/Backend/Migrations/20220417114429_V3.Designer.cs
new file mode 100644
index 0000000..8601f5d
--- /dev/null
+++ b/Backend/Backend/Migrations/20220417114429_V3.Designer.cs
@@ -0,0 +1,395 @@
+﻿// <auto-generated />
+using System;
+using Core.Contexts;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+#nullable disable
+
+namespace RestAPI.Migrations
+{
+    [DbContext(typeof(DatabaseContext))]
+    [Migration("20220417114429_V3")]
+    partial class V3
+    {
+        protected override void BuildTargetModel(ModelBuilder modelBuilder)
+        {
+#pragma warning disable 612, 618
+            modelBuilder
+                .HasAnnotation("ProductVersion", "6.0.3")
+                .HasAnnotation("Relational:MaxIdentifierLength", 63);
+
+            NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
+
+            modelBuilder.Entity("AnnotationClass", b =>
+                {
+                    b.Property<Guid>("AnnotationsId")
+                        .HasColumnType("uuid");
+
+                    b.Property<Guid>("ClassesId")
+                        .HasColumnType("uuid");
+
+                    b.HasKey("AnnotationsId", "ClassesId");
+
+                    b.HasIndex("ClassesId");
+
+                    b.ToTable("AnnotationClass");
+                });
+
+            modelBuilder.Entity("Core.Entities.Annotation", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<DateTime>("DateAssigned")
+                        .HasColumnType("timestamp with time zone");
+
+                    b.Property<DateTime>("DateLastChanged")
+                        .HasColumnType("timestamp with time zone");
+
+                    b.Property<Guid>("DocumentId")
+                        .HasColumnType("uuid");
+
+                    b.Property<string>("Note")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.Property<int>("State")
+                        .HasColumnType("integer");
+
+                    b.Property<Guid>("UserAssignedId")
+                        .HasColumnType("uuid");
+
+                    b.Property<Guid>("UserId")
+                        .HasColumnType("uuid");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("DocumentId");
+
+                    b.HasIndex("UserAssignedId");
+
+                    b.HasIndex("UserId");
+
+                    b.ToTable("Annotations");
+                });
+
+            modelBuilder.Entity("Core.Entities.AnnotationTag", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<Guid>("AnnotationId")
+                        .HasColumnType("uuid");
+
+                    b.Property<Guid>("Instance")
+                        .HasColumnType("uuid");
+
+                    b.Property<int>("Length")
+                        .HasColumnType("integer");
+
+                    b.Property<string>("Note")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.Property<int>("Position")
+                        .HasColumnType("integer");
+
+                    b.Property<Guid?>("SubTagId")
+                        .HasColumnType("uuid");
+
+                    b.Property<Guid>("TagId")
+                        .HasColumnType("uuid");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("AnnotationId");
+
+                    b.HasIndex("SubTagId");
+
+                    b.HasIndex("TagId");
+
+                    b.ToTable("AnnotationTags");
+                });
+
+            modelBuilder.Entity("Core.Entities.Class", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<string>("Color")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.Property<string>("Description")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.HasKey("Id");
+
+                    b.ToTable("Classes");
+                });
+
+            modelBuilder.Entity("Core.Entities.Document", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<Guid?>("ContentId")
+                        .HasColumnType("uuid");
+
+                    b.Property<DateTime>("DateAdded")
+                        .HasColumnType("timestamp with time zone");
+
+                    b.Property<int>("Length")
+                        .HasColumnType("integer");
+
+                    b.Property<string>("Name")
+                        .HasColumnType("text");
+
+                    b.Property<int>("RequiredAnnotations")
+                        .HasColumnType("integer");
+
+                    b.Property<Guid?>("UserAddedId")
+                        .HasColumnType("uuid");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("ContentId");
+
+                    b.HasIndex("UserAddedId");
+
+                    b.ToTable("Documents");
+                });
+
+            modelBuilder.Entity("Core.Entities.DocumentContent", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<string>("Content")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.HasKey("Id");
+
+                    b.ToTable("DocumentContents");
+                });
+
+            modelBuilder.Entity("Core.Entities.SubTag", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<string>("Description")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.Property<Guid>("TagId")
+                        .HasColumnType("uuid");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("TagId");
+
+                    b.ToTable("SubTags");
+                });
+
+            modelBuilder.Entity("Core.Entities.Tag", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<Guid>("CategoryId")
+                        .HasColumnType("uuid");
+
+                    b.Property<string>("Color")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.Property<string>("Description")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.Property<string>("Name")
+                        .IsRequired()
+                        .HasColumnType("text");
+
+                    b.HasKey("Id");
+
+                    b.HasIndex("CategoryId");
+
+                    b.ToTable("Tags");
+                });
+
+            modelBuilder.Entity("Core.Entities.TagCategory", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<string>("Color")
+                        .HasColumnType("text");
+
+                    b.Property<string>("Description")
+                        .HasColumnType("text");
+
+                    b.Property<string>("Name")
+                        .HasColumnType("text");
+
+                    b.HasKey("Id");
+
+                    b.ToTable("TagCategories");
+                });
+
+            modelBuilder.Entity("Core.Entities.User", b =>
+                {
+                    b.Property<Guid>("Id")
+                        .ValueGeneratedOnAdd()
+                        .HasColumnType("uuid");
+
+                    b.Property<string>("Name")
+                        .HasColumnType("text");
+
+                    b.Property<string>("Password")
+                        .HasColumnType("text");
+
+                    b.Property<int>("Role")
+                        .HasColumnType("integer");
+
+                    b.Property<string>("Surname")
+                        .HasColumnType("text");
+
+                    b.Property<string>("Username")
+                        .HasColumnType("text");
+
+                    b.HasKey("Id");
+
+                    b.ToTable("Users");
+                });
+
+            modelBuilder.Entity("AnnotationClass", b =>
+                {
+                    b.HasOne("Core.Entities.Annotation", null)
+                        .WithMany()
+                        .HasForeignKey("AnnotationsId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Core.Entities.Class", null)
+                        .WithMany()
+                        .HasForeignKey("ClassesId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+                });
+
+            modelBuilder.Entity("Core.Entities.Annotation", b =>
+                {
+                    b.HasOne("Core.Entities.Document", "Document")
+                        .WithMany()
+                        .HasForeignKey("DocumentId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Core.Entities.User", "UserAssigned")
+                        .WithMany()
+                        .HasForeignKey("UserAssignedId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Core.Entities.User", "User")
+                        .WithMany()
+                        .HasForeignKey("UserId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.Navigation("Document");
+
+                    b.Navigation("User");
+
+                    b.Navigation("UserAssigned");
+                });
+
+            modelBuilder.Entity("Core.Entities.AnnotationTag", b =>
+                {
+                    b.HasOne("Core.Entities.Annotation", "Annotation")
+                        .WithMany()
+                        .HasForeignKey("AnnotationId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.HasOne("Core.Entities.SubTag", "SubTag")
+                        .WithMany()
+                        .HasForeignKey("SubTagId");
+
+                    b.HasOne("Core.Entities.Tag", "Tag")
+                        .WithMany()
+                        .HasForeignKey("TagId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.Navigation("Annotation");
+
+                    b.Navigation("SubTag");
+
+                    b.Navigation("Tag");
+                });
+
+            modelBuilder.Entity("Core.Entities.Document", b =>
+                {
+                    b.HasOne("Core.Entities.DocumentContent", "Content")
+                        .WithMany()
+                        .HasForeignKey("ContentId");
+
+                    b.HasOne("Core.Entities.User", "UserAdded")
+                        .WithMany()
+                        .HasForeignKey("UserAddedId");
+
+                    b.Navigation("Content");
+
+                    b.Navigation("UserAdded");
+                });
+
+            modelBuilder.Entity("Core.Entities.SubTag", b =>
+                {
+                    b.HasOne("Core.Entities.Tag", "Tag")
+                        .WithMany()
+                        .HasForeignKey("TagId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.Navigation("Tag");
+                });
+
+            modelBuilder.Entity("Core.Entities.Tag", b =>
+                {
+                    b.HasOne("Core.Entities.TagCategory", "Category")
+                        .WithMany()
+                        .HasForeignKey("CategoryId")
+                        .OnDelete(DeleteBehavior.Cascade)
+                        .IsRequired();
+
+                    b.Navigation("Category");
+                });
+#pragma warning restore 612, 618
+        }
+    }
+}
diff --git a/Backend/Backend/Migrations/20220417114429_V3.cs b/Backend/Backend/Migrations/20220417114429_V3.cs
new file mode 100644
index 0000000..aecbffb
--- /dev/null
+++ b/Backend/Backend/Migrations/20220417114429_V3.cs
@@ -0,0 +1,258 @@
+﻿using System;
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace RestAPI.Migrations
+{
+    public partial class V3 : Migration
+    {
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropForeignKey(
+                name: "FK_Documents_DocumentContents_ContentId",
+                table: "Documents");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Documents_Users_UserAddedId",
+                table: "Documents");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Username",
+                table: "Users",
+                type: "text",
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "text");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Surname",
+                table: "Users",
+                type: "text",
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "text");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Password",
+                table: "Users",
+                type: "text",
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "text");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Name",
+                table: "Users",
+                type: "text",
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "text");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Name",
+                table: "TagCategories",
+                type: "text",
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "text");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Description",
+                table: "TagCategories",
+                type: "text",
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "text");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Color",
+                table: "TagCategories",
+                type: "text",
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "text");
+
+            migrationBuilder.AlterColumn<Guid>(
+                name: "UserAddedId",
+                table: "Documents",
+                type: "uuid",
+                nullable: true,
+                oldClrType: typeof(Guid),
+                oldType: "uuid");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Name",
+                table: "Documents",
+                type: "text",
+                nullable: true,
+                oldClrType: typeof(string),
+                oldType: "text");
+
+            migrationBuilder.AlterColumn<Guid>(
+                name: "ContentId",
+                table: "Documents",
+                type: "uuid",
+                nullable: true,
+                oldClrType: typeof(Guid),
+                oldType: "uuid");
+
+            migrationBuilder.AlterColumn<Guid>(
+                name: "Instance",
+                table: "AnnotationTags",
+                type: "uuid",
+                nullable: false,
+                oldClrType: typeof(int),
+                oldType: "integer");
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Documents_DocumentContents_ContentId",
+                table: "Documents",
+                column: "ContentId",
+                principalTable: "DocumentContents",
+                principalColumn: "Id");
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Documents_Users_UserAddedId",
+                table: "Documents",
+                column: "UserAddedId",
+                principalTable: "Users",
+                principalColumn: "Id");
+        }
+
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropForeignKey(
+                name: "FK_Documents_DocumentContents_ContentId",
+                table: "Documents");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Documents_Users_UserAddedId",
+                table: "Documents");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Username",
+                table: "Users",
+                type: "text",
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "text",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Surname",
+                table: "Users",
+                type: "text",
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "text",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Password",
+                table: "Users",
+                type: "text",
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "text",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Name",
+                table: "Users",
+                type: "text",
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "text",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Name",
+                table: "TagCategories",
+                type: "text",
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "text",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Description",
+                table: "TagCategories",
+                type: "text",
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "text",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Color",
+                table: "TagCategories",
+                type: "text",
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "text",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<Guid>(
+                name: "UserAddedId",
+                table: "Documents",
+                type: "uuid",
+                nullable: false,
+                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
+                oldClrType: typeof(Guid),
+                oldType: "uuid",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Name",
+                table: "Documents",
+                type: "text",
+                nullable: false,
+                defaultValue: "",
+                oldClrType: typeof(string),
+                oldType: "text",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<Guid>(
+                name: "ContentId",
+                table: "Documents",
+                type: "uuid",
+                nullable: false,
+                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
+                oldClrType: typeof(Guid),
+                oldType: "uuid",
+                oldNullable: true);
+
+            migrationBuilder.AlterColumn<int>(
+                name: "Instance",
+                table: "AnnotationTags",
+                type: "integer",
+                nullable: false,
+                oldClrType: typeof(Guid),
+                oldType: "uuid");
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Documents_DocumentContents_ContentId",
+                table: "Documents",
+                column: "ContentId",
+                principalTable: "DocumentContents",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Documents_Users_UserAddedId",
+                table: "Documents",
+                column: "UserAddedId",
+                principalTable: "Users",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+        }
+    }
+}
diff --git a/Backend/Backend/Migrations/DatabaseContextModelSnapshot.cs b/Backend/Backend/Migrations/DatabaseContextModelSnapshot.cs
index 489b9ee..5f9ffb3 100644
--- a/Backend/Backend/Migrations/DatabaseContextModelSnapshot.cs
+++ b/Backend/Backend/Migrations/DatabaseContextModelSnapshot.cs
@@ -85,8 +85,8 @@ namespace RestAPI.Migrations
                     b.Property<Guid>("AnnotationId")
                         .HasColumnType("uuid");
 
-                    b.Property<int>("Instance")
-                        .HasColumnType("integer");
+                    b.Property<Guid>("Instance")
+                        .HasColumnType("uuid");
 
                     b.Property<int>("Length")
                         .HasColumnType("integer");
@@ -144,7 +144,7 @@ namespace RestAPI.Migrations
                         .ValueGeneratedOnAdd()
                         .HasColumnType("uuid");
 
-                    b.Property<Guid>("ContentId")
+                    b.Property<Guid?>("ContentId")
                         .HasColumnType("uuid");
 
                     b.Property<DateTime>("DateAdded")
@@ -154,13 +154,12 @@ namespace RestAPI.Migrations
                         .HasColumnType("integer");
 
                     b.Property<string>("Name")
-                        .IsRequired()
                         .HasColumnType("text");
 
                     b.Property<int>("RequiredAnnotations")
                         .HasColumnType("integer");
 
-                    b.Property<Guid>("UserAddedId")
+                    b.Property<Guid?>("UserAddedId")
                         .HasColumnType("uuid");
 
                     b.HasKey("Id");
@@ -246,15 +245,12 @@ namespace RestAPI.Migrations
                         .HasColumnType("uuid");
 
                     b.Property<string>("Color")
-                        .IsRequired()
                         .HasColumnType("text");
 
                     b.Property<string>("Description")
-                        .IsRequired()
                         .HasColumnType("text");
 
                     b.Property<string>("Name")
-                        .IsRequired()
                         .HasColumnType("text");
 
                     b.HasKey("Id");
@@ -269,22 +265,18 @@ namespace RestAPI.Migrations
                         .HasColumnType("uuid");
 
                     b.Property<string>("Name")
-                        .IsRequired()
                         .HasColumnType("text");
 
                     b.Property<string>("Password")
-                        .IsRequired()
                         .HasColumnType("text");
 
                     b.Property<int>("Role")
                         .HasColumnType("integer");
 
                     b.Property<string>("Surname")
-                        .IsRequired()
                         .HasColumnType("text");
 
                     b.Property<string>("Username")
-                        .IsRequired()
                         .HasColumnType("text");
 
                     b.HasKey("Id");
@@ -363,15 +355,11 @@ namespace RestAPI.Migrations
                 {
                     b.HasOne("Core.Entities.DocumentContent", "Content")
                         .WithMany()
-                        .HasForeignKey("ContentId")
-                        .OnDelete(DeleteBehavior.Cascade)
-                        .IsRequired();
+                        .HasForeignKey("ContentId");
 
                     b.HasOne("Core.Entities.User", "UserAdded")
                         .WithMany()
-                        .HasForeignKey("UserAddedId")
-                        .OnDelete(DeleteBehavior.Cascade)
-                        .IsRequired();
+                        .HasForeignKey("UserAddedId");
 
                     b.Navigation("Content");
 
diff --git a/Backend/Backend/Program.cs b/Backend/Backend/Program.cs
index 89b3219..4380358 100644
--- a/Backend/Backend/Program.cs
+++ b/Backend/Backend/Program.cs
@@ -113,6 +113,8 @@ using (var scope = app.Services.CreateScope())
     // In development we seed dummy data
     if (app.Environment.IsDevelopment())
     {
+        //context.Database.EnsureDeleted();
+        //context.Database.EnsureCreated();
         context.Database.Migrate();
         Seeder.SeedDevelopment(context);
     }
diff --git a/Backend/Core/Entities/AnnotationTag.cs b/Backend/Core/Entities/AnnotationTag.cs
index 9241c44..798b090 100644
--- a/Backend/Core/Entities/AnnotationTag.cs
+++ b/Backend/Core/Entities/AnnotationTag.cs
@@ -17,7 +17,7 @@ namespace Core.Entities
         /** Nullable for optional */
         public SubTag? SubTag { get; set; }
 
-        public int Instance { get; set; }
+        public Guid Instance { get; set; }
         public string Note { get; set; }
         public int Position { get; set; }
         public int Length { get; set; }
diff --git a/Backend/Core/Seeding/Seeder.cs b/Backend/Core/Seeding/Seeder.cs
index 3e1a7df..7fe2795 100644
--- a/Backend/Core/Seeding/Seeder.cs
+++ b/Backend/Core/Seeding/Seeder.cs
@@ -36,7 +36,7 @@ namespace Core.Seeding
             AnnotationTag at = new AnnotationTag()
             {
                 Annotation = annotation,
-                Instance = 1,
+                Instance = Guid.NewGuid(),
                 Length = 10,
                 Position = 0,
                 SubTag = subtag,
diff --git a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
index 3cde9ae..e71f215 100644
--- a/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
+++ b/Backend/Core/Services/AnnotationService/AnnotationServiceEF.cs
@@ -184,6 +184,10 @@ namespace Core.Services.AnnotationService
             HtmlSanitizer sanitizer = new HtmlSanitizer();
             sanitizer.AllowedAttributes.Clear();
             sanitizer.AllowedAttributes.Add(idAttributeName);
+            if (sanitizer.AllowedTags.Contains("script"))
+            {
+                sanitizer.AllowedTags.Remove("script");
+            }
 
             docToRender = sanitizer.Sanitize(docToRender);
 
@@ -195,5 +199,49 @@ namespace Core.Services.AnnotationService
         {
             return text.Contains("<!DOCTYPE html>");
         }
+
+        public void AddAnnotationInstance(Guid annotationId, Guid userId, ERole userRole, AnnotationInstanceAddRequest request)
+        {
+            var annotation = context.Annotations
+               .Where(a => a.Id == annotationId)
+               .Include(a => a.User)
+               .Include(a => a.Document).ThenInclude(d => d.Content)
+               .First();
+
+            if (userRole < ERole.ADMINISTRATOR)
+            {
+                if (annotation.User.Id != userId)
+                {
+                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
+                }
+            }
+
+            AnnotationTag annotationTag = new()
+            {
+                Annotation = annotation,
+                Instance = request.InstanceId == null ? Guid.NewGuid() : request.InstanceId.Value,
+                Length = request.Length,
+                Position = request.Position,
+                Note = ""
+            };
+
+            if (request.Type == ETagType.TAG)
+            {
+                annotationTag.Tag = context.Tags.Where(t => t.Id == request.Id).Single();
+                annotationTag.SubTag = null;
+            }
+            else if (request.Type == ETagType.SUBTAG)
+            {
+                var subTag = context.SubTags.Where(st => st.Id == request.Id).Include(st => st.Tag).Single();
+                annotationTag.SubTag = subTag;
+                annotationTag.Tag = subTag.Tag;
+            }
+            else
+            {
+                throw new ArgumentException($"Unknown tag type {request.Type}");
+            }
+
+            context.SaveChanges();
+        }
     }
 }
diff --git a/Backend/Core/Services/AnnotationService/IAnnotationService.cs b/Backend/Core/Services/AnnotationService/IAnnotationService.cs
index afb584d..5946e9c 100644
--- a/Backend/Core/Services/AnnotationService/IAnnotationService.cs
+++ b/Backend/Core/Services/AnnotationService/IAnnotationService.cs
@@ -13,5 +13,7 @@ namespace Core.Services.AnnotationService
         public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid userId);
         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);
+
     }
 }
diff --git a/Backend/Models/Annotations/AnnotationInstanceAddRequest.cs b/Backend/Models/Annotations/AnnotationInstanceAddRequest.cs
new file mode 100644
index 0000000..a56ff8f
--- /dev/null
+++ b/Backend/Models/Annotations/AnnotationInstanceAddRequest.cs
@@ -0,0 +1,20 @@
+﻿using Models.Enums;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Models.Annotations
+{
+    public class AnnotationInstanceAddRequest
+    {
+        public int Position { get; set; }
+        public int Length { get; set; }
+        public ETagType Type { get; set; }
+        public Guid Id { get; set; }
+        
+        // If se to null, the instance is a new one 
+        public Guid? InstanceId { get; set; }
+    }
+}
diff --git a/Backend/Models/Enums/ETagType.cs b/Backend/Models/Enums/ETagType.cs
new file mode 100644
index 0000000..18686de
--- /dev/null
+++ b/Backend/Models/Enums/ETagType.cs
@@ -0,0 +1,14 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Models.Enums
+{
+    public enum ETagType
+    {
+        TAG,
+        SUBTAG
+    }
+}
diff --git a/Backend/Models/Tags/TagInstanceInfo.cs b/Backend/Models/Tags/TagInstanceInfo.cs
index 204a690..3c5cbcc 100644
--- a/Backend/Models/Tags/TagInstanceInfo.cs
+++ b/Backend/Models/Tags/TagInstanceInfo.cs
@@ -15,7 +15,7 @@ namespace Models.Tags
         public string? SubTagName { get; set; }
         public Guid? SubTagId { get; set; }
 
-        public int Instance { get; set; }
+        public Guid Instance { get; set; }
         public int Position { get; set; }
         public int Length { get; set; }
 
