2
\$\begingroup\$

I use the Laravel API and want make messy code cleaner.

I have a form for registering business and businessOwner. And stylist who is working under this business registering from same function.

And which case best to use there? if statements or Switch case statements?

public function businessRegister (Request $request) { 

        if ($request['stylist'] === "stylist") {
            $validator = Validator::make($request->all(), [
                'firstName' => 'required|string',
                'lastName' => 'required|string',
                'gender' => 'required|in:Male,Female',
                'email' => 'required|string|email|max:255|unique:stylists',
                'password' => 'required|string|min:6|confirmed',
                'mobileNumber' => 'required|integer',
                'address' => 'required|string',
                'name' => 'required|string',
                'registrationNumber' => 'required|unique:businesses',
                'bMobileNumber' => 'required|integer',
                'bAddress' => 'required',

            ]);
        } elseif ($request['business'] === "business") {
            $validator = Validator::make($request->all(), [
                'firstName' => 'required|string',
                'lastName' => 'required|string',
                'gender' => 'required|in:Male,Female',
                'email' => 'required|string|email|max:255|unique:stylists',
                'password' => 'required|string|min:6|confirmed',
                'mobileNumber' => 'required|integer',
                'address' => 'required|string',
                'name' => 'required|string',
                'registrationNumber' => 'required|unique:businesses',
                'bMobileNumber' => 'required|integer',
                'bAddress' => 'required',

            ]);
        }

        if ($validator->fails()) {
            return response(['errors' => $validator->errors()->all()], 422);
        }

        if ($request['stylist'] === "stylist") {
            $findBusiness = Business::where([['name',$request->name], ['inviteCode',$request->inviteCode]])->first();
        } elseif ($request['business'] === "business") {
            $inviteCode = $this->inviteCodeGen();
            //print_r($inviteCode);
            $findBusiness = $this->business($request->name, $request->registrationNumber, $request->bImage, $request->bAddress, $request->bLongitude, $request->bLatitude, $request->bMobileNumber, 0, $inviteCode);
        }

        if (count($findBusiness) === 1 || $findBusiness['success'] === 1) {
            $request['device'] = "deviceToken|Or|DeviceTypeLike|IOS|OR|Android|IDK";
            $request['longitude'] = "getStylistLongitude";
            $request['latitude'] = "getStylistLatitude";

            if ($request['stylist'] === "stylist") {
                $id = $findBusiness->id;
            } elseif ($request['business'] === "business") {
                $id = $findBusiness['id'];
            }

            $stylist = Stylist::create([
                'firstName' => $request['firstName'],
                'lastName' => $request['lastName'],
                'image' => $request['image'],
                'stylistStatus' => "Stylist", // Freelancer - Stylist
                'businessId' => $id, // Last inserted Business ID 
                'gender' => $request['gender'],
                'email' => $request['email'],
                'password' => Hash::make($request['password']),
                'mobileNumber' => $request['mobileNumber'],
                'address' => $request['address'],
                'longitude' => $request['longitude'],
                'latitude' => $request['latitude'],
                'device' => $request['device'],
            ]);

            BookingTimes::insert(array(
                array('stylistId' => $stylist->id, 'day' => 'Monday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Tuesday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Wednesday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Thursday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Friday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Saturday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
                array('stylistId' => $stylist->id, 'day' => 'Sunday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            ));

            if ($request['business']) {
                Business::where('id', $findBusiness['id'])->update(['ownerId' => $stylist->id]);
            }

            $token = $stylist->createToken('Stylist registered')->accessToken;
            $response = ['token' => $token];

            return response($response, 200);
        }

        $response = ['error' => 'Registration error'];
        return response($response, 422);
    }

    private function inviteCodeGen($length = 3) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }

        $randomString2 = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString2 .= $characters[rand(0, $charactersLength - 1)];
        }

        return strtoupper($randomString . "-" . $randomString2);
    }

    protected function business ($name, $registrationNumber, $image, $address, $longitude, $latitude, $mobileNumber, $ownerId, $inviteCode) {
        $business = Business::create([
            'name' => $name,
            'inviteCode' => $inviteCode,
            'registrationNumber' => $registrationNumber,
            'image' => $image,
            'address' => $address,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'mobileNumber' => $mobileNumber,
        ]);

        BusinessBookingTimes::insert(array(
            array('businessId' => $business->id, 'day' => 'Monday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Tuesday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Wednesday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Thursday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Friday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Saturday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
            array('businessId' => $business->id, 'day' => 'Sunday', 'slotDuration' => 30, 'status' => 1, 'startTime' => '08:00:00', 'endTime' => '16:00:00'),
        ));

        return ['id' => $business->id, 'success' => 1];
    }
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You have repeated the following block of code twice:

'firstName' => 'required|string',
'lastName' => 'required|string',
'gender' => 'required|in:Male,Female',
...

What I would do would be to create a Custom Form Request, let's call that ValidateNewUserRequest. Then, your controller can take the argument ValidateNewUserRequest $request.

In ValidateNewUserRequest, you can apply the validation in the rules() function. I've also noticed that both pieces of your validation above contains the line: 'email' => 'required|string|email|max:255|unique:stylists', but I'm guessing that you want the stylists to be unique to the stylists, and the businesses unique to the businesses (potentially). If so, at the top of rules() you can have your if statement that determines which table to look at.

public function rules()
{

    if ($request->stylist == "stylist") {
        $table = 'stylists';

    } elseif ($request->business == "business") {
        $table = 'business';
    }

    // TODO :: you will want to determine which table to use if neither of the above

    return [
            'firstName' => 'required|string',
            'lastName' => 'required|string',
            'gender' => 'required|in:Male,Female',
            'email' => 'required|string|email|max:255|unique:stylists',
            'password' => 'required|string|min:6|confirmed',
            'mobileNumber' => 'required|integer',
            'address' => 'required|string',
            'name' => 'required|string',
            'registrationNumber' => 'required|unique:businesses',
            'bMobileNumber' => 'required|integer',
            'bAddress' => 'required',
    ];
} 

When you have this as your request object in your controller, it's already validated for (yay). There's lots of other stuff you can do in the custom request including error handling which you will be able to read about in the docs.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.