public int EnrollStudentInCourse(int studentId, int courseId, Enrollment enrollment)
        {
            try
            {
                if (_ctx.Enrollments.Any(e => e.Course.Id == courseId && e.Student.Id == studentId))
                {
                    return 2;
                }

                _ctx.Database.ExecuteSqlCommand("INSERT INTO Enrollments VALUES (@p0, @p1, @p2)",
                    enrollment.EnrollmentDate, courseId.ToString(), studentId.ToString());

                return 1;
            }
            catch ( DbEntityValidationException dbex)
            {
                foreach (var eve in dbex.EntityValidationErrors)
                {
                    string line = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);

                    foreach (var ve in eve.ValidationErrors)
                    {
                        line = string.Format("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);

                    }
                }
                return 0;

            }
            catch (Exception ex)
            {
                return 0;
            }
        }
Esempio n. 2
0
        public void Seed()
        {
            if (_ctx.Courses.Count() > 0)
            {
                return;
            }

            try
            {
                foreach (var subjectName in subjects)
                {
                    var subject = new Subject
                    {
                        Name = subjectName
                    };
                    _ctx.Subjects.Add(subject);
                    _ctx.SaveChanges();
                }

                for (int i = 0; i < tutorNames.Length; i++)
                {
                    var nameGenderMail = SplitValue(tutorNames[i]);
                    var tutor = new Tutor
                    {
                        Email = String.Format("{0}.{1}@{2}", nameGenderMail[0], nameGenderMail[1], nameGenderMail[3]),
                        UserName = String.Format("{0}{1}", nameGenderMail[0], nameGenderMail[1]),
                        Password = RandomString(8),
                        FirstName = nameGenderMail[0],
                        LastName = nameGenderMail[1],
                        Gender = ((Gender)Enum.Parse(typeof(Gender), nameGenderMail[2]))
                    };

                    _ctx.Tutors.Add(tutor);

                    var courseSubject = _ctx.Subjects.Where(s => s.Id == i + 1).Single();

                    foreach (var CourseDataItem in CoursesSeedData.Where(c => c.SubjectID == courseSubject.Id))
                    {
                        var course = new Course
                        {
                            Name = CourseDataItem.CourseName,
                            CourseSubject = courseSubject,
                            CourseTutor = tutor,
                            Duration = new Random().Next(3, 6),
                            Description = String.Format("The course will talk in depth about: {0}", CourseDataItem.CourseName)
                        };
                        _ctx.Courses.Add(course);
                    }
                }

                _ctx.SaveChanges();

                for (int i = 0; i < studentNames.Length; i++)
                {
                    var nameGenderMail = SplitValue(studentNames[i]);
                    var student = new Student()
                    {
                        Email = String.Format("{0}.{1}@{2}", nameGenderMail[0], nameGenderMail[1], nameGenderMail[3]),
                        UserName = String.Format("{0}{1}", nameGenderMail[0], nameGenderMail[1]),
                        Password = RandomString(8),
                        FirstName = nameGenderMail[0],
                        LastName = nameGenderMail[1],
                        Gender = ((Gender)Enum.Parse(typeof(Gender), nameGenderMail[2])),
                        DateOfBirth = DateTime.UtcNow.AddDays(-new Random().Next(7000, 8000)),
                        RegistrationDate = DateTime.UtcNow.AddDays(-new Random().Next(365, 730))
                    };

                    _ctx.Students.Add(student);

                    int maxCoursesId = _ctx.Courses.Max(c => c.Id);

                    //To enroll in 4 courses randomly
                    for (int z = 0; z < 4; z++)
                    {

                        int randomCourseId = new Random().Next(1, maxCoursesId);

                        var enrollment = new Enrollment
                        {
                            Student = student,
                            Course = _ctx.Courses.Where(c => c.Id == randomCourseId).Single(),
                            EnrollmentDate = DateTime.UtcNow.AddDays(-new Random().Next(10, 30))
                        };
                        _ctx.Enrollments.Add(enrollment);
                    }
                }

                _ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                string message = ex.ToString();
                throw ex;
            }

        }
        // POST: odata/Enrollments
        public async Task<IHttpActionResult> Post(Enrollment enrollment)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Enrollments.Add(enrollment);
            await db.SaveChangesAsync();

            return Created(enrollment);
        }