예제 #1
0
        public async Task <DbCourse> CreateCourseAsync(DbCourse course)
        {
            if (course == null || course.Id != 0)
            {
                return(null);
            }

            _context.Courses.Add(course);
            await _context.SaveChangesAsync();

            return(course);
        }
예제 #2
0
        public async Task <DbEnrollment> EnrollCourseAsync(DbEnrollment enrollment)
        {
            DbEnrollment existingEnrollment = _context.Enrollments
                                              .Where(e => e.CourseId == enrollment.CourseId && e.UserId == enrollment.UserId)
                                              .SingleOrDefault();

            if (existingEnrollment != null)
            {
                return(null);
            }

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

            DbEnrollment newEnrollment = await _context.Enrollments
                                         .Where(e => e.CourseId == enrollment.CourseId && e.UserId == enrollment.UserId)
                                         .SingleOrDefaultAsync();

            return(newEnrollment);
        }