Пример #1
0
        // POST: api/SchedulesAPI
        public HttpResponseMessage Post([FromBody] CreateEditScheduleVM model)
        {
            HttpResponseMessage response = ValidateModel(model);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                ApplicationDbContext db = new ApplicationDbContext();
                db.Schedules.Add(CopySchedule(model, db));
                db.SaveChanges();
            }

            return(response);
        }
Пример #2
0
        // PUT: api/SchedulesAPI/5
        public HttpResponseMessage Put([FromBody] CreateEditScheduleVM model)
        {
            HttpResponseMessage response = ValidateModel(model);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                ApplicationDbContext db = new ApplicationDbContext();
                db.Entry(CopySchedule(model, db, model.ID)).State = EntityState.Modified;
                db.SaveChanges();
            }

            return(response);
        }
Пример #3
0
        private Schedule CopySchedule(CreateEditScheduleVM model, ApplicationDbContext db, int?scheduleId = null)
        {
            Schedule schedule = null;

            if (scheduleId == null)
            {
                schedule = new Schedule {
                    Students = new List <User>()
                }
            }
            ;
            else
            {
                schedule = db.Schedules.FirstOrDefault(s => s.ID == scheduleId);

                // Delation of all students who "disappeared" from the previous schedule
                foreach (User student in schedule.Students.Where(s => !model.Students.Contains(s.Id)))
                {
                    student.Schedules.Remove(schedule);
                }
            }

            schedule.WeekDay       = model.WeekDay;
            schedule.BeginningTime = model.BeginningTime.ToString(ScheduleConstants.TIME_FORMAT);
            schedule.EndingTime    = model.EndingTime.ToString(ScheduleConstants.TIME_FORMAT);
            schedule.Course        = db.Courses.FirstOrDefault(c => c.ID == model.CourseID);
            schedule.Classroom     = db.Classrooms.FirstOrDefault(c => c.ID == model.ClassroomID);

            // Filter on the students to be added to the new version of the schedule
            List <string> studentsId = schedule.Students.Select(s => s.Id).ToList();

            model.Students = model.Students.Where(s => s != null && !studentsId.Contains(s)).ToArray();

            foreach (string studentId in model.Students)
            {
                schedule.Students.Add(db.LMSUsers.FirstOrDefault(u => u.Id == studentId));
            }

            return(schedule);
        }
Пример #4
0
        private HttpResponseMessage ValidateModel(CreateEditScheduleVM model)
        {
            /// Some basic validity checks ///
            // - Is the beginning time lower than the ending time ? - //
            if (model.BeginningTime >= model.EndingTime)
            {
                HttpError err = new HttpError("The ending time must be greater than the beginning time.");
                return(Request.CreateResponse(HttpStatusCode.NotAcceptable, err));
            }

            // - At least one student must take part to the lesson - //
            if (model.Students.Length == 0)
            {
                HttpError err = new HttpError("You must select at least one student for that lesson.");
                return(Request.CreateResponse(HttpStatusCode.NotAcceptable, err));
            }

            // - Are all elements to create the schedule actually available at the given date and times? - //
            List <string> availability = TestAvailability(model);

            if (availability.Count > 0)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable,
                                                   new HttpError(string.Join(@"\\", availability))));
            }

            // - Is the classroom big enough for all the students? - //
            Classroom classroom  = new ClassroomsRepository().Classroom(model.ClassroomID);
            int       nbStudents = model.Students.Where(s => s != null).Count();

            if (classroom.AmountStudentsMax < nbStudents)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable,
                                                   new HttpError("The selected classroom is too small for the amount of users.\\" +
                                                                 classroom.AmountStudentsMax.ToString() + " places for " + nbStudents.ToString() + " students.")));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Пример #5
0
        private List <string> TestAvailability(CreateEditScheduleVM schedule)
        {
            // Let's check if all data are available at the given day and times, starting with the teacher
            Course course = new CoursesRepository().Course(schedule.CourseID);

            if (course == null)
            {
                return(new List <string> {
                    "The selected course is invalid."
                });
            }

            Schedule availability = repository.TeacherAvailability(course.TeacherID,
                                                                   schedule.WeekDay,
                                                                   schedule.BeginningTime.ToString(ScheduleConstants.TIME_FORMAT),
                                                                   schedule.EndingTime.ToString(ScheduleConstants.TIME_FORMAT));

            if (availability != null && availability.ID != schedule.ID)
            {
                return(new List <string> {
                    course.Teacher.ToString() + " is not available at the given day and times:",
                    availability.ToString()
                });
            }

            // Then, we can check if the room is available
            Classroom classroom = new ClassroomsRepository().Classroom(schedule.ClassroomID);

            if (classroom == null)
            {
                return(new List <string> {
                    "The selected classroom is invalid."
                });
            }

            availability = repository.ClassroomAvailability(schedule.ClassroomID,
                                                            schedule.WeekDay,
                                                            schedule.BeginningTime.ToString(ScheduleConstants.TIME_FORMAT),
                                                            schedule.EndingTime.ToString(ScheduleConstants.TIME_FORMAT));

            if (availability != null && availability.ID != schedule.ID)
            {
                return(new List <string> {
                    "The classroom " + classroom.Name + " is not available at the given day and times:",
                    availability.ToString()
                });
            }

            // Finally, let's check if all the students are available for the lesson
            foreach (string studentId in schedule.Students)
            {
                availability = repository.StudentAvailability(studentId,
                                                              schedule.WeekDay,
                                                              schedule.BeginningTime.ToString(ScheduleConstants.TIME_FORMAT),
                                                              schedule.EndingTime.ToString(ScheduleConstants.TIME_FORMAT));

                if (availability != null && availability.ID != schedule.ID)
                {
                    return(new List <string> {
                        new UsersRepository().UserById(studentId).ToString() + " is not available at the given day and times:",
                        availability.ToString()
                    });
                }
            }

            return(new List <string>());
        }