commit ebb51a7fe75758a27c3285eb275580c2d9ac2eed
Author: Vojtěch Bartička <aitakaitov@gmail.com>
Date:   Fri Apr 1 20:44:16 2022 +0200

    Added seeding, cleaned up code

diff --git a/Backend/Backend/Controllers/WeatherForecastController.cs b/Backend/Backend/Controllers/WeatherForecastController.cs
index 94dcf69..a1eee59 100644
--- a/Backend/Backend/Controllers/WeatherForecastController.cs
+++ b/Backend/Backend/Controllers/WeatherForecastController.cs
@@ -27,39 +27,7 @@ namespace Backend.Controllers
 
         [HttpGet(Name = "GetWeatherForecast")]
         public IEnumerable<WeatherForecast> Get()
-        {
-            DocumentContent content = new DocumentContent() { Content = "sample content" };
-            _databaseContext.DocumentContents.Add(content);
-
-            User user = new User() { Username = "test", Name = "testname", Surname = "testsurname", Password = "password", Role = ERole.ANNOTATOR};
-            _databaseContext.Users.Add(user);
-
-            Document document = new Document() { Content = content, Name = "test", DateAdded = DateTime.Now, Length = 10, RequiredAnnotations = 3, UserAdded = user };
-            _databaseContext.Documents.Add(document);
-            _databaseContext.SaveChanges();
-
-            Annotation annotation = new Annotation() { DateAssigned = DateTime.Now, DateLastChanged = DateTime.Now, Document = document, Note = "",
-             State = EState.NEW, User = user, UserAssigned = user};
-            _databaseContext.Annotations.Add(annotation);
-
-            Class clss = new Class() { Color = "rgb", Description = "desc", Name = "class name" };
-            _databaseContext.Classes.Add(clss);
-
-            annotation.Classes.Add(clss);
-
-            TagCategory category = new TagCategory() { Name = "category", Color = "rgb", Description = "desc" };
-            _databaseContext.TagCategories.Add(category);
-
-            Tag tag = new Tag() { Name = "testtag", Category = category, Description = "tag", Color = "color" };
-            _databaseContext.Tags.Add(tag);
-            SubTag subTag = new SubTag() { Description = "test subtag", Name = "subtag", Tag = tag };
-            _databaseContext.SubTags.Add(subTag);
-
-            AnnotationTag annotationTag = new AnnotationTag() { Annotation = annotation, Instance = 0, Length = 1, Position = 0, Tag = tag, Note = "" };
-            _databaseContext.AnnotationTags.Add(annotationTag);
-
-            _databaseContext.SaveChanges();
-
+        {     
             return Enumerable.Range(1, 5).Select(index => new WeatherForecast
             {
                 Date = DateTime.Now.AddDays(index),
diff --git a/Backend/Backend/Program.cs b/Backend/Backend/Program.cs
index c7a407c..d50cf82 100644
--- a/Backend/Backend/Program.cs
+++ b/Backend/Backend/Program.cs
@@ -1,6 +1,7 @@
 using Core.Contexts;
+using Core.Seeding;
 using Serilog;
- 
+
 
 var builder = WebApplication.CreateBuilder(args);
 
@@ -13,7 +14,7 @@ builder.Services.AddControllers();
 builder.Services.AddEndpointsApiExplorer();
 builder.Services.AddSwaggerGen();
 
-builder.Services.AddDbContext<DatabaseContext>(); 
+builder.Services.AddDbContext<DatabaseContext>();
 
 var app = builder.Build();
 
@@ -36,4 +37,23 @@ app.MapControllers();
 app.UseSerilogRequestLogging();
 app.Logger.LogInformation("Starting up");
 
+// Database seeding
+using (var scope = app.Services.CreateScope())
+{
+    var services = scope.ServiceProvider;
+    var context = scope.ServiceProvider.GetService<DatabaseContext>();
+
+    // In development we seed dummy data
+    if (app.Environment.IsDevelopment())
+    {
+        Seeder.SeedDevelopment(context);
+    }
+    else
+    {
+        // In production we seed administrator 
+        Seeder.SeedProduction(context);
+    }
+
+}
+
 app.Run();
diff --git a/Backend/Backend/appsettings.json b/Backend/Backend/appsettings.json
index 10f68b8..418c201 100644
--- a/Backend/Backend/appsettings.json
+++ b/Backend/Backend/appsettings.json
@@ -5,5 +5,6 @@
       "Microsoft.AspNetCore": "Warning"
     }
   },
-  "AllowedHosts": "*"
+  "AllowedHosts": "*",
+  "ConnectionString": "Host=localhost:5432;Database=dbo;Username=myuser;Password=password"
 }
diff --git a/Backend/Core/Class1.cs b/Backend/Core/Class1.cs
deleted file mode 100644
index fc94686..0000000
--- a/Backend/Core/Class1.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-﻿namespace Core
-{
-    public class Class1
-    {
-
-    }
-}
\ No newline at end of file
diff --git a/Backend/Core/Contexts/DatabaseContext.cs b/Backend/Core/Contexts/DatabaseContext.cs
index 86706f6..07be3c1 100644
--- a/Backend/Core/Contexts/DatabaseContext.cs
+++ b/Backend/Core/Contexts/DatabaseContext.cs
@@ -1,5 +1,6 @@
 ﻿using Core.Entities;
 using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Configuration;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -10,6 +11,13 @@ namespace Core.Contexts
 {
     public class DatabaseContext : DbContext
     {
+        private readonly IConfiguration _configuration;
+
+        public DatabaseContext(IConfiguration configuration)
+        {
+            _configuration = configuration;
+        }
+
         public DbSet<DocumentContent> DocumentContents { get; set; }
         public DbSet<Document> Documents { get; set; }
         public DbSet<User> Users { get; set; }
@@ -23,19 +31,7 @@ namespace Core.Contexts
 
         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {
-            optionsBuilder.UseNpgsql("Host=localhost:5432;Database=dbo;Username=myuser;Password=password", b => b.MigrationsAssembly("RestAPI"));
-        }
-
-        protected override void OnModelCreating(ModelBuilder modelBuilder)
-        {
-          /*  modelBuilder.Entity<AnnotationClass>()
-                .HasOne(ac => ac.Annotation)
-                .WithMany(a => a.Classes)
-                .HasForeignKey(ac => ac.AnnotationId);
-            modelBuilder.Entity<AnnotationClass>()
-                .HasOne(ac => ac.Class)
-                .WithMany(c => c.Annotations)
-                .HasForeignKey(ac => ac.ClassId);*/
+            optionsBuilder.UseNpgsql(_configuration["ConnectionString"], b => b.MigrationsAssembly("RestAPI"));
         }
     }
 }
diff --git a/Backend/Core/Core.csproj b/Backend/Core/Core.csproj
index fc403e1..e84c590 100644
--- a/Backend/Core/Core.csproj
+++ b/Backend/Core/Core.csproj
@@ -16,4 +16,8 @@
     <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.3" />
   </ItemGroup>
 
+  <ItemGroup>
+    <Folder Include="Services\" />
+  </ItemGroup>
+
 </Project>
diff --git a/Backend/Core/Seeding/Seeder.cs b/Backend/Core/Seeding/Seeder.cs
new file mode 100644
index 0000000..8cbedba
--- /dev/null
+++ b/Backend/Core/Seeding/Seeder.cs
@@ -0,0 +1,71 @@
+﻿using Core.Contexts;
+using Core.Entities;
+using Core.Enums;
+using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Core.Seeding
+{
+    public class Seeder
+    {
+        public static void SeedDevelopment(DatabaseContext context)
+        {
+            if (!context.Documents.Any())
+            {
+                DocumentContent content = new DocumentContent() { Content = "Dummy content" };
+                context.DocumentContents.Add(content);
+
+                User user = new User() { Username = "testuser", Name = "Test", Surname = "User", Password = "password", Role = ERole.ANNOTATOR };
+                context.Users.Add(user);
+
+                Document document = new Document() { Content = content, Name = "Test document", DateAdded = DateTime.Now, Length = 10, RequiredAnnotations = 3, UserAdded = user };
+                context.Documents.Add(document);
+                context.SaveChanges();
+
+                Annotation annotation = new Annotation()
+                {
+                    DateAssigned = DateTime.Now,
+                    DateLastChanged = DateTime.Now,
+                    Document = document,
+                    Note = "",
+                    State = EState.NEW,
+                    User = user,
+                    UserAssigned = user
+                };
+                context.Annotations.Add(annotation);
+
+                Class clss = new Class() { Color = "rgb(...)", Description = "Dummy description", Name = "Dummy class name" };
+                context.Classes.Add(clss);
+
+                annotation.Classes.Add(clss);
+
+                TagCategory category = new TagCategory() { Name = "Dummy category", Color = "rgb(...)", Description = "Dummy category description" };
+                context.TagCategories.Add(category);
+
+                Tag tag = new Tag() { Name = "Dummy tag", Category = category, Description = "Dummy tag", Color = "rgb(...)" };
+                context.Tags.Add(tag);
+                SubTag subTag = new SubTag() { Description = "Dummy subtag", Name = "Dummy subtag", Tag = tag };
+                context.SubTags.Add(subTag);
+
+                AnnotationTag annotationTag = new AnnotationTag() { Annotation = annotation, Instance = 0, Length = 1, Position = 0, Tag = tag, Note = "Dummy note" };
+                context.AnnotationTags.Add(annotationTag);
+
+                context.SaveChanges();
+            }
+        }
+
+        public static void SeedProduction(DatabaseContext context)
+        {
+            if (!context.Documents.Any())
+            {
+                User admin = new User() { Name = "Admin", Surname = "Admin", Role = ERole.ADMINISTRATOR, Username = "admin", Password = "123a456b789c" };
+                context.Users.Add(admin);
+                context.SaveChanges();
+            }
+        }
+    }
+}
