public async Task <UserCourseModel> UnSubscribeFromCourseAsync(UserCourseModel userCourseModel)
        {
            var userCourse = await _context.UserCourses.Where(uc => uc.StudentId == userCourseModel.StudentId &&
                                                              uc.CourseId == userCourseModel.CourseId)
                             .SingleOrDefaultAsync();

            if (userCourse != null)
            {
                var user = await _context.Users.Include(u => u.ScheduledJobs)
                           .Where(u => u.Id == userCourseModel.StudentId)
                           .SingleOrDefaultAsync();

                if (user != null)
                {
                    foreach (var item in user.ScheduledJobs)
                    {
                        _backgroundJob.Delete(item.Id);
                        _context.ScheduledJobs.Remove(item);
                    }
                }

                _context.UserCourses.Remove(userCourse);
                await _context.SaveChangesAsync();

                return(_mapper.Map <UserCourseModel>(userCourse));
            }
            return(null);
        }
        public async Task <CourseModel> SubscribeToCourseAsync(SubscribeToCourseModel subscribeToCourseModel)
        {
            var course = await _context.Courses.FindAsync(subscribeToCourseModel.CourseId);

            var user = await _context.Users.FindAsync(subscribeToCourseModel.StudentId);

            if (course != null && user != null)
            {
                var startingDate    = DateTimeOffset.FromUnixTimeMilliseconds(subscribeToCourseModel.EnrollmentDate).UtcDateTime;
                var userCourseModel = new UserCourseModel
                {
                    CourseId  = course.Id,
                    StudentId = user.Id,
                    StartDate = startingDate,
                    EndDate   = startingDate.AddDays((double)course.DurationDays)
                };

                var userCourse = _mapper.Map <UserCourse>(userCourseModel);

                _context.UserCourses.Add(userCourse);
                await _context.SaveChangesAsync();

                await SendScheduledEmailBackgroundAsync(userCourseModel.StartDate, user.Email, user.Id);

                return(_mapper.Map <CourseModel>(course));
            }

            return(null);
        }
        public async Task <UserCourseModel> Add(UserCourseModel model)
        {
            try
            {
                var entity = _mapper.Map <UserCourse>(model);
                await _repository.Insert(entity);

                return(model);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task <IActionResult> UnSubscribeFromCourse(UserCourseModel userCourseModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var course = await _courseService.UnSubscribeFromCourseAsync(userCourseModel);

            if (course != null)
            {
                return(Ok(course));
            }
            return(BadRequest(new { title = "Couldnt unsubscribe to course" }));
        }
Exemple #5
0
        public IActionResult Register(long id)
        {
            var course = _context.Courses.FirstOrDefault(x => x.Id == id);

            if (course.PaymentRequired && course.Price > 0.00m)
            {
                return(RedirectToAction("Checkout", "Transaction", new { id = course.Id }));
            }
            var entity = new UserCourseModel
            {
                UserId   = long.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value),
                CourseId = id
            };

            if (_context.UserCourses.Where(x => x.CourseId == id && x.UserId == entity.UserId) != null)
            {
                _context.Add(entity);
                _context.SaveChanges();
            }
            return(RedirectToAction(nameof(MyCourses)));
        }
        public async Task <UserCourseModel> Update(UserCourseModel model)
        {
            try
            {
                var entity = await _repository.Find(model.Id);

                if (entity == null)
                {
                    throw new Exception("UserCourse not found");
                }

                _mapper.Map(model, entity);
                await _repository.Update(entity);

                return(model);
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemple #7
0
        public IEnumerable <UserCourseModel> GetChildrenCourse(int id)
        {
            HashSet <int>          childrenIds = new HashSet <int>(_context.Families.Where(family => family.ParentId == id).Select(family => family.StudentId));
            List <User>            children    = _context.Users.Where(u => childrenIds.Contains(u.Id)).ToList();
            List <UserCourseModel> res         = new List <UserCourseModel>();

            foreach (User child in children)
            {
                HashSet <int> coursesChildTake = new HashSet <int>(_context.Enrollments.Where(enroll => enroll.StudentId == child.Id).Select(enroll => enroll.CourseId));
                var           currChildCourses = _context.Courses.Where(c => coursesChildTake.Contains(c.Id));

                UserCourseModel model = new UserCourseModel
                {
                    Id        = child.Id,
                    FirstName = child.FirstName,
                    LastName  = child.LastName,
                    Username  = child.Username,
                    Courses   = currChildCourses.ToList()
                };
                res.Add(model);
            }
            return(res);
        }