Projekt

Obecné

Profil

« Předchozí | Další » 

Revize ebb51a7f

Přidáno uživatelem Vojtěch Bartička před více než 2 roky(ů)

Added seeding, cleaned up code

Zobrazit rozdíly:

Backend/Backend/Controllers/WeatherForecastController.cs
27 27

  
28 28
        [HttpGet(Name = "GetWeatherForecast")]
29 29
        public IEnumerable<WeatherForecast> Get()
30
        {
31
            DocumentContent content = new DocumentContent() { Content = "sample content" };
32
            _databaseContext.DocumentContents.Add(content);
33

  
34
            User user = new User() { Username = "test", Name = "testname", Surname = "testsurname", Password = "password", Role = ERole.ANNOTATOR};
35
            _databaseContext.Users.Add(user);
36

  
37
            Document document = new Document() { Content = content, Name = "test", DateAdded = DateTime.Now, Length = 10, RequiredAnnotations = 3, UserAdded = user };
38
            _databaseContext.Documents.Add(document);
39
            _databaseContext.SaveChanges();
40

  
41
            Annotation annotation = new Annotation() { DateAssigned = DateTime.Now, DateLastChanged = DateTime.Now, Document = document, Note = "",
42
             State = EState.NEW, User = user, UserAssigned = user};
43
            _databaseContext.Annotations.Add(annotation);
44

  
45
            Class clss = new Class() { Color = "rgb", Description = "desc", Name = "class name" };
46
            _databaseContext.Classes.Add(clss);
47

  
48
            annotation.Classes.Add(clss);
49

  
50
            TagCategory category = new TagCategory() { Name = "category", Color = "rgb", Description = "desc" };
51
            _databaseContext.TagCategories.Add(category);
52

  
53
            Tag tag = new Tag() { Name = "testtag", Category = category, Description = "tag", Color = "color" };
54
            _databaseContext.Tags.Add(tag);
55
            SubTag subTag = new SubTag() { Description = "test subtag", Name = "subtag", Tag = tag };
56
            _databaseContext.SubTags.Add(subTag);
57

  
58
            AnnotationTag annotationTag = new AnnotationTag() { Annotation = annotation, Instance = 0, Length = 1, Position = 0, Tag = tag, Note = "" };
59
            _databaseContext.AnnotationTags.Add(annotationTag);
60

  
61
            _databaseContext.SaveChanges();
62

  
30
        {     
63 31
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
64 32
            {
65 33
                Date = DateTime.Now.AddDays(index),
Backend/Backend/Program.cs
1 1
using Core.Contexts;
2
using Core.Seeding;
2 3
using Serilog;
3
 
4

  
4 5

  
5 6
var builder = WebApplication.CreateBuilder(args);
6 7

  
......
13 14
builder.Services.AddEndpointsApiExplorer();
14 15
builder.Services.AddSwaggerGen();
15 16

  
16
builder.Services.AddDbContext<DatabaseContext>(); 
17
builder.Services.AddDbContext<DatabaseContext>();
17 18

  
18 19
var app = builder.Build();
19 20

  
......
36 37
app.UseSerilogRequestLogging();
37 38
app.Logger.LogInformation("Starting up");
38 39

  
40
// Database seeding
41
using (var scope = app.Services.CreateScope())
42
{
43
    var services = scope.ServiceProvider;
44
    var context = scope.ServiceProvider.GetService<DatabaseContext>();
45

  
46
    // In development we seed dummy data
47
    if (app.Environment.IsDevelopment())
48
    {
49
        Seeder.SeedDevelopment(context);
50
    }
51
    else
52
    {
53
        // In production we seed administrator 
54
        Seeder.SeedProduction(context);
55
    }
56

  
57
}
58

  
39 59
app.Run();
Backend/Backend/appsettings.json
5 5
      "Microsoft.AspNetCore": "Warning"
6 6
    }
7 7
  },
8
  "AllowedHosts": "*"
8
  "AllowedHosts": "*",
9
  "ConnectionString": "Host=localhost:5432;Database=dbo;Username=myuser;Password=password"
9 10
}
Backend/Core/Class1.cs
1
namespace Core
2
{
3
    public class Class1
4
    {
5

  
6
    }
7
}
Backend/Core/Contexts/DatabaseContext.cs
1 1
using Core.Entities;
2 2
using Microsoft.EntityFrameworkCore;
3
using Microsoft.Extensions.Configuration;
3 4
using System;
4 5
using System.Collections.Generic;
5 6
using System.Linq;
......
10 11
{
11 12
    public class DatabaseContext : DbContext
12 13
    {
14
        private readonly IConfiguration _configuration;
15

  
16
        public DatabaseContext(IConfiguration configuration)
17
        {
18
            _configuration = configuration;
19
        }
20

  
13 21
        public DbSet<DocumentContent> DocumentContents { get; set; }
14 22
        public DbSet<Document> Documents { get; set; }
15 23
        public DbSet<User> Users { get; set; }
......
23 31

  
24 32
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
25 33
        {
26
            optionsBuilder.UseNpgsql("Host=localhost:5432;Database=dbo;Username=myuser;Password=password", b => b.MigrationsAssembly("RestAPI"));
27
        }
28

  
29
        protected override void OnModelCreating(ModelBuilder modelBuilder)
30
        {
31
          /*  modelBuilder.Entity<AnnotationClass>()
32
                .HasOne(ac => ac.Annotation)
33
                .WithMany(a => a.Classes)
34
                .HasForeignKey(ac => ac.AnnotationId);
35
            modelBuilder.Entity<AnnotationClass>()
36
                .HasOne(ac => ac.Class)
37
                .WithMany(c => c.Annotations)
38
                .HasForeignKey(ac => ac.ClassId);*/
34
            optionsBuilder.UseNpgsql(_configuration["ConnectionString"], b => b.MigrationsAssembly("RestAPI"));
39 35
        }
40 36
    }
41 37
}
Backend/Core/Core.csproj
16 16
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.3" />
17 17
  </ItemGroup>
18 18

  
19
  <ItemGroup>
20
    <Folder Include="Services\" />
21
  </ItemGroup>
22

  
19 23
</Project>
Backend/Core/Seeding/Seeder.cs
1
using Core.Contexts;
2
using Core.Entities;
3
using Core.Enums;
4
using Microsoft.EntityFrameworkCore;
5
using System;
6
using System.Collections.Generic;
7
using System.Linq;
8
using System.Text;
9
using System.Threading.Tasks;
10

  
11
namespace Core.Seeding
12
{
13
    public class Seeder
14
    {
15
        public static void SeedDevelopment(DatabaseContext context)
16
        {
17
            if (!context.Documents.Any())
18
            {
19
                DocumentContent content = new DocumentContent() { Content = "Dummy content" };
20
                context.DocumentContents.Add(content);
21

  
22
                User user = new User() { Username = "testuser", Name = "Test", Surname = "User", Password = "password", Role = ERole.ANNOTATOR };
23
                context.Users.Add(user);
24

  
25
                Document document = new Document() { Content = content, Name = "Test document", DateAdded = DateTime.Now, Length = 10, RequiredAnnotations = 3, UserAdded = user };
26
                context.Documents.Add(document);
27
                context.SaveChanges();
28

  
29
                Annotation annotation = new Annotation()
30
                {
31
                    DateAssigned = DateTime.Now,
32
                    DateLastChanged = DateTime.Now,
33
                    Document = document,
34
                    Note = "",
35
                    State = EState.NEW,
36
                    User = user,
37
                    UserAssigned = user
38
                };
39
                context.Annotations.Add(annotation);
40

  
41
                Class clss = new Class() { Color = "rgb(...)", Description = "Dummy description", Name = "Dummy class name" };
42
                context.Classes.Add(clss);
43

  
44
                annotation.Classes.Add(clss);
45

  
46
                TagCategory category = new TagCategory() { Name = "Dummy category", Color = "rgb(...)", Description = "Dummy category description" };
47
                context.TagCategories.Add(category);
48

  
49
                Tag tag = new Tag() { Name = "Dummy tag", Category = category, Description = "Dummy tag", Color = "rgb(...)" };
50
                context.Tags.Add(tag);
51
                SubTag subTag = new SubTag() { Description = "Dummy subtag", Name = "Dummy subtag", Tag = tag };
52
                context.SubTags.Add(subTag);
53

  
54
                AnnotationTag annotationTag = new AnnotationTag() { Annotation = annotation, Instance = 0, Length = 1, Position = 0, Tag = tag, Note = "Dummy note" };
55
                context.AnnotationTags.Add(annotationTag);
56

  
57
                context.SaveChanges();
58
            }
59
        }
60

  
61
        public static void SeedProduction(DatabaseContext context)
62
        {
63
            if (!context.Documents.Any())
64
            {
65
                User admin = new User() { Name = "Admin", Surname = "Admin", Role = ERole.ADMINISTRATOR, Username = "admin", Password = "123a456b789c" };
66
                context.Users.Add(admin);
67
                context.SaveChanges();
68
            }
69
        }
70
    }
71
}

Také k dispozici: Unified diff