Beispiel #1
0
        public async Task <IActionResult> AddLecturecise(LectureciseCreateBindingModel lectureciseModel)
        {
            if (!this.ModelState.IsValid)
            {
                this.View(lectureciseModel);
            }

            var newLectureciseId = await this.lectureciseService.CreateAsync(lectureciseModel);

            if (lectureciseModel.EducatorIds != null || lectureciseModel.Day1 != null || lectureciseModel.Day2 != null)
            {
                var lecturecise = this.lectureciseService.GetByOriginal(newLectureciseId);

                if (lectureciseModel.EducatorIds != null)
                {
                    var course = this.courseService.GetByIdOriginal(lectureciseModel.CourseId);

                    foreach (var educatorId in lectureciseModel.EducatorIds)
                    {
                        lecturecise.LectureciseEducators.Add(new EducatorLecturecise()
                        {
                            EducatorId = educatorId, LectureciseId = lecturecise.Id
                        });

                        //Add the Educators to the course if not added previously.
                        if (course != null && !course.CourseEducators.Any(ce => ce.EducatorId == educatorId))
                        {
                            course.CourseEducators.Add(new EducatorCourse()
                            {
                                EducatorId   = educatorId,
                                CourseId     = course.Id,
                                TeachingRole = lectureciseModel.Type == LectureciseType.Lecture ? TeachingRole.Lecturer : TeachingRole.Assistant
                            });
                        }
                    }
                }

                if (lectureciseModel.Day1 != null && lectureciseModel.StartTime1 != null)
                {
                    var weekTime1 = new WeekTime()
                    {
                        DayOfWeek = lectureciseModel.Day1.Value, StartHour = lectureciseModel.StartTime1.Value.TimeOfDay.ToString()
                    };

                    if (lectureciseModel.EndTime1 != null)
                    {
                        weekTime1.EndHour = lectureciseModel.EndTime1.Value.TimeOfDay.ToString();
                    }

                    lecturecise.WeekTimes.Add(weekTime1);
                }

                if (lectureciseModel.Day2 != null && lectureciseModel.StartTime2 != null)
                {
                    var weekTime2 = new WeekTime()
                    {
                        DayOfWeek = lectureciseModel.Day2.Value, StartHour = lectureciseModel.StartTime2.Value.TimeOfDay.ToString()
                    };

                    if (lectureciseModel.EndTime2 != null)
                    {
                        weekTime2.EndHour = lectureciseModel.EndTime1.Value.TimeOfDay.ToString();
                    }

                    lecturecise.WeekTimes.Add(weekTime2);
                }

                await this.lectureciseService.SaveLecturecises();

                //await this.lectureciseService.EditLecturecise(lecturecise);
            }

            return(RedirectToAction("AddLecturecise", new { courseId = lectureciseModel.CourseId }));
        }
        public async Task <IActionResult> Edit(LectureciseEditModel lectureciseModel)
        {
            var lecturecise = this.lectureciseService.GetByOriginal(lectureciseModel.Id);

            if (lectureciseModel.Type != null && lectureciseModel.Type != lecturecise.Type)
            {
                lecturecise.Type = lectureciseModel.Type;
            }

            if (lectureciseModel.EducatorIdsForForm != null || lectureciseModel.Day1 != null || lectureciseModel.Day2 != null)
            {
                if (lectureciseModel.EducatorIdsForForm != null)
                {
                    var course = this.courseService.GetByIdOriginal(lectureciseModel.CourseId);

                    foreach (var educatorId in lectureciseModel.EducatorIdsForForm)
                    {
                        lecturecise.LectureciseEducators.Add(new EducatorLecturecise()
                        {
                            EducatorId = educatorId, LectureciseId = lecturecise.Id
                        });

                        //Add the Educators to the course if not added previously.
                        if (course != null && !course.CourseEducators.Any(ce => ce.EducatorId == educatorId))
                        {
                            course.CourseEducators.Add(new EducatorCourse()
                            {
                                EducatorId   = educatorId,
                                CourseId     = course.Id,
                                TeachingRole = lectureciseModel.Type == LectureciseType.Lecture ? TeachingRole.Lecturer : TeachingRole.Assistant
                            });
                        }
                    }
                }

                if (lectureciseModel.Day1 != null && lectureciseModel.StartTime1 != null)
                {
                    var weekTime1 = new WeekTime()
                    {
                        DayOfWeek = lectureciseModel.Day1.Value,
                        StartHour = lectureciseModel.StartTime1.Value.TimeOfDay.ToString()
                    };

                    if (lectureciseModel.EndTime1 != null)
                    {
                        weekTime1.EndHour = lectureciseModel.EndTime1.Value.TimeOfDay.ToString();
                    }

                    lecturecise.WeekTimes.Add(weekTime1);
                }

                if (lectureciseModel.Day2 != null && lectureciseModel.StartTime2 != null)
                {
                    var weekTime2 = new WeekTime()
                    {
                        DayOfWeek = lectureciseModel.Day2.Value,
                        StartHour = lectureciseModel.StartTime2.Value.TimeOfDay.ToString()
                    };

                    if (lectureciseModel.EndTime2 != null)
                    {
                        weekTime2.EndHour = lectureciseModel.EndTime1.Value.TimeOfDay.ToString();
                    }

                    lecturecise.WeekTimes.Add(weekTime2);
                }

                await this.lectureciseService.SaveLecturecises();

                await this.courseService.SaveCoursesDb();
            }

            return(RedirectToAction("AllLecturecises"));
        }