Untitled

mail@pastecode.io avatar
unknown
php
a year ago
7.4 kB
0
Indexable
Never
class PropertyRequestController extends Controller 
{
    /**
     * Get the common validation rules for all property types.
     *
     * @return array
     */
    private function getCommonValidationRules(): array 
    {
        return [
            "property_request_type_id" => 'required|numeric',
            "type" => 'required|string',
            "state_id" => 'required|numeric',
            "city_id" => 'required|numeric',
            "area_ids" => 'required',
            "sector_id" => 'required|numeric',
            "sector_detail_name" => 'required|string',
            "price_low" => 'required|numeric',
            "price_high" => 'required|numeric',
            "more_details" => 'nullable|string',
        ];
    }

    /**
     * Validate the request parameters for an apartment property request.
     *
     * @param  Request  $request
     * @return Illuminate\Validation\Validator
     */
    private function validateApartment(Request $request): Validator 
    {
        $validator = Validator::make($request->all(), array_merge($this->getCommonValidationRules(), [
            "bedrooms" => 'required|numeric',
            "bathrooms" => 'required|numeric'
        ]));
        return $validator;
    }

    /**
     * Validate the request parameters for a land property request.
     *
     * @param  Request  $request
     * @return Illuminate\Validation\Validator
     */
    private function validateLand(Request $request): Validator
    {
        $validator = Validator::make($request->all(), array_merge($this->getCommonValidationRules(), [
            "land_size" => 'required|numeric',
        ]));
        return $validator;
    }

    /**
     * Validate the request parameters for an office property request.
     *
     * @param  Request  $request
     * @return Illuminate\Validation\Validator
     */
    private function validateOffice(Request $request): Validator
    {
        $validator = Validator::make($request->all(), array_merge($this->getCommonValidationRules(), [
            "building_size" => 'required|numeric',
        ]));
        return $validator;
    }

    /**
     * Validate the request parameters for a room property request.
     *
     * @param  Request  $request
     * @return Illuminate\Validation\Validator
     */
    private function validateRoom(Request $request): Validator
    {
        $validator = Validator::make($request->all(), array_merge($this->getCommonValidationRules(), [
            "room_type_id" => 'required|numeric',
        ]));
        return $validator;
    }

    /**
     * Validate the request parameters for a shop property request.
     *
     * @param  Request  $request
     * @return Illuminate\Validation\Validator
     */
    private function validateShop(Request $request): Validator
    {
        $validator = Validator::make($request->all(), array_merge($this->getCommonValidationRules(), [
            "building_size" => 'required|numeric',
        ]));
        return $validator;
    }

    /**
     * Validate the request parameters for a store property request.
     *
     * @param  Request  $request
     * @return Illuminate\Validation\Validator
     */
    private function validateStore(Request $request): Validator
    {
        $validator = Validator::make($request->all(), array_merge($this->getCommonValidationRules(), [
            "building_size" => 'required|numeric',
        ]));
        return $validator;
    }

    /**
     * Validate the request parameters for a villa property request.
     *
     * @param  Request  $request
     * @return Illuminate\Validation\Validator
     */
    private function validateVilla(Request $request): Validator
    {
        $validator = Validator::make($request->all(), array_merge($this->getCommonValidationRules(), [
            "land_size" => 'required|numeric',
            "building_size" => 'required|numeric',
            "built_year_from" => 'required|date',
            "built_year_to" => 'required|date',
            "bedrooms" => 'required|numeric',
            "bathrooms" => 'required|numeric',
        ]));

        if($request->built_year_from AND $request->built_year_to) {
            $request->built_year_from = Carbon::parse($request->built_year_from);
            $request->built_year_to = Carbon::parse($request->built_year_to);
        }
        return $validator;
    }

	public function storePropertyRequest(Request $request) {
        $sector_detail_name = $request->sector_detail_name;
        switch ($sector_detail_name) {
            case "apartment":
                $validator = $this->validateApartment($request);
                $builder = new ApartmentPropertyRequestBuilder();
                break;
            case "room":
                $validator = $this->validateRoom($request);
                $builder = new RoomPropertyRequestBuilder();
                break;
            case "villa":
                $validator = $this->validateVilla($request);
                $builder = new VillaPropertyRequestBuilder();
                break;
            case "land":
                $validator = $this->validateLand($request);
                $builder = new LandPropertyRequestBuilder();
                break;
            case "office":
                $validator = $this->validateOffice($request);
                $builder = new OfficePropertyRequestBuilder();
                break;
            case "shop":
                $validator = $this->validateShop($request);
                $builder = new ShopPropertyRequestBuilder();
                break;
            case "store":
                $validator = $this->validateStore($request);
                $builder = new StorePropertyRequestBuilder();
                break;
            default:
                return response()->json(['errors' => "wrong sector detail name"], 422);
        }

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

        // Check if there are any extra parameters in the request
        $extraParams = array_diff_key($request->all(), $validator->validated());

        if (!empty($extraParams)) {
            
            return response()->json([
                'message' => 'Validation error',
                'errors' => [
                    'params' => 'The following parameters are not allowed: '.implode(', ', array_keys($extraParams)),
                ],
            ], 422);
        }

        // Determine the sector detail ID based on the sector ID and sector detail name
        $sector_detail_id = SectorDetail::where('sector_id', $request->sector_id)->where('slug', $sector_detail_name)->value('id');

        if(!$sector_detail_id)
            return response()->json(['errors' => "wrong sector detail name"], 422);

        $attributes = Collection::make($validator->attributes())->merge([
            'user_id' => Auth::id(),
            'sector_detail_id' => $sector_detail_id,
        ])->all();

        // Use the PropertyRequestDirector to build the property request using the specified builder and attributes
        $director = new PropertyRequestDirector($builder);
        $result = $director->buildPropertyRequest($attributes);

        return response()->json([
            'success' => true,
            'description' => "data created successfully",
            'content' => [
                'data' => $result,
            ]
        ], 200);
	 }
}