SKE-39 add competitions to database

This commit is contained in:
Przemysław Stawujak 2018-12-03 21:36:11 +01:00
parent 0248cf639d
commit e13a951ded
6 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,34 @@
using Abp.Domain.Entities.Auditing;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using SystemKonkursow.Authorization.Users;
namespace SystemKonkursow.Domain
{
[Table("Competitions")]
public class Competition : FullAuditedEntity<long>
{
public Competition()
{
CreationTime = DateTime.Now;
}
public string Name { get; set; }
public int CompetitionCategoryId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string Description { get; set; }
public string Prize { get; set; }
[ForeignKey(nameof(CompetitionCategoryId))]
public virtual CompetitionCategory CompetitionCategory { get; set; }
[ForeignKey(nameof(CreatorUserId))]
public virtual User Creator { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using Abp.Domain.Entities;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace SystemKonkursow.Domain
{
[Table("CompetitionCategories")]
public class CompetitionCategory : Entity<int>
{
public string Name { get; set; }
public virtual ICollection<Competition> Competitions { get; set; }
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace SystemKonkursow.EntityFrameworkCore.Seed
{
public class CompetitionBuilder
{
private const int CompetitionsNumber = 10;
private readonly SystemKonkursowDbContext _context;
public CompetitionBuilder(SystemKonkursowDbContext context)
{
_context = context;
}
public void Create()
{
if (null != _context.CompetitionCategories.FirstOrDefault())
return;
CreateOneCategory("Matematyka");
CreateOneCategory("Biologia");
CreateOneCategory("Chemia");
CreateOneCategory("Fizyka");
CreateOneCategory("Geografia");
CreateOneCategory("Język polski");
CreateOneCategory("Język angielski");
CreateOneCategory("Język niemiecki");
CreateOneCategory("Historia");
CreateOneCategory("Informatyka");
}
private void CreateOneCategory(string name)
{
Random rnd = new Random();
var competitions = new List<Domain.Competition> { };
for (int competitionId = 1; competitionId < CompetitionsNumber; competitionId++)
{
competitions.Add(new Domain.Competition
{
CreatorUserId = 3,
Name = name + " : konkurs nr " + competitionId,
StartDate = new DateTime(2019, rnd.Next(1, 4), rnd.Next(1, 29)),
EndDate = new DateTime(2019, rnd.Next(4, 7), rnd.Next(1, 31)),
Description = "Opis konkursu nr " + competitionId + " z kategorii: " + name,
Prize = "Opis nagród dla konkursu nr " + competitionId + " z kategorii: " + name
});
}
var exampleCategory = new Domain.CompetitionCategory()
{
Name = name,
Competitions = competitions
};
_context.CompetitionCategories.Add(exampleCategory);
_context.SaveChanges();
}
}
}

View File

@ -28,6 +28,7 @@ namespace SystemKonkursow.EntityFrameworkCore.Seed
new DefaultTenantBuilder(context).Create();
new TenantRoleAndUserBuilder(context, 1).Create();
new UserBuilder(context).Create();
new CompetitionBuilder(context).Create();
}
private static void WithDbContext<TDbContext>(IIocResolver iocResolver, Action<TDbContext> contextAction)

View File

@ -13,6 +13,12 @@ namespace SystemKonkursow.EntityFrameworkCore
public SystemKonkursowDbContext(DbContextOptions<SystemKonkursowDbContext> options)
: base(options)
{
}
public DbSet<Domain.CompetitionCategory> CompetitionCategories { get; set; }
public DbSet<Domain.Competition> Competitions { get; set; }
}
}

View File

@ -1008,6 +1008,60 @@ namespace SystemKonkursow.Migrations
b.ToTable("AbpUsers");
});
modelBuilder.Entity("SystemKonkursow.Domain.Competition", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<int>("CompetitionCategoryId");
b.Property<DateTime>("CreationTime");
b.Property<long?>("CreatorUserId");
b.Property<long?>("DeleterUserId");
b.Property<DateTime?>("DeletionTime");
b.Property<string>("Description");
b.Property<DateTime>("EndDate");
b.Property<bool>("IsDeleted");
b.Property<DateTime?>("LastModificationTime");
b.Property<long?>("LastModifierUserId");
b.Property<string>("Name");
b.Property<string>("Prize");
b.Property<DateTime>("StartDate");
b.HasKey("Id");
b.HasIndex("CompetitionCategoryId");
b.HasIndex("CreatorUserId");
b.ToTable("Competitions");
});
modelBuilder.Entity("SystemKonkursow.Domain.CompetitionCategory", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
b.Property<string>("Name");
b.HasKey("Id");
b.ToTable("CompetitionCategories");
});
modelBuilder.Entity("SystemKonkursow.MultiTenancy.Tenant", b =>
{
b.Property<int>("Id")
@ -1209,6 +1263,18 @@ namespace SystemKonkursow.Migrations
.HasForeignKey("LastModifierUserId");
});
modelBuilder.Entity("SystemKonkursow.Domain.Competition", b =>
{
b.HasOne("SystemKonkursow.Domain.CompetitionCategory", "CompetitionCategory")
.WithMany("Competitions")
.HasForeignKey("CompetitionCategoryId")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("SystemKonkursow.Authorization.Users.User", "Creator")
.WithMany()
.HasForeignKey("CreatorUserId");
});
modelBuilder.Entity("SystemKonkursow.MultiTenancy.Tenant", b =>
{
b.HasOne("SystemKonkursow.Authorization.Users.User", "CreatorUser")