Beispiel #1
0
        public UserModel RegisterOrUpdate(string name, int age)
        {
            var student = _context.Users.Where(w => w.Name == name).FirstOrDefault();

            if (student == null)
            {
                student      = new UserModel();
                student.Name = name;
                student.Age  = age;

                _context.Users.Add(student);
                _context.SaveChanges();
            }
            else if (student.Age != age)
            {
                student.Age = age;

                _context.Users.Attach(student);
                _context.Entry(student).State = EntityState.Modified;
                _context.SaveChanges();
            }
            return(student);
        }
Beispiel #2
0
        public void SignUp(int courseID, int studentID)
        {
            //get course
            var savedCourse = this.GetByID(courseID);

            if (savedCourse == null)
            {
                throw new CourseNotFoundException("The course does not exists");
            }

            if (savedCourse.CourseStudent.Count == savedCourse.MaxSeats)
            {
                throw new CourseFullException("The selected course is full. Please, select another one.");
            }

            //check if student isn't already in this course
            if (savedCourse.CourseStudent.Any(i => i.StudentId == studentID))
            {
                throw new StudentAlreadyOnCourseException("The selected student is already on this course. Please, select another one.");
            }

            //get student
            var savedStudent = this.GetByID(studentID);

            if (savedStudent == null)
            {
                throw new StudentNotFoundException("The selected student does not exists");
            }

            var newStudent = new CourseStudent();

            newStudent.CourseId  = courseID;
            newStudent.StudentId = studentID;

            //sign up student to a course
            savedCourse.CourseStudent.Add(newStudent);

            //save changes
            _chamaContext.SaveChanges();
        }
 public void Register(CourseModel model)
 {
     _context.Courses.Add(model);
     _context.SaveChanges();
 }
Beispiel #4
0
 public bool Commit()
 {
     return(_context.SaveChanges() > 0);
 }
Beispiel #5
0
 public int SaveChanges()
 {
     return(Db.SaveChanges());
 }