Skip to content

Add endpoint for getting addresses #32

@PhantomWatson

Description

@PhantomWatson

Add a public endpoint such as GET /address that takes a locationName input and outputs the most recent address associated with that location. In the future, this could be upgraded to use the Google Maps API to determine the canonical address for a location.

// Controller code
    /**
     * Returns the address most recently associated with the provided location name
     *
     * @return void
     */
    public function getAddress()
    {
        $location = (string)$this->request->getData('location');
        $address = $location ? $this->Events->getAddress($location) : false;
        $this->viewBuilder()->setClassName('Json');
        $this->set([
            'address' => $address,
            '_serialize' => ['address']
        ]);
    }

// Table code
    /**
     * Returns the most recently published address for the provided location name or FALSE if none is found
     *
     * @param string|null $location Location name
     * @return string|boolean
     */
    public function getAddress($location)
    {
        if (!$location) {
            return false;
        }

        /** @var Event|null $result */
        $result = $this->find()
            ->select(['Events.address'])
            ->where([
                'Events.published' => true,
                'Events.location' => $location,
                'Events.address NOT' => ''
            ])
            ->orderDesc('Events.created')
            ->first();

        return $result ? $result->address : false;
    }

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions