예제 #1
0
        private void RemoveCoursePaymentType(string courseId, string pTypeId)
        {
            CoursePaymentType coursePT = _context.CoursePaymentTypes
                                         .FirstOrDefault(cpt => cpt.CourseId == courseId && cpt.PaymentTypeId == pTypeId);

            if (coursePT != null)
            {
                _context.CoursePaymentTypes.Remove(coursePT);
                _context.SaveChanges();
            }
        }
예제 #2
0
        public async Task <IdentityResult> UpdateCourseAsync(CourseViewModel course)
        {
            Course dbCourse = _context.Courses
                              .Include(cour => cour.CourseAppUsers)
                              .Include(cour => cour.CoursePaymentTypes)
                              .FirstOrDefault(cour => cour.Id == course.Id);

            if (dbCourse == null)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = "Курс не знайдено"
                }));
            }
            IList <string>       coursTeachersId = dbCourse.CourseAppUsers.Select(usr => usr.AppUserId).ToList();
            IEnumerable <string> addedTeachers   = course.TeachersId.Except(coursTeachersId);
            IEnumerable <string> removedTeachers = coursTeachersId.Except(course.TeachersId);
            IList <string>       coursPtypes     = dbCourse.CoursePaymentTypes.Select(cpt => cpt.PaymentTypeId).ToList();
            IEnumerable <string> addedPTypes     = course.PTIds.Except(coursPtypes);
            IEnumerable <string> removedPTypes   = coursPtypes.Except(course.PTIds);

            dbCourse.Name              = course.Name;
            dbCourse.Description       = course.Description;
            dbCourse.IsActive          = course.IsActive;
            dbCourse.Price             = course.Price;
            dbCourse.AllowOneTimePrice = course.AllowOneTimePrice;
            dbCourse.OneTimePrice      = course.OneTimePrice;
            var state = _context.Courses.Update(dbCourse);

            if (state.State != EntityState.Modified)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = "Курс не оновлено"
                }));
            }
            await _context.SaveChangesAsync();

            foreach (string teacher in addedTeachers)
            {
                await _teacherService.AddTeacherToCourse(teacher, dbCourse.Id);
            }
            foreach (string teacher in removedTeachers)
            {
                await _teacherService.RemoveTeacherFromCourse(teacher, dbCourse.Id);
            }
            foreach (string pType in addedPTypes)
            {
                _context.CoursePaymentTypes.Add(new CoursePaymentType {
                    CourseId = course.Id, PaymentTypeId = pType
                });
            }
            foreach (string pType in removedPTypes)
            {
                CoursePaymentType removedCPT = _context.CoursePaymentTypes
                                               .FirstOrDefault(cpt => cpt.PaymentTypeId == pType && cpt.CourseId == course.Id);
                if (removedCPT != null)
                {
                    _context.CoursePaymentTypes.Remove(removedCPT);
                }
            }
            await _context.SaveChangesAsync();

            return(IdentityResult.Success);
        }