public async Task <bool> EnrollUserToLessonAsync(string userId, int lessonId) { await CheckIfUserAndLessonExistsAsync(userId, lessonId); if (this.IsAlreadyEnrolledInLesson(userId, lessonId)) { throw new InvalidOperationException(ExceptionConstants.AlreadyEnrolledInLessonErrorMessage); } int? seatsLeft = this.SeatsLeftInLesson(lessonId); bool canJoin = seatsLeft.HasValue == false || seatsLeft.Value > 0; if (canJoin == false) { return(false); } LessonUser lessonUser = new LessonUser() { UserId = userId, LessonId = lessonId, EnrollmentDate = DateTime.UtcNow, }; await this.dbContext.LessonsUsers.AddAsync(lessonUser); await this.dbContext.SaveChangesAsync(); return(true); }
public bool IsAlreadyEnrolledInLesson(string userId, int lessonId) { LessonUser enrollment = this.GetEnrollment(userId, lessonId); bool isAlreadyEnrolled = enrollment != null; return(isAlreadyEnrolled); }
private LessonUser GetEnrollment(string userId, int lessonId) { LessonUser enrollment = this.dbContext.LessonsUsers .Where(lu => lu.UserId == userId && lu.LessonId == lessonId) .FirstOrDefault(); return(enrollment); }
public async Task <bool> RemoveUserFromLessonAsync(string userId, int lessonId) { await CheckIfUserAndLessonExistsAsync(userId, lessonId); if (this.IsAlreadyEnrolledInLesson(userId, lessonId) == false) { return(false); } LessonUser enrollment = this.GetEnrollment(userId, lessonId); this.dbContext.LessonsUsers.Remove(enrollment); await this.dbContext.SaveChangesAsync(); return(true); }