Esempio n. 1
0
        public async Task <ActionResult <bool> > deregisterUserFromClass(RegisterUserForClassRequest register)
        {
            if (await classRepository.getClassById(register.ClassId) == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "The class the user wants to signup for does not exist!"));
            }

            if (await userRepository.getUser(register.Username) == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "The user does not exist!"));
            }

            ClassRegister existingRegister = await registerRepository.getSpecificUserAndClass(register.Username, register.ClassId);

            if (existingRegister == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "User is not signed up for the class!"));
            }

            if (await registerRepository.removeRegister(existingRegister))
            {
                return(Ok(true));
            }
            else
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while attempting to remove the register from the database!"));
            }
        }
Esempio n. 2
0
        public async Task <ActionResult <bool> > deregisterUserFromClass(RegisterUserForClassRequest register)
        {
            if (await classRepository.getClassById(register.ClassId) == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "The class the user wants to signup for does not exist!"));
            }

            if (await userRepository.getUser(register.Username) == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "The user does not exist!"));
            }

            ClassRegister existingRegister = await registerRepository.getSpecificUserAndClass(register.Username, register.ClassId);

            if (existingRegister == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "User is not signed up for the class!"));
            }

            if (await registerRepository.removeRegister(existingRegister))
            {
                DayOfWeek  today = DateTime.Today.DayOfWeek;
                DateTime   date;
                GymClasses targetClass = await classRepository.getClassById(register.ClassId);

                if (today.ToString().ToLower() == targetClass.Day.ToLower())
                {
                    date = DateTime.Today.Date;
                }
                else
                {
                    DayOfWeek dayOfClass;

                    switch (targetClass.Day.ToLower())
                    {
                    case "monday":
                        dayOfClass = DayOfWeek.Monday;
                        break;

                    case "tuesday":
                        dayOfClass = DayOfWeek.Tuesday;
                        break;

                    case "wednesday":
                        dayOfClass = DayOfWeek.Wednesday;
                        break;

                    case "thursday":
                        dayOfClass = DayOfWeek.Thursday;
                        break;

                    case "friday":
                        dayOfClass = DayOfWeek.Friday;
                        break;

                    case "saturday":
                        dayOfClass = DayOfWeek.Saturday;
                        break;

                    default:
                        dayOfClass = DayOfWeek.Sunday;
                        break;
                    }

                    int difference = days[(int)today, (int)dayOfClass];

                    date = DateTime.Today.AddDays(difference).Date;
                }

                ClassAttendance classToChange = await classAttendanceRepository.getClassInstance(register.ClassId, date);

                if (classToChange == null)
                {
                    ClassAttendance newClass = new ClassAttendance();
                    newClass.Date             = date;
                    newClass.ClassId          = targetClass.ClassId;
                    newClass.Capacity         = targetClass.MaxCapacity;
                    newClass.Class            = targetClass;
                    newClass.NumberOfStudents = targetClass.CurrentStudents;

                    await classAttendanceRepository.addNewClassInstance(newClass);
                }
                else
                {
                    await classAttendanceRepository.editClassAttendance(classToChange.ClassId, classToChange.Date, -1);
                }


                return(Ok(true));
            }
            else
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while attempting to remove the register from the database!"));
            }
        }