Example #1
0
        static void Main(string[] args)
        {
            using (var studentSystem = new StudentSystemEntities())
            {
                Homework homework = new Homework()
                {
                    Content  = "No homework. It's summer!",
                    TimeSent = DateTime.Now
                };
                Student student = new Student()
                {
                    Name   = "Name Test",
                    Number = 15
                };

                List <Student> students = new List <Student>();
                students.Add(student);
                List <Homework> homeworks = new List <Homework>();
                homeworks.Add(homework);

                Course course = new Course()
                {
                    Name        = "FirstTest",
                    Description = "Just description!",
                    Materials   = "No materials",
                    Students    = students,
                    Homework    = homeworks
                };

                studentSystem.Courses.Add(course);
                studentSystem.SaveChanges();
            }
        }
Example #2
0
        public static void AddHomeworks(int homeworksCount, StudentSystemEntities dbContext)
        {
            var coursesIds = dbContext.Courses
                             .Select(c => c.Id)
                             .ToList();
            int coursesIdsCount = coursesIds.Count;

            var studentsIds = dbContext.Students
                              .Select(s => s.Id)
                              .ToList();

            int studentIdsCount = studentsIds.Count;

            for (int i = 0; i < homeworksCount; i++)
            {
                var hw = new Homework();
                hw.Content  = RandomGenerator.GenerateString(1, 2000);
                hw.TimeSent = RandomGenerator.GenarateDateInPast();

                int courseIndex = RandomGenerator.GenerateIntenger(0, coursesIdsCount - 1);
                hw.CourseId = coursesIds[courseIndex];

                int studentIndex = RandomGenerator.GenerateIntenger(0, studentIdsCount - 1);
                hw.StudentId = studentsIds[studentIndex];

                dbContext.Homeworks.Add(hw);
            }

            dbContext.SaveChanges();
        }
Example #3
0
        public static void AddCourses(int coursesCount, StudentSystemEntities dbContext)
        {
            for (int i = 0; i < coursesCount; i++)
            {
                var course = new Course();
                course.Name = RandomGenerator.GenerateString(2, 150);
                dbContext.Courses.Add(course);
            }

            dbContext.SaveChanges();
        }
Example #4
0
        private static void Main()
        {
            using (var context = new StudentSystemEntities())
                using (var tran = context.Database.BeginTransaction())
                {
                    try
                    {
                        var pesho   = AddStudent(context, "Pesho");
                        var goshoId = AddStudent(context, "Gosho").StudentId;
                        AddStudent(context, "Mariika");

                        var cSharp = AddCourse(context, "C#", "Fundamentals of C# programming language");
                        AddCourse(context, "JavaScript", "Fundamental of JavaScript programming language");
                        var gosho = FindStudentById(context, goshoId);
                        AddCourse(context, "JavaScript SPA", "Single page applications with JavaScript programming language", "Google", new HashSet <Student>()
                        {
                            pesho, gosho
                        });

                        var js = FindCourseByName(context, "JavaScript");
                        Console.WriteLine("Gosho =? " + gosho.Name);

                        AddHomework(context, "xxxxxxxxxx", DateTime.Now, cSharp, pesho);
                        AddHomework(context, "Some homework", new DateTime(2014, 05, 05), js, gosho);

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                    }

                    List <Student> students = context.Students.ToList();
                    foreach (var student in students)
                    {
                        Console.WriteLine(student);
                    }

                    foreach (var course in context.Courses)
                    {
                        Console.WriteLine(course);
                    }

                    foreach (var homework in context.Homeworks)
                    {
                        Console.WriteLine(homework);
                    }
                }
        }
Example #5
0
        private static void AddHomework(StudentSystemEntities context, string content, DateTime sentDate, Course course, Student student)
        {
            var newHomework = new Homework()
            {
                Content  = content,
                TimeSent = sentDate,
                Course   = course,
                Student  = student
            };

            course.Homeworks.Add(newHomework);
            student.Homeworks.Add(newHomework);
            context.Homeworks.Add(newHomework);
            context.SaveChanges();
        }
Example #6
0
        private static Student AddStudent(StudentSystemEntities context, string name, ICollection <Course> courses = null)
        {
            var newStudent = new Student()
            {
                Name = name,
            };

            if (courses != null)
            {
                newStudent.Courses = courses;
            }

            context.Students.Add(newStudent);
            context.SaveChanges();

            return(newStudent);
        }
Example #7
0
        public static void AddStudents(int studentsCount, StudentSystemEntities dbContext)
        {
            for (int i = 0; i < studentsCount; i++)
            {
                var student = new Student();
                student.FirstName     = RandomGenerator.GenerateString(1, 50);
                student.LastName      = RandomGenerator.GenerateString(1, 50);
                student.FacultyNumber = RandomGenerator.GenerateNumberAsString(10, 10);
                var courses     = dbContext.Courses.ToList();
                int courseIndex = RandomGenerator.GenerateIntenger(0, courses.Count - 1);
                var course      = courses[courseIndex];
                student.Courses.Add(course);

                dbContext.Students.Add(student);
            }

            dbContext.SaveChanges();
        }
Example #8
0
        public static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion <StudentSystemEntities, Configuration>());

            var dbContext = new StudentSystemEntities();

            using (dbContext)
            {
                Importer.AddCourses(50, dbContext);
                Importer.AddStudents(150, dbContext);
                Importer.AddHomeworks(450, dbContext);

                var studentsInLiWuCurs = Searcher.GetAllStudenstByCourseName("LiWU", dbContext);

                foreach (var student in studentsInLiWuCurs)
                {
                    System.Console.WriteLine($"{student.FacultyNumber} --> {student.FirstName} {student.LastName}");
                }
            }
        }
Example #9
0
        private static Course AddCourse(
            StudentSystemEntities context,
            string name,
            string description             = null,
            string materials               = null,
            ICollection <Student> students = null)
        {
            var newCourse = new Course()
            {
                Name        = name,
                Description = description,
                Materials   = materials
            };

            if (students != null)
            {
                newCourse.Students = students;
            }

            context.Courses.Add(newCourse);
            context.SaveChanges();

            return(newCourse);
        }
Example #10
0
        private static Course FindCourseByName(StudentSystemEntities context, string name)
        {
            var foundStudent = context.Courses.FirstOrDefault(c => c.Name == name);

            return(foundStudent);
        }
Example #11
0
 public HomeworkRepository(StudentSystemEntities db)
 {
     this.db = db;
 }
 public HomeworkRepository(StudentSystemEntities db)
 {
     this.db = db;
 }
 public HomeworkRepository()
 {
     this.db = new StudentSystemEntities();
 }
 public StudentRepository(StudentSystemEntities db)
 {
     this.db = db;
 }
 public StudentRepository()
 {
     this.db = new StudentSystemEntities();
 }
 public CourseRepository()
 {
     this.db = new StudentSystemEntities();
 }
Example #17
0
 public HomeworkRepository()
 {
     this.db = new StudentSystemEntities();
 }
 protected BaseManipulator()
 {
     database = new StudentSystemEntities();
 }
Example #19
0
        private static Course FindCourseById(StudentSystemEntities context, int courseId)
        {
            var foundStudent = context.Courses.FirstOrDefault(c => c.CourseId == courseId);

            return(foundStudent);
        }
Example #20
0
        private static Student FindStudentByNumber(StudentSystemEntities context, Guid number)
        {
            var foundStudent = context.Students.FirstOrDefault(s => s.Number == number);

            return(foundStudent);
        }
Example #21
0
        private static Student FindStudentById(StudentSystemEntities context, int studentId)
        {
            var foundStudent = context.Students.FirstOrDefault(s => s.StudentId == studentId);

            return(foundStudent);
        }
 public CourseRepository(StudentSystemEntities db)
 {
     this.db = db;
 }
Example #23
0
        public static ICollection <Student> GetAllStudenstByCourseName(string courseName, StudentSystemEntities dbContext)
        {
            if (string.IsNullOrEmpty(courseName))
            {
                throw new ArgumentNullException("The course name cant not be null or enpty string");
            }

            Course course = dbContext.Courses.Where(c => c.Name == courseName).FirstOrDefault();

            if (course == null)
            {
                throw new ArgumentException(nameof(courseName), $"This {courseName} dont exist");
            }

            var students = course.Students;

            return(students);
        }
        static void Main()
        {
            var context = new StudentSystemEntities();


            // 01. List all students with their homeworks
            var studentsAndHomeworks = context.Students
                                       .Select(s => new
            {
                Name      = s.Name,
                Homeworks = s.Homeworks.Select(h => new
                {
                    Content     = h.Content,
                    ContentType = h.ContentType
                })
            }).ToList();

            foreach (var student in studentsAndHomeworks)
            {
                Console.WriteLine(student.Name);

                foreach (var homework in student.Homeworks)
                {
                    Console.WriteLine("----Content: {0}, Type: {1}", homework.Content, homework.ContentType);
                }
            }



            // 02.List all courses with resources
            // Note that I had no time to fill much test data so I'll just create the query here and fill test data later

            var coursesAndResources = context.Courses
                                      .OrderBy(c => c.StartDate)
                                      .ThenByDescending(c => c.EndDate)
                                      .Select(c => new
            {
                Name        = c.Name,
                Description = c.Description,
                Resources   = c.Resources
            }).ToList();

            // 03. All courses with more than 5 resources

            var coursesWith5Resources = context.Courses
                                        .Where(c => c.Resources.Count > 5)
                                        .OrderByDescending(c => c.Resources.Count)
                                        .ThenByDescending(c => c.StartDate)
                                        .Select(c => new
            {
                Name          = c.Name,
                ResourceCount = c.Resources.Count
            }).ToList();

            // 04. Courses active on a given date

            var today = DateTime.Today;

            var coursesActiveToday = context.Courses
                                     .Where(c => c.StartDate >= today && c.EndDate <= today)
                                     .Select(c => new
            {
                Name          = c.Name,
                StartDate     = c.StartDate,
                EndDate       = c.EndDate,
                StudentsCount = c.Students.Count,
                Duration      = SqlFunctions.DateDiff("day", c.EndDate, c.StartDate)
            })
                                     .OrderByDescending(c => c.StudentsCount)
                                     .ThenByDescending(c => c.Duration)
                                     .ToList();

            // 05. Calculate number of courses for each student

            var studentsCourses = context.Students
                                  .Select(s => new
            {
                Name               = s.Name,
                CoursesCount       = s.Courses.Count,
                TotalPrice         = s.Courses.Select(c => c.Price).Sum(),
                AverageCoursePrice = s.Courses.Select(c => c.Price).Average()
            })
                                  .OrderByDescending(s => s.TotalPrice)
                                  .ThenByDescending(s => s.CoursesCount)
                                  .ThenBy(s => s.Name)
                                  .ToList();
        }