Jump to content

Resp API in my resources


Adriano Faria
 Share

Go to solution Solved by Ryan Ashbrook,

Recommended Posts

I'm adding suport to Rest API in my resources and I'm facing a issue in the categories. I always get the error:

Quote

{ "errorCode": "EX1054", "errorMessage": "UNKNOWN_ERROR" }

MySQL 1054 is unknown column in 'Field List' so debugging I see the error comes from IPS\Node\Api\NodeController_list():

	protected function _list( $where = array() )
	{
		$class = $this->class;

		/* Return */
		return new \IPS\Api\PaginatedResponse(
			200,
			\IPS\Db::i()->select( '*', $class::$databaseTable, $where, $class::$databaseColumnOrder ? $class::$databaseColumnOrder . " asc" : NULL ),
			isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
			$class,
			\IPS\Db::i()->select( 'COUNT(*)', $class::$databaseTable, $where )->first()
		);
	}

And debugging more, I see the error comes from the $class::$databaseColumnOrder.  I got on my node model:

	/**
	 * @brief	[ActiveRecord] Database Table
	 */
	public static $databaseTable = 'links_categories';
	
	/**
	 * @brief	[ActiveRecord] Database Prefix
	 */
	public static $databasePrefix = 'category_';
		
	/**
	 * @brief	[Node] Order Database Column
	 */
	public static $databaseColumnOrder = 'position';

Thing is: if I change my $databaseColumnOrder to add the $databasePrefix in my api call, it works fine.

So I changed my IPS\my_app\api\categories from:

	public function GETindex()
	{
		/* Return */
		return $this->_list();
	}

to:

	public function GETindex()
	{
		/* Return */
		return new \IPS\Api\PaginatedResponse(
			200,
			\IPS\Db::i()->select( '*', 'links_categories', NULL, "category_position asc" ),
			isset( \IPS\Request::i()->page ) ? \IPS\Request::i()->page : 1,
			'IPS\links\Category',
			\IPS\Db::i()->select( 'COUNT(*)', 'links_categories' )->first()
		);
	}

(Notice the position column with prefix).

Things works:

Quote

{ "page": 1, "perPage": 25, "totalResults": 1, "totalPages": 1, "results": [ { "id": 1, "title": "Test Category", "links": 0, "url": "http:\/\/localhost\/general\/index.php?\/links\/category\/1-test-category\/" } ] }

So the question is: am I doing something wrong? Is there anything I'm forgetting like the prefix to add/remove somewhere? Or is it a bug?

My whole categories file is:

namespace IPS\links\api;

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
	exit;
}

/**
 * @brief	Categories API
 */
class _categories extends \IPS\Node\Api\NodeController
{
	/**
	 * Class
	 */
	protected $class = 'IPS\links\Category';
	
	/**
	 * GET /links/category/
	 * Get list of categories
	 *
	 * @return		\IPS\Api\PaginatedResponse<IPS\links\Category>
	 */
	public function GETindex()
	{
		/* Return */
		return $this->_list();
	}

	/**
	 * GET /links/category/{id}
	 * Get specific forum
	 *
	 * @param		int		$id			ID Number
	 *
	 * @return		\IPS\Api\PaginatedResponse<IPS\links\Category>
	 */
	public function GETitem( $id )
	{
		/* Return */
		return $this->_view( $id );
	}

	/**
	 * POST /links/category/
	 * Create a category
	 *
	 * @reqapiparam	string		title				The category name
	 * @apiparam	string		description			The category description
	 * @apiparam	int|null	parent				The ID number of the parent the category should be created in. NULL for root.
	 * @apiparam	int			theme				Theme to use as an override
	 * @apiparam	int			sitemap_priority	1-9 1 highest priority
	 * @apiparam	int			min_content			The minimum amount of posts to be able to view
	 * @apiparam	int			can_see_others		0|1 Users can see topics posted by other users?
	 * @return		\IPS\links\Category
	 */
	public function POSTindex()
	{
		return $this->_create();
	}
	
	/**
	 * POST /links/category/{id}
	 * Edit a forum
	 *
	 * @reqapiparam	string		title				The forum title
	 * @apiparam	string		description			The forum description
	 * @apiparam	string		type				normal|qa|redirect|category
	 * @apiparam	int|null	parent				The ID number of the parent the forum should be created in. NULL for root.
	 * @apiparam	string		password			Forum password
	 * @apiparam	int			theme				Theme to use as an override
	 * @apiparam	int			sitemap_priority	1-9 1 highest priority
	 * @apiparam	int			min_content			The minimum amount of posts to be able to view
	 * @apiparam	int			can_see_others		0|1 Users can see topics posted by other users?
	 * @return		\IPS\forums\Topic
	 */
	public function POSTitem( $id )
	{
		$class = $this->class;
		$category = $class::load( $id );
		$category = $this->_createOrUpdate( $category );

		return $category;
	}
	
	/**
	 * DELETE /links/category/{id}
	 * Delete a forum
	 *
	 * @param		int		$id			ID Number
	 * @return		void
	 */
	public function DELETEitem( $id )
	{
		return $this->_delete( $id );
	}

	/**
	 * Create or update node
	 *
	 * @param	\IPS\node\Model	$category				The node
	 * @return	\IPS\node\Model
	 */
	protected function _createOrUpdate( \IPS\node\Model $category )
	{
		if ( !\IPS\Request::i()->title )
		{
			throw new \IPS\Api\Exception( 'NO_TITLE', '', 400 );
		}

		foreach ( array( 'title' => "links_category_{$category->id}", 'description' => "links_category_{$category->id}_desc" ) as $fieldKey => $langKey )
		{
			if ( isset( \IPS\Request::i()->$fieldKey ) )
			{
				\IPS\Lang::saveCustom( 'links', $langKey, \IPS\Request::i()->$fieldKey );

				if ( $fieldKey === 'title' )
				{
					$category->seo_title = \IPS\Http\Url\Friendly::seoTitle( \IPS\Request::i()->$fieldKey );
				}
			}
		}

		$category->parent = (int) \IPS\Request::i()->parent?: -1;

		return parent::_createOrUpdate( $category );
	}
}

Tks.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...