From f31c58045dfeb338d08236fe329f971805419e80 Mon Sep 17 00:00:00 2001 From: Johan Reitan Date: Mon, 22 Sep 2025 14:30:28 +0200 Subject: [PATCH] added new seeder + some changes in program.cs --- exercise.wwwapi/DTOs/Users/UserDTO.cs | 38 +- exercise.wwwapi/Data/DataContext.cs | 3 +- exercise.wwwapi/Data/ModelSeeder.cs | 1234 ++++++++++++-------- exercise.wwwapi/Endpoints/UserEndpoints.cs | 35 +- exercise.wwwapi/Factories/UserFactory.cs | 1 - exercise.wwwapi/Program.cs | 1 + 6 files changed, 817 insertions(+), 495 deletions(-) diff --git a/exercise.wwwapi/DTOs/Users/UserDTO.cs b/exercise.wwwapi/DTOs/Users/UserDTO.cs index 5f33c7a..6c8f93b 100644 --- a/exercise.wwwapi/DTOs/Users/UserDTO.cs +++ b/exercise.wwwapi/DTOs/Users/UserDTO.cs @@ -1,5 +1,6 @@ using exercise.wwwapi.DTOs.Notes; using exercise.wwwapi.Enums; +using exercise.wwwapi.Factories; using exercise.wwwapi.Models; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json.Serialization; @@ -7,10 +8,7 @@ namespace exercise.wwwapi.DTOs.Users; public class UserDTO -{ - [JsonPropertyName("id")] - public int Id { get; set; } - +{ [JsonPropertyName("email")] public string Email { get; set; } @@ -35,6 +33,12 @@ public class UserDTO [JsonPropertyName("specialism")] public Specialism? Specialism { get; set; } + public int? CohortId { get; set; } + + public DateTime? CurrentStartdate { get; set; } + public DateTime? CurrentEnddate { get; set; } + + [JsonPropertyName("notes")] public ICollection Notes { get; set; } = new List(); @@ -48,7 +52,6 @@ public UserDTO() public UserDTO(User model) { - Id = model.Id; Email = model.Email; FirstName = model.FirstName; LastName = model.LastName; @@ -58,6 +61,31 @@ public UserDTO(User model) Mobile = model.Mobile; Specialism = model.Specialism; Role = model.Role.ToString(); + CohortId = model.User_CC.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc + CurrentStartdate = model.User_CC.LastOrDefault().CohortCourse.Cohort.StartDate; //autofetching the first element of usercc + CurrentEnddate = model.User_CC.LastOrDefault().CohortCourse.Cohort.EndDate; //autofetching the first element of usercc Notes = model.Notes.Select(n => new NoteDTO(n)).ToList(); } + + public UserDTO(User model, PrivilegeLevel privilegeLevel) + { + Email = model.Email; + FirstName = model.FirstName; + LastName = model.LastName; + Bio = model.Bio; + Github = model.Github; + Username = model.Username; + Mobile = model.Mobile; + Specialism = model.Specialism; + Role = model.Role.ToString(); + CohortId = model.User_CC.LastOrDefault()?.CohortCourse.CohortId; //autofetching the first element of usercc + CurrentStartdate = model.User_CC.LastOrDefault().CohortCourse.Cohort.StartDate; //autofetching the first element of usercc + CurrentEnddate = model.User_CC.LastOrDefault().CohortCourse.Cohort.EndDate; //autofetching the first element of usercc + + + if (privilegeLevel == PrivilegeLevel.Teacher) + { + Notes = model.Notes.Select(n => new NoteDTO(n)).ToList(); + } + } } \ No newline at end of file diff --git a/exercise.wwwapi/Data/DataContext.cs b/exercise.wwwapi/Data/DataContext.cs index 7bca99f..97c20cc 100644 --- a/exercise.wwwapi/Data/DataContext.cs +++ b/exercise.wwwapi/Data/DataContext.cs @@ -13,6 +13,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); - ModelSeeder.Seed(modelBuilder); + var seeder = new ModelSeeder(); + seeder.Seed(modelBuilder); } } \ No newline at end of file diff --git a/exercise.wwwapi/Data/ModelSeeder.cs b/exercise.wwwapi/Data/ModelSeeder.cs index ac859ce..be8c7bb 100644 --- a/exercise.wwwapi/Data/ModelSeeder.cs +++ b/exercise.wwwapi/Data/ModelSeeder.cs @@ -2,10 +2,11 @@ using exercise.wwwapi.Models; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.EntityFrameworkCore; +using System.Net.Sockets; namespace exercise.wwwapi.Data; -public static class ModelSeeder +public class ModelSeeder { private static readonly DateTime _seedTime = new DateTime(2025, 01, 01, 0, 0, 0, DateTimeKind.Utc); @@ -15,510 +16,797 @@ public static class ModelSeeder "$2a$11$MYFrTWP6v64imGdsbibutOW/DSZiu3wg5rWR1Nm5Zjb5XBNut5HKq", // Test2test2% "$2a$11$JyMDiDHwh8hrcjNmp0zb8uZGFettl5dyJ3FDa3S5iOCTYnDn6GZqm", // Test3test3% "$2a$11$.daNf2PApH/oqC8MGCQq5uHqw2zmjmIiIB8A6WZ/nLXjbI4iuQsEW", // Test4test4% - "$2a$11$HmeURzynKz6PqTVeZxfDIeg6MRpzI/5ZAY1GyHW0hJuNUvv7ixOOO" // Test5test5% + "$2a$11$HmeURzynKz6PqTVeZxfDIeg6MRpzI/5ZAY1GyHW0hJuNUvv7ixOOO", // Test5test5% + "$2a$11$bUz6SRMx6L3gjVLtV7cKOO2R46tGJNrhF.myYyP2Odu5deLMRmfh6" // Password123! ]; + private List _firstnames = new List() + { + "Audrey", + "Donald", + "Elvis", + "Barack", + "Oprah", + "Jimi", + "Mick", + "Kate", + "Charles", + "Kate", + "Pepo", + "Claus", + "Fred" + }; + private List _lastnames = new List() + { + "Hepburn", + "Trump", + "Presley", + "Obama", + "Winfrey", + "Hendrix", + "Jagger", + "Winslet", + "Windsor", + "Middleton" - public static void Seed(ModelBuilder modelBuilder) - { - SeedUsers(ref modelBuilder); - SeedPosts(ref modelBuilder); - SeedComments(ref modelBuilder); - SeedLikes(ref modelBuilder); - SeedCourses(ref modelBuilder); - SeedCohorts(ref modelBuilder); - SeedModules(ref modelBuilder); - SeedUnits(ref modelBuilder); - SeedExercises(ref modelBuilder); - SeedNotes(ref modelBuilder); - - SeedCourseModules(ref modelBuilder); - SeedUserExercises(ref modelBuilder); - SeedCohortCourses(ref modelBuilder); - SeedUserCC(ref modelBuilder); + }; + private List _domain = new List() + { + "bbc.co.uk", + "google.com", + "theworld.ca", + "something.com", + "tesla.com", + "nasa.org.us", + "gov.us", + "gov.gr", + "gov.nl", + "gov.ru" + }; + private List _firstword = new List() + { + "The", + "Two", + "Several", + "Fifteen", + "A bunch of", + "An army of", + "A herd of" - } - + }; + private List _secondword = new List() + { + "Orange", + "Purple", + "Large", + "Microscopic", + "Green", + "Transparent", + "Rose Smelling", + "Bitter" + }; + private List _thirdword = new List() + { + "Buildings", + "Cars", + "Planets", + "Houses", + "Flowers", + "Leopards" + }; + private List _roles = new List() + { + Role.Student, + Role.Teacher + }; + private List _specialisms = new List() + { + Specialism.Frontend, + Specialism.Backend, + Specialism.Fullstack, + Specialism.None + }; + + private List _firstPart = new List() + { + "The curious fox dashed into the woods", + "Rain tapped gently on the window glass", + "She waited by the old lamp all night", + "A shadow crossed the silent avenue", + "He remembered the promise at dawn", + "Stars blinked over the distant meadow", + "The clock struck midnight with heavy sound", + "Fresh bread scented the empty kitchen", + "Hope lingered near the broken gate", + "Birdsong rose above the misty lake", + "The child clutched his favorite book", + "Leaves fluttered around the cold bench", + "A coin sparkled under the streetlight", + "Bells rang softly through the sleepy town", + "Warm sun dried yesterday’s heavy rain" + }; + + private List _lastPart = new List() + { + "until moonlight danced upon the forest floor.", + "as stories unfolded behind quiet eyes.", + "bringing dreams whispered by the falling leaves.", + "while memories carved footprints in the snow.", + "reminding all of hope’s persistent return.", + "around secret paths only wanderers find.", + "and time felt gentle for a fleeting moment.", + "before laughter echoed down the empty hall.", + "while courage grew beneath trembling hands.", + "singing lost lullabies into the morning air.", + "drowning sorrow under pages full of joy.", + "welcoming travelers searching for home.", + "evoking wishes only midnight could hear.", + "breathing life into ordinary days.", + "ending with promises of another tomorrow." + }; + private List _commentText = new List() +{ + "What a magical night, full of hope and gentle surprises.", + "The quiet rain inspired deep thoughts and sleepy smiles.", + "Sometimes, the smallest coin glimmers brightest in the darkness.", + "She found comfort in the book’s worn pages and simple stories.", + "Even the silent streets carry echoes of laughter from days past.", + "Moonlight always manages to reveal secrets hidden by the sun.", + "I never realized how peaceful the kitchen can be before sunrise.", + "Birdsong on the lake always reminds me of childhood adventures.", + "Courage is found in moments when the gate appears locked.", + "Each bell in this town rings with a promise of new beginnings.", + "The forest floor feels alive during the twilight hours.", + "There is something soothing about bread fresh from the oven.", + "Every leaf that falls brings memories of autumn’s sweetest days.", + "Sometimes, time slows and lets us enjoy the quiet miracles.", + "Her wish was carried by stars shining over the distant meadow.", + "Even in heavy rain, hope waits at every window.", + "Travelers are welcomed here not just by open doors, but by open hearts.", + "Promises whispered at dawn often come true by nightfall.", + "The misty lake holds stories not yet spoken.", + "Ordinary days can become extraordinary with just a smile." +}; + + + private List _users = new List(); + private List _notes = new List(); + private List _posts = new List(); + private List _comments = new List(); + private List _likes = new List(); + private List _cohorts = new List(); + private List _courses = new List(); + private List _cohortCourses = new List(); + private List _userCCs = new List(); + private List _exercises = new List(); + private List _units = new List(); + private List _modules = new List(); + private List _courseModules = new List(); + private List _userExercises = new List(); + - private static void SeedUsers(ref ModelBuilder modelBuilder) + public void Seed(ModelBuilder modelBuilder) { - modelBuilder.Entity().HasData( - new User - { - Id = 1, - Username = "test1", - Email = "test1@test1", - PasswordHash = _passwordHashes[0], - Role = Role.Student, - FirstName = "Lionel", - LastName = "Richie", - Mobile = "1234567890", - Github = "", - Bio = "", - Specialism = Specialism.Frontend, - PhotoUrl = "" - }, - new User - { - Id = 2, - Username = "test2", - Email = "test2@test2", - PasswordHash = _passwordHashes[1], - Role = Role.Teacher, - FirstName = "Michael", - LastName = "Jordan", - Mobile = "1234123", - Github = "", - Bio = "", - Specialism = Specialism.Backend, - PhotoUrl = "" - }, - new User - { - Id = 3, - Username = "test3", - Email = "test3@test3", - PasswordHash = _passwordHashes[2], - Role = Role.Student, - FirstName = "Michael", - LastName = "Johansen", - Mobile = "55555555", - Github = "", - Bio = "", - Specialism = Specialism.Frontend, + + User user1 = new User() + { + Id = 1, + Username = "test1", + Email = "test1@test1", + PasswordHash = _passwordHashes[0], + Role = Role.Student, + FirstName = "Lionel", + LastName = "Richie", + Mobile = "1234567890", + Github = "", + Bio = "", + Specialism = Specialism.Frontend, + PhotoUrl = "" + }; + _users.Add(user1); + + User user2 = new User() + { + Id = 2, + Username = "test2", + Email = "test2@test2", + PasswordHash = _passwordHashes[1], + Role = Role.Teacher, + FirstName = "Michael", + LastName = "Jordan", + Mobile = "1234123", + Github = "", + Bio = "", + Specialism = Specialism.Backend, + PhotoUrl = "" + }; + _users.Add(user2); + + User user3 = new User() + { + Id = 3, + Username = "test3", + Email = "test3@test3", + PasswordHash = _passwordHashes[2], + Role = Role.Student, + FirstName = "Michael", + LastName = "Johansen", + Mobile = "55555555", + Github = "", + Bio = "", + Specialism = Specialism.Frontend, + PhotoUrl = "" + }; + _users.Add(user3); + + User user4 = new User() + { + Id = 4, + Username = "test4", + Email = "test4@test4", + PasswordHash = _passwordHashes[3], + Role = Role.Student, + FirstName = "Michael", + LastName = "Jackson", + Mobile = "98987878", + Github = "", + Bio = "", + Specialism = Specialism.Backend, + PhotoUrl = "" + }; + _users.Add(user4); + + User user5 = new User() + { + Id = 5, + Username = "test5", + Email = "test5@test5", + PasswordHash = _passwordHashes[4], + Role = Role.Teacher, + FirstName = "Johnny", + LastName = "Cash", + Mobile = "111222333", + Github = "", + Bio = "", + Specialism = Specialism.Frontend, + PhotoUrl = "" + }; + _users.Add(user5); + + for (int i = 6; i < 50; i++) + { + Random userRandom = new Random(); + var firstname = _firstnames[userRandom.Next(_firstnames.Count)]; + var lastname = _lastnames[userRandom.Next(_lastnames.Count)]; + var username = $"{firstname}{lastname}{i}"; + User user = new User() + { + Id = i, + FirstName = firstname, + LastName = lastname, + Username = username, + Email = $"{username}@{_domain[userRandom.Next(_domain.Count)]}", + PasswordHash = _passwordHashes[5], + Role = _roles[userRandom.Next(_roles.Count)], + Mobile = userRandom.Next(12345678, 23456789).ToString(), + Github = $"{username}git", + Bio = $"{_firstword[userRandom.Next(_firstword.Count)]}{_secondword[userRandom.Next(_secondword.Count)]}{_thirdword[userRandom.Next(_thirdword.Count)]}", + Specialism = _specialisms[userRandom.Next(_specialisms.Count)], PhotoUrl = "" - } - - ); - } + }; + _users.Add(user); + } - private static void SeedPosts(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Post - { - Id = 1, - AuthorId = 1, - Body = "Post 1 Body", - CreatedAt = _seedTime, - }, - new Post - { - Id = 2, - AuthorId = 2, - Body = "Post 2 Body", - CreatedAt = _seedTime, - }, - new Post - { - Id = 3, - AuthorId = 1, - Body = "Post 3 Body", - CreatedAt = _seedTime, - }, - new Post - { - Id = 4, - AuthorId = 3, - Body = "Post 4 Body", - CreatedAt = _seedTime, - }, - new Post - { - Id = 5, - AuthorId = 3, - Body = "Post 5 Body", - CreatedAt = _seedTime, - } - ); - } + Post post1 = new Post() + { + Id = 1, + AuthorId = 1, + Body = $"{_firstPart[0]} {_lastPart[0]}", + CreatedAt = _seedTime + }; + _posts.Add(post1); + Post post2 = new Post() + { + Id = 2, + AuthorId = 2, + Body = $"{_firstPart[1]} {_lastPart[1]}", + CreatedAt = _seedTime + }; + _posts.Add(post2); + Post post3 = new Post() + { + Id = 3, + AuthorId = 2, + Body = $"{_firstPart[2]} {_lastPart[2]}", + CreatedAt = _seedTime + }; + _posts.Add(post3); + Post post4 = new Post() + { + Id = 4, + AuthorId = 2, + Body = $"{_firstPart[3]} {_lastPart[3]}", + CreatedAt = _seedTime + }; + _posts.Add(post4); + Post post5 = new Post() + { + Id = 5, + AuthorId = 1, + Body = $"{_firstPart[4]} {_lastPart[4]}", + CreatedAt = _seedTime + }; + _posts.Add(post5); - private static void SeedComments(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Comment - { - Id = 1, - PostId = 1, - UserId = 1, - Body = "Comment 1 Body", - CreatedAt = _seedTime, - }, - new Comment - { - Id = 2, - PostId = 2, - UserId = 2, - Body = "Comment 2 Body", - CreatedAt = _seedTime, - }, - new Comment - { - Id = 3, - PostId = 2, - UserId = 3, - Body = "Comment 3 Body", - CreatedAt = _seedTime, - }, - new Comment - { - Id = 4, - PostId = 2, - UserId = 1, - Body = "Comment 4 Body", - CreatedAt = _seedTime, - }, - new Comment - { - Id = 5, - PostId = 3, - UserId = 1, - Body = "Comment 5 Body", + for (int i = 6; i < 20; i++) + { + Random postRandom = new Random(); + Post p = new Post() + { + Id = i, + AuthorId = postRandom.Next(_users.Count), + Body = $"{_firstPart[postRandom.Next(_firstPart.Count)]} {_lastPart[postRandom.Next(_lastPart.Count)]}", + CreatedAt = _seedTime + }; + _posts.Add(p); + } + + Comment comment1 = new Comment() + { + Id = 1, + PostId = 1, + UserId = 1, + Body = "Comment 1 Body", + CreatedAt = _seedTime, + }; + _comments.Add(comment1); + + Comment comment2 = new Comment + { + Id = 2, + PostId = 2, + UserId = 2, + Body = "Comment 2 Body", + CreatedAt = _seedTime, + }; + _comments.Add(comment2); + + Comment comment3 = new Comment + { + Id = 3, + PostId = 2, + UserId = 3, + Body = "Comment 3 Body", + CreatedAt = _seedTime, + }; + _comments.Add(comment3); + + Comment comment4 = new Comment + { + Id = 4, + PostId = 2, + UserId = 1, + Body = "Comment 4 Body", + CreatedAt = _seedTime, + }; + _comments.Add(comment4); + + Comment comment5 = new Comment + { + Id = 5, + PostId = 3, + UserId = 1, + Body = "Comment 5 Body", + CreatedAt = _seedTime, + }; + _comments.Add(comment5); + + for (int i = 6; i < 50; i++) + { + Random commentRandom = new Random(); + int postId = _posts[commentRandom.Next(_posts.Count)].Id; + int userId = _users[commentRandom.Next(_users.Count)].Id; + Comment c = new Comment + { + Id = i, + PostId = postId, + UserId = userId, + Body = _commentText[commentRandom.Next(_commentText.Count)], CreatedAt = _seedTime, - } - ); - } + }; + _comments.Add(c); + } - private static void SeedLikes(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Like - { - Id = 1, - PostId = 1, - UserId = 1 - }, - new Like - { - Id = 2, - PostId = 1, - UserId = 2 - }, - new Like - { - Id = 3, - PostId = 1, - UserId = 3 - } - ); - } + Like like1 = new Like + { + Id = 1, + PostId = 1, + UserId = 1 + }; + _likes.Add(like1); - private static void SeedCourses(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Course - { - Id = 1, - Name = "Course 1", - }, - new Course - { - Id = 2, - Name = "Course 2", - }, - new Course - { - Id = 3, - Name = "Course 3", - }, - new Course - { - Id = 4, - Name = "Course 4", - }, - new Course - { - Id = 5, - Name = "Course 5", - } - ); - } + Like like2 = new Like + { + Id = 2, + PostId = 1, + UserId = 2 + }; + _likes.Add(like2); - private static void SeedCohorts(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Cohort - { - Id = 1, - CohortNumber = 1, - CohortName = "August 2025", - StartDate = new DateTime(2025, 8, 1), - EndDate = new DateTime(2025, 9, 29), - }, - new Cohort + Like like3 = new Like + { + Id = 3, + PostId = 1, + UserId = 3 + }; + _likes.Add(like3); + + /* + for (int i = 4; i < 50; i++) { + Random likeRandom = new Random(); + Like l = new Like { - Id = 2, - CohortNumber = 2, - CohortName = "February 2026", - StartDate = new DateTime(2026, 2, 1), - EndDate = new DateTime(2026, 3, 29), + Id = i, + Postid = } + */ - ); - } + Course course1 = new Course + { + Id = 1, + Name = "Java", + }; + _courses.Add(course1); - private static void SeedCohortCourses(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new CohortCourse - { - Id = 1, - CohortId = 1, - CourseId = 1, - }, - new CohortCourse - { - Id = 2, - CohortId = 1, - CourseId = 2 - }, - new CohortCourse - { - Id = 3, - CohortId = 2, - CourseId = 1 - } + Course course2 = new Course + { + Id = 2, + Name = ".NET", + }; + _courses.Add(course2); + Cohort cohort1 = new Cohort + { + Id = 1, + CohortNumber = 1, + CohortName = "August 2025", + StartDate = new DateTime(2025, 8, 1), + EndDate = new DateTime(2025, 9, 29), + }; + _cohorts.Add(cohort1); - ); - } + Cohort cohort2 = new Cohort + { + Id = 2, + CohortNumber = 2, + CohortName = "February 2026", + StartDate = new DateTime(2026, 2, 1), + EndDate = new DateTime(2026, 3, 29), + }; + _cohorts.Add(cohort2); - private static void SeedUserCC(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new UserCC - { - Id = 1, - CcId = 1, - UserId = 1 - }, - new UserCC - { - Id = 2, - CcId = 1, - UserId = 2, - }, - new UserCC - { - Id = 3, - CcId = 1, - UserId = 3 - } - ); - } + CohortCourse cc1 = new CohortCourse + { + Id = 1, + CohortId = 1, + CourseId = 1, + }; + _cohortCourses.Add(cc1); - private static void SeedUserExercises(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new UserExercise - { - Id = 1, - SubmissionLink = "subLink 1", - SubmitionTime = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), - Grade = 0, - UserId = 1, - Submitted = true, - ExerciseId = 1 - }, - new UserExercise - { - Id = 2, - SubmissionLink = "subLink 2", - SubmitionTime = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), - Grade = 3, - UserId = 2, - Submitted = true, - ExerciseId = 1 - }, - new UserExercise - { - Id = 3, - SubmissionLink = "subLink 3", - SubmitionTime = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), - Grade = 0, - UserId = 3, - Submitted = false, - ExerciseId = 1 - } + CohortCourse cc2 = new CohortCourse + { + Id = 2, + CohortId = 1, + CourseId = 2 + }; + _cohortCourses.Add(cc2); + CohortCourse cc3 = new CohortCourse + { + Id = 3, + CohortId = 2, + CourseId = 1 + }; + _cohortCourses.Add(cc3); - ); - } + UserCC ucc1 = new UserCC + { + Id = 1, + CcId = 1, + UserId = 1 + }; + _userCCs.Add(ucc1); + UserCC ucc2 = new UserCC + { + Id = 2, + CcId = 1, + UserId = 2, + }; + _userCCs.Add(ucc2); + UserCC ucc3 = new UserCC + { + Id = 3, + CcId = 1, + UserId = 3 + }; + _userCCs.Add(ucc3); - private static void SeedModules(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Module - { - Id = 1, - Title = "Module 1" - }, - new Module - { - Id = 2, - Title = "Module 2" - }, - new Module - { - Id = 3, - Title = "Module 3" - } - ); - } - private static void SeedCourseModules(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new CourseModule - { - Id = 1, - CourseId = 1, - ModuleId = 1 - }, - new CourseModule - { - Id = 2, - CourseId = 2, - ModuleId = 2 - }, - new CourseModule - { - Id = 3, - CourseId = 2, - ModuleId = 1 - } + for (int i = 4; i <= _users.Count; i++) + { + Random userCCRandom = new Random(); + var userId = i; + var ccId = _cohortCourses[userCCRandom.Next(_cohortCourses.Count)].Id; + UserCC ucc = new UserCC + { + Id = i, + UserId = userId, + CcId = ccId + }; + _userCCs.Add(ucc); + } - ); - } + Module module1 = new Module + { + Id = 1, + Title = "API" + }; + _modules.Add(module1); - private static void SeedUnits(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Unit - { - Id = 1, - ModuleId = 1, - Name = "Unit 1", - }, - new Unit - { - Id = 2, - ModuleId = 1, - Name = "Unit 2", - }, - new Unit - { - Id = 3, - ModuleId = 2, - Name = "Unit 3", - }, - new Unit - { - Id = 4, - ModuleId = 2, - Name = "Unit 4", - }, - new Unit - { - Id = 5, - ModuleId = 2, - Name = "Unit 5", - } - ); - } + Module module2 = new Module + { + Id = 2, + Title = "UI" + }; + _modules.Add(module2); - private static void SeedExercises(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Exercise - { - Id = 1, - UnitId = 1, - Name = "Exercise 1", - GitHubLink = "", - Description = "Exercise 1 description" - }, - new Exercise - { - Id = 2, - UnitId = 2, - Name = "Exercise 2", - GitHubLink = "", - Description = "Exercise 2 description" - }, - new Exercise - { - Id = 3, - UnitId = 3, - Name = "Exercise 3", - GitHubLink = "", - Description = "Exercise 3 description" - }, - new Exercise - { - Id = 4, - UnitId = 3, - GitHubLink = "", - Name = "Exercise 4", - Description = "Exercise 4 description" - }, - new Exercise - { - Id = 5, - UnitId = 4, - GitHubLink = "", - Name = "Exercise 5", - Description = "Exercise 5 description" - } - ); - } - private static void SeedNotes(ref ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasData( - new Note - { - Id = 1, - UserId = 1, - Title = "Name Note 1", - Content = "note1note1 note1 note1 content", - CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc) - }, - new Note - { - Id = 2, - UserId = 2, - Title = "Name Note 2", - Content = "note2 note2 note2 note2 content", - CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc) - }, - new Note - { - Id = 3, - UserId = 1, - Title = "Name Note 3", - Content = "note3 note3 note3 note3 content", - CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc) - }, - new Note - { - Id = 4, - UserId = 1, - Title = "Name Note 4", - Content = "note4 note4 note4 note4 content", - CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc) - }, - new Note - { - Id = 5, - UserId = 1, - Title = "Name Note 5", - Content = "note5 note5 note5 note5 content", - CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc) + Unit unit1 = new Unit + { + Id = 1, + ModuleId = 1, + Name = "Many2Many", + }; + _units.Add(unit1); + + Unit unit2 = new Unit + { + Id = 2, + ModuleId = 1, + Name = "TDD", + }; + _units.Add(unit2); + + Unit unit3 = new Unit + { + Id = 3, + ModuleId = 2, + Name = "Styling", + }; + _units.Add(unit3); + + Unit unit4 = new Unit + { + Id = 4, + ModuleId = 2, + Name = "responsive UX", + }; + _units.Add(unit4); + + Exercise exercise1 = new Exercise + { + Id = 1, + UnitId = 1, + Name = "pong_challenge", + GitHubLink = "github.com/1", + Description = "making pong game" + }; + _exercises.Add(exercise1); + + Exercise exercise2 = new Exercise + { + Id = 2, + UnitId = 2, + Name = "testing exercise 2", + GitHubLink = "github.com/2", + Description = "exercise for testing" + }; + _exercises.Add(exercise2); + + Exercise exercise3 = new Exercise + { + Id = 3, + UnitId = 3, + Name = "first_css", + GitHubLink = "github.com/3", + Description = "Styling html with css" + }; + _exercises.Add(exercise3); + + Exercise exercise4 = new Exercise + { + Id = 4, + UnitId = 3, + GitHubLink = "github.com/4", + Name = "css_javascript", + Description = "modifying css with javascript" + }; + _exercises.Add(exercise4); + + Exercise exercise5 = new Exercise + { + Id = 5, + UnitId = 4, + GitHubLink = "github.com/5", + Name = "button_press", + Description = "responsive UX with buttons" + }; + _exercises.Add(exercise5); + + UserExercise ux1 = new UserExercise + { + Id = 1, + SubmissionLink = "github.com/user1/1", + SubmitionTime = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + Grade = 0, + UserId = 1, + Submitted = true, + ExerciseId = 1 + }; + _userExercises.Add(ux1); + + UserExercise ux2 = new UserExercise + { + Id = 2, + SubmissionLink = "github.com/user2/1", + SubmitionTime = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + Grade = 3, + UserId = 2, + Submitted = true, + ExerciseId = 1 + }; + _userExercises.Add(ux2); + + UserExercise ux3 = new UserExercise + { + Id = 3, + SubmissionLink = "github.com/user3/1", + SubmitionTime = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + Grade = 0, + UserId = 3, + Submitted = true, + ExerciseId = 1 + }; + _userExercises.Add(ux3); + + + CourseModule cm1 = new CourseModule + { + Id = 1, + CourseId = 1, + ModuleId = 1 + }; + _courseModules.Add(cm1); + + CourseModule cm2 = new CourseModule + { + Id = 2, + CourseId = 2, + ModuleId = 2 + }; + _courseModules.Add(cm2); + + CourseModule cm3 = new CourseModule + { + Id = 3, + CourseId = 2, + ModuleId = 1 + }; + _courseModules.Add(cm3); + + Note note1 = new Note + { + Id = 1, + UserId = 1, + Title = "Late", + Content = "student was late", + CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + UpdatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + }; + _notes.Add(note1); + + Note note2 = new Note + { + Id = 2, + UserId = 3, + Title = "Late", + Content = "student was late", + CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + UpdatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + }; + _notes.Add(note2); + + Note note3 = new Note + { + Id = 3, + UserId = 1, + Title = "Late", + Content = "student was late", + CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + UpdatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + }; + _notes.Add(note3); + + Note note4 = new Note + { + Id = 4, + UserId = 1, + Title = "Late", + Content = "student was late", + CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + UpdatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + }; + _notes.Add(note4); + + Note note5 = new Note + { + Id = 5, + UserId = 1, + Title = "Late", + Content = "student was late", + CreatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + UpdatedAt = new DateTime(2025, 9, 5, 11, 2, 0, DateTimeKind.Utc), + }; + _notes.Add(note5); + + + + int noteId = 6; // Start after existing notes + for (int i = 0; i < _users.Count; i++) + { + var user = _users[i]; + if (user.Role == Role.Student) + { + Random noteRandom = new Random(); + Note n = new Note + { + Id = noteId++, + UserId = user.Id, + Title = $"Note for {user.FirstName}", + Content = _commentText[noteRandom.Next(_commentText.Count)], + CreatedAt = _seedTime, + UpdatedAt = _seedTime + }; + _notes.Add(n); } - ); + } + + + modelBuilder.Entity().HasData(_users); + modelBuilder.Entity().HasData(_posts); + modelBuilder.Entity().HasData(_comments); + modelBuilder.Entity().HasData(_likes); + modelBuilder.Entity().HasData(_courses); + modelBuilder.Entity().HasData(_cohorts); + modelBuilder.Entity().HasData(_modules); + modelBuilder.Entity().HasData(_units); + modelBuilder.Entity().HasData(_exercises); + modelBuilder.Entity().HasData(_notes); + modelBuilder.Entity().HasData(_courseModules); + modelBuilder.Entity().HasData(_userExercises); + modelBuilder.Entity().HasData(_cohortCourses); + modelBuilder.Entity().HasData(_userCCs); + + + } + + + + + + } \ No newline at end of file diff --git a/exercise.wwwapi/Endpoints/UserEndpoints.cs b/exercise.wwwapi/Endpoints/UserEndpoints.cs index 8009be1..70dc308 100644 --- a/exercise.wwwapi/Endpoints/UserEndpoints.cs +++ b/exercise.wwwapi/Endpoints/UserEndpoints.cs @@ -45,7 +45,7 @@ public static void ConfigureAuthApi(this WebApplication app) private static async Task GetUsers(IRepository userRepository, string? searchTerm, ClaimsPrincipal claimPrincipal) { - var results = await userRepository.GetWithIncludes(a => a.Include(u => u.Notes)); + var results = await userRepository.GetWithIncludes(x => x.Include(u => u.User_CC).ThenInclude(c => c.CohortCourse).ThenInclude(d => d.Cohort).Include(p => p.Notes)); if (!string.IsNullOrWhiteSpace(searchTerm)) { @@ -71,12 +71,14 @@ private static async Task GetUsers(IRepository userRepository, st var userRole = claimPrincipal.Role(); var authorizedAsTeacher = AuthorizeTeacher(claimPrincipal); + + + var userData = new UsersSuccessDTO - { + { Users = results.Select(user => authorizedAsTeacher - ? UserFactory.GetUserDTO(user, PrivilegeLevel.Teacher) //if teacher loads students, also load notes for students. - : UserFactory.GetUserDTO(user, PrivilegeLevel.Student)) - .ToList() + ? new UserDTO(user, PrivilegeLevel.Teacher) //if teacher loads students, also load notes for students. + : new UserDTO(user, PrivilegeLevel.Student)).ToList() //if teacher loads students, also load notes for students. }; var response = new ResponseDTO @@ -101,7 +103,7 @@ private static async Task GetUsersByCohort(IRepository reposito [ProducesResponseType(StatusCodes.Status200OK)] private static async Task GetUsersByCohortCourse(IRepository ccRepository, int cc_id, ClaimsPrincipal claimsPrincipal) { - var response = await ccRepository.GetByIdWithIncludes(a => a.Include(b => b.UserCCs).ThenInclude(a => a.User).ThenInclude(u => u.Notes), cc_id); + var response = await ccRepository.GetByIdWithIncludes(a => a.Include(z => z.Cohort).Include(b => b.UserCCs).ThenInclude(a => a.User).ThenInclude(u => u.Notes), cc_id); var results = response.UserCCs.Select(a => a.User).ToList(); var dto_results = results.Select(a => new UserDTO(a)); @@ -173,7 +175,6 @@ private static async Task Register(PostUserDTO request, IRepository Login(LoginRequestDTO request, IRepository GetUserById(IRepository userRepository, int id, ClaimsPrincipal claimsPrincipal) { - var response = await userRepository.GetByIdWithIncludes(x => x.Include(u => u.Notes), id); + var response = await userRepository.GetByIdWithIncludes(x => x.Include(u => u.User_CC).ThenInclude(c => c.CohortCourse).ThenInclude(d => d.Cohort).Include(p => p.Notes), id); if (response == null) { return TypedResults.NotFound(); } - var result = new UserDTO(response); - + var userData = new UserDTO(response); + // userData.CurrentStartdate = response.User_CC.ElementAt(0).CohortCourse.Cohort.StartDate; + var responseObject = new ResponseDTO + { + Status = "success", + Data = userData + }; + return TypedResults.Ok(responseObject); + } - - return TypedResults.Ok(response); - } [Authorize] [ProducesResponseType(StatusCodes.Status200OK)] @@ -325,7 +330,7 @@ public static async Task UpdateUser(IRepository userRepository, i userRepository.Update(user); await userRepository.SaveAsync(); - var result = await userRepository.GetByIdWithIncludes(x => x.Include(u => u.Notes), id); + var result = await userRepository.GetByIdWithIncludes(x => x.Include(u => u.User_CC).ThenInclude(c => c.CohortCourse).ThenInclude(d => d.Cohort).Include(p => p.Notes), id); var response = new ResponseDTO() { @@ -349,7 +354,7 @@ public static async Task DeleteUser(IRepository userRepository, i return Results.Unauthorized(); } - var user = await userRepository.GetByIdWithIncludes(null, id); + var user = await userRepository.GetByIdWithIncludes(x => x.Include(u => u.User_CC).ThenInclude(c => c.CohortCourse).ThenInclude(d => d.Cohort).Include(p => p.Notes), id); if (user == null) { return TypedResults.NotFound(); diff --git a/exercise.wwwapi/Factories/UserFactory.cs b/exercise.wwwapi/Factories/UserFactory.cs index da1f3b0..080e50b 100644 --- a/exercise.wwwapi/Factories/UserFactory.cs +++ b/exercise.wwwapi/Factories/UserFactory.cs @@ -12,7 +12,6 @@ public static UserDTO GetUserDTO(User user, PrivilegeLevel privilegeLevel) { var userDTO = new UserDTO() { - Id = user.Id, FirstName = user.FirstName, LastName = user.LastName, Bio = user.Bio, diff --git a/exercise.wwwapi/Program.cs b/exercise.wwwapi/Program.cs index 1ac5eb6..dbdf3da 100644 --- a/exercise.wwwapi/Program.cs +++ b/exercise.wwwapi/Program.cs @@ -217,6 +217,7 @@ app.ConfigureCommentEndpoints(); app.ConfigureExerciseEndpoints(); app.ConfigureCourseEndpoints(); +app.ConfigureLikeEndpoints(); app.Run(); static string GenerateDevJwtToken(string signingKey)