Пример #1
0
        public async Task <ActionResult <bool> > signUpUserToClass(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)
            {
                GymClasses targetClass = await classRepository.getClassById(register.ClassId);

                if (targetClass.CurrentStudents < targetClass.MaxCapacity)
                {
                    if (await registerRepository.addRegister(RegisterMapper.registerUserForClassToClassRegister(register)))
                    {
                        return(Ok(true));
                    }
                    else
                    {
                        return(StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while trying to register the user for the class!"));
                    }
                }
                else
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, "Class max capacity reached!"));
                }
            }
            else
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "User is already signed up for the specified class"));
            }
        }
Пример #2
0
        public async Task <ActionResult <bool> > signUpUserToClass(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)
            {
                GymClasses targetClass = await classRepository.getClassById(register.ClassId);

                if (targetClass.CurrentStudents < targetClass.MaxCapacity)
                {
                    if (await registerRepository.addRegister(RegisterMapper.registerUserForClassToClassRegister(register)))
                    {
                        DayOfWeek today = DateTime.Today.DayOfWeek;

                        DateTime date;

                        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 trying to register the user for the class!"));
                    }
                }
                else
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, "Class max capacity reached!"));
                }
            }
            else
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "User is already signed up for the specified class"));
            }
        }