Example #1
0
        public async Task <Enrollment> EnrollCourseAsync(Enrollment enrollment)
        {
            DbEnrollment dbEnrollment = _mapper.Map <DbEnrollment>(enrollment);

            return(_mapper.Map <Enrollment>(
                       await _enrollmentRepository.EnrollCourseAsync(dbEnrollment)
                       ));
        }
Example #2
0
        public DbEnrollment Subscribe(string userId, string courseId, DateTime studyDate)
        {
            var enrollment = new DbEnrollment
            {
                UserId    = userId,
                CourseId  = courseId,
                StudyDate = studyDate
            };

            context.Enrollments.Add(enrollment);
            context.SaveChanges();

            return(enrollment);
        }
        private async Task <bool> CreateNotificationEmailsAsync(DbEnrollment enrollment)
        {
            var user      = enrollment.User;
            var course    = enrollment.Course;
            var studyDate = enrollment.StudyDate;


            var notificationDays = new DateTime[] {
                studyDate.AddMonths(-1),
                studyDate.AddDays(-7),
                studyDate.AddDays(-1)
            };

            var body = await emailService.GetNotificationTemplateAsync(course.Name, studyDate.GetDaysToStudy());

            if (body == null)
            {
                return(false);
            }

            BackgroundJob.Enqueue(() => emailService.SendEmail(user.Email, "Notification", body));

            for (int i = 0; i < notificationDays.Length; i++)
            {
                if (notificationDays[i] > DateTime.UtcNow)
                {
                    var arrayDate = new int[] { 30, 7, 1 };
                    body = await emailService.GetNotificationTemplateAsync(course.Name, arrayDate[i]);

                    if (body == null)
                    {
                        return(false);
                    }

                    var jobId = BackgroundJob.Schedule(
                        () => emailService.SendEmail(user.Email, "Notification", body),
                        notificationDays[i]);

                    DbNotification notification = new DbNotification
                    {
                        EnrollmentId  = enrollment.Id,
                        HangfireJobId = jobId
                    };

                    context.Notifications.Add(notification);
                    context.SaveChanges();
                }
            }
            return(true);
        }
 public void DeleteNotifications(DbEnrollment enrollment)
 {
     if (enrollment != null)
     {
         var notifications = context.Notifications.Where(e => e.EnrollmentId == enrollment.Id);
         if (notifications.Count() != 0)
         {
             foreach (var notification in notifications)
             {
                 BackgroundJob.Delete(notification.HangfireJobId);
                 context.Notifications.Remove(notification);
             }
         }
         context.SaveChanges();
     }
 }
Example #5
0
 public void Unsubscribe(DbEnrollment enrollment)
 {
     context.Enrollments.Remove(enrollment);
     context.SaveChanges();
 }
 public bool CreateNotificationEmails(DbEnrollment enrollment)
 => CreateNotificationEmailsAsync(enrollment).Result;