Skip to content

Commit 4ce70b9

Browse files
author
Roman Eriksen
committed
Cohorts with seeding
1 parent 9d5aa49 commit 4ce70b9

File tree

17 files changed

+713
-54
lines changed

17 files changed

+713
-54
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using exercise.wwwapi.DTOs.GetUsers;
2+
using exercise.wwwapi.Models;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
5+
namespace exercise.wwwapi.DTOs.Cohort
6+
{
7+
public class CohortCourseUserDTO
8+
{
9+
public string Cohort { get; set; }
10+
public string Course { get; set; }
11+
public UserBasicDTO User { get; set; }
12+
}
13+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
namespace exercise.wwwapi.DTOs.Cohort
1+
using exercise.wwwapi.DTOs.GetUsers;
2+
3+
namespace exercise.wwwapi.DTOs.Cohort
24
{
35
public class CohortDTO
46
{
7+
public int Id { get; set; }
58
public string Title { get; set; }
6-
7-
public ICollection<UserCohortDTO> Students { get; set; } = new List<UserCohortDTO>();
8-
9-
public ICollection<UserCohortDTO> Teachers { get; set; } = new List<UserCohortDTO>();
9+
public ICollection<CourseInCohortDTO> Courses { get; set; } = new List<CourseInCohortDTO>();
1010
}
1111
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using exercise.wwwapi.DTOs.GetUsers;
2+
3+
namespace exercise.wwwapi.DTOs.Cohort
4+
{
5+
public class CourseInCohortDTO
6+
{
7+
public string Title { get; set; }
8+
public ICollection<UserBasicDTO> Students { get; set; } = new List<UserBasicDTO>();
9+
public ICollection<UserBasicDTO> Teachers { get; set; } = new List<UserBasicDTO>();
10+
//public ICollection<UserCohortDTO> Students { get; set; } = new List<UserCohortDTO>();
11+
//public ICollection<UserCohortDTO> Teachers { get; set; } = new List<UserCohortDTO>();
12+
}
13+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using exercise.wwwapi.Models;
2+
3+
namespace exercise.wwwapi.Data
4+
{
5+
public class CohortCourseData
6+
{
7+
private List<string> _courseNames = new List<string>()
8+
{
9+
"Software Development",
10+
"Front-End Development",
11+
"Data Analytics"
12+
};
13+
private List<string> _cohortNames = new List<string>()
14+
{
15+
"Cohort 1",
16+
"Cohort 2",
17+
"Best Cohort (Nigel's Cohort)",
18+
"Cohort 4",
19+
"Cohort 5",
20+
};
21+
private List<Course> _courses = new List<Course>();
22+
private List<Cohort> _cohorts = new List<Cohort>();
23+
private List<CohortCourse> _cohortCourses = new List<CohortCourse>();
24+
private List<CohortCourseUser> _cohortCourseUsers = new List<CohortCourseUser>();
25+
public CohortCourseData(List<User> users)
26+
{
27+
Random random = new Random(1);
28+
29+
for (int x = 0; x < _courseNames.Count; x++)
30+
{
31+
Course course = new Course() { Id = x+1 , Title = _courseNames[x] };
32+
_courses.Add(course);
33+
}
34+
35+
for (int x = 0; x < _cohortNames.Count; x++)
36+
{
37+
Cohort cohort = new Cohort() { Id = x+1 , Title = _cohortNames[x] };
38+
_cohorts.Add(cohort);
39+
}
40+
41+
foreach (var cohort in _cohorts)
42+
{
43+
foreach (var course in _courses)
44+
{
45+
CohortCourse cc = new CohortCourse()
46+
{
47+
CohortId = cohort.Id,
48+
CourseId = course.Id
49+
};
50+
_cohortCourses.Add(cc);
51+
}
52+
}
53+
54+
foreach (var user in users)
55+
{
56+
var cc = _cohortCourses[random.Next(_cohortCourses.Count)];
57+
CohortCourseUser ccu = new CohortCourseUser()
58+
{
59+
CohortId = cc.CohortId,
60+
CourseId = cc.CourseId,
61+
UserId = user.Id
62+
};
63+
_cohortCourseUsers.Add(ccu);
64+
}
65+
}
66+
public List<Course> Courses { get { return _courses; } }
67+
public List<Cohort> Cohorts { get { return _cohorts; } }
68+
public List<CohortCourse> CohortCourses { get { return _cohortCourses; } }
69+
public List<CohortCourseUser> CohortCourseUsers { get { return _cohortCourseUsers; } }
70+
71+
}
72+
}

exercise.wwwapi/Data/DataContext.cs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,41 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
1919

2020
protected override void OnModelCreating(ModelBuilder modelBuilder)
2121
{
22-
22+
#region CohortCourse
23+
// Composite key for CohortCourse
24+
modelBuilder.Entity<CohortCourse>()
25+
.HasKey(cc => new { cc.CohortId, cc.CourseId });
26+
27+
modelBuilder.Entity<CohortCourseUser>()
28+
.HasKey(ccu => new { ccu.CohortId, ccu.CourseId, ccu.UserId });
29+
30+
// Relationships
31+
modelBuilder.Entity<CohortCourse>()
32+
.HasOne(cc => cc.Cohort)
33+
.WithMany(c => c.CohortCourses)
34+
.HasForeignKey(cc => cc.CohortId);
35+
36+
modelBuilder.Entity<CohortCourse>()
37+
.HasOne(cc => cc.Course)
38+
.WithMany(c => c.CohortCourses)
39+
.HasForeignKey(cc => cc.CourseId);
40+
41+
modelBuilder.Entity<CohortCourseUser>()
42+
.HasOne(ccu => ccu.Cohort)
43+
.WithMany() // ← Specify the inverse navigation
44+
.HasForeignKey(ccu => ccu.CohortId);
45+
46+
modelBuilder.Entity<CohortCourseUser>()
47+
.HasOne(ccu => ccu.Course)
48+
.WithMany() // ← Specify the inverse navigation
49+
.HasForeignKey(ccu => ccu.CourseId);
50+
51+
modelBuilder.Entity<CohortCourseUser>()
52+
.HasOne(ccu => ccu.User)
53+
.WithMany(u => u.CohortCourseUsers) // ← Specify the inverse navigation
54+
.HasForeignKey(ccu => ccu.UserId);
55+
56+
#endregion CohortCourse
2357

2458
modelBuilder.Entity<UserCohort>()
2559
.HasKey(uc => new { uc.UserId, uc.CohortId });
@@ -29,45 +63,35 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2963
.Property(u => u.Role)
3064
.HasConversion<string>();
3165

32-
33-
//modelBuilder.Entity<User>()
34-
// .HasMany(u => u.Post)
35-
// .WithOne(p => p.User)
36-
// .HasForeignKey(p => p.UserId)
37-
// .OnDelete(DeleteBehavior.Cascade);
38-
3966
modelBuilder.Entity<Post>()
4067
.HasMany(p => p.Comments)
4168
.WithOne(c => c.Post)
4269
.HasForeignKey(c => c.PostId)
4370
.OnDelete(DeleteBehavior.Cascade);
4471

45-
//modelBuilder.Entity<UserCohort>()
46-
// .HasKey(tc => new { tc.UserId, tc.CohortId });
47-
48-
//modelBuilder.Entity<UserCohort>()
49-
// .HasOne(tc => tc.User)
50-
// .WithMany(u => u.TeacherCohorts)
51-
// .HasForeignKey(tc => tc.UserId);
52-
53-
//modelBuilder.Entity<UserCohort>()
54-
// .HasOne(tc => tc.Cohort)
55-
// .WithMany(c => c.TeacherCohorts)
56-
// .HasForeignKey(tc => tc.CohortId);
57-
5872
// seed users
5973
PersonData personData = new PersonData();
74+
// seed cohorts with courses
75+
CohortCourseData cohortCourseData = new CohortCourseData(personData.Users);
76+
// seed posts
6077
PostData postData = new PostData(personData.Users);
6178
PostCommentData postCommentData = new PostCommentData(postData.Posts, personData.Users);
6279
modelBuilder.Entity<User>().HasData(personData.Users);
6380
modelBuilder.Entity<Post>().HasData(postData.Posts);
6481
modelBuilder.Entity<PostComment>().HasData(postCommentData.Comments);
82+
modelBuilder.Entity<Course>().HasData(cohortCourseData.Courses);
83+
modelBuilder.Entity<Cohort>().HasData(cohortCourseData.Cohorts);
84+
modelBuilder.Entity<CohortCourse>().HasData(cohortCourseData.CohortCourses);
85+
modelBuilder.Entity<CohortCourseUser>().HasData(cohortCourseData.CohortCourseUsers);
6586

6687
}
6788
public DbSet<User> Users { get; set; }
6889
public DbSet<Post> Posts { get; set; }
6990
public DbSet<PostComment> PostComments { get; set; }
7091
public DbSet<Cohort> Cohorts { get; set; }
92+
public DbSet<Course> Courses { get; set; }
93+
public DbSet<CohortCourse> CohortCourses { get; set; }
94+
public DbSet<CohortCourseUser> CohortCourseUsers { get; set; }
7195
public DbSet<UserCohort> UserCohorts { get; set; }
7296
}
7397
}

0 commit comments

Comments
 (0)