Exemplo n.º 1
0
        public async Task <ActionResult <GymApplications[]> > AcceptApplications(SetApplicationState request)
        {
            GymApplications[] application = await applicationRepository.getApplication(request.GymName, request.BranchName);

            if (request.status == "Approve")
            {
                //GymApplications[] application = await applicationRepository.getApplication(request.GymName, request.BranchName);
                application[0].Status = ApplicationStatus.Approved;
                Gym newgym = new Gym();
                newgym.GymName   = request.GymName.Trim();
                newgym.GymBranch = request.BranchName.Trim();
                bool creategym = await gymRepository.addGym(newgym);

                bool updateStatus = await applicationRepository.updateApplication(application[0]);

                /* Generate Code For Sign Up */
                string generated = getRandomString(10);

                GymApplicationCodes code = await codeRepository.getByCode(generated);

                while (code != null)
                {
                    generated = getRandomString(10);
                    code      = await codeRepository.getByCode(generated);
                }

                code = new GymApplicationCodes();

                code.BranchName = newgym.GymBranch;
                code.GymName    = newgym.GymName;
                code.Code       = generated;

                if (!await codeRepository.add(code))
                {
                    /* Remove gym from database and do not change application status */
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to store generated code!"));
                }

                await mailer.sendEmail("*****@*****.**", "Gym Moves", "Gym Application Approval", $"Congratulations! Your gym approval has been approved! Use the following code {generated}\nAt https://gymmoveswebapi.azurewebsites.net/managerdetails", application[0].Email);
            }


            if (request.status == "Reject")
            {
                // GymApplications[] application = await applicationRepository.getApplication(request.GymName, request.BranchName);
                application[0].Status = ApplicationStatus.Rejected;
                bool updateStatus = await applicationRepository.updateApplication(application[0]);

                await mailer.sendEmail("*****@*****.**", "Gym Moves", "Gym Application Rejected", $"Unfortunately your gym application has been declined.", application[0].Email);
            }

            return(Ok(application));
        }
Exemplo n.º 2
0
        public async Task <ActionResult <UserResponseModel> > firstManager(FirstManagerModel user)
        {
            GymApplicationCodes code = await codeRepository.getByCode(user.Code);

            if (code == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "Invalid code!"));
            }

            Users existingUser = await userGymMovesRepository.getUser(user.Username);

            if (existingUser != null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "A user with that username already exists!"));
            }

            GymApplications[] applications = await applicationRepository.getApplication(code.GymName, code.BranchName);

            if (applications.Length == 0)
            {
                return(StatusCode(StatusCodes.Status404NotFound, "Application for gym not found!"));
            }

            GymApplications application = applications[0];
            Gym             gym         = await gymRepository.getGymByNameAndBranch(code.GymName, code.BranchName);

            existingUser = new Users();

            existingUser.GymIdForeignKey = gym.GymId;
            existingUser.Username        = user.Username;
            existingUser.MembershipId    = $"{code.GymName}{code.BranchName}defaultmanager";
            existingUser.Name            = application.Name;
            existingUser.Surname         = application.Surname;
            existingUser.PhoneNumber     = application.PhoneNumber;
            existingUser.Email           = application.Email;
            existingUser.UserType        = UserTypes.Manager;

            Random random = new Random();

            existingUser.Salt = getRandomString(random.Next(5, 10));
            string hash = getHash(SHA256.Create(), user.Password + existingUser.Salt);

            existingUser.Password = hash;

            if (await userGymMovesRepository.addUser(existingUser))
            {
                await applicationRepository.removeApplication(application);

                await codeRepository.remove(code);

                UserResponseModel response = new UserResponseModel();

                response.gymID       = existingUser.GymIdForeignKey;
                response.gymMemberID = existingUser.MembershipId;
                response.name        = existingUser.Name;
                response.userType    = existingUser.UserType;

                return(Ok(response));
            }
            else
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "An internal server error occured while adding the manager account!"));
            }
        }
Exemplo n.º 3
0
 public async Task <bool> remove(GymApplicationCodes application)
 {
     context.Remove(application);
     return((await context.SaveChangesAsync()) > 0);
 }