Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Change Log
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## xx.yy.zz
### Fixed
- {description_of_fixed}

### Added
- {description_of_added}

### Changed
- {description_of_added}

### Removed
- {description_of_removed}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@ Lint code using [PSR-2](http://www.php-fig.org/psr/psr-2/) coding style
```bash
composer lint
```

## Debugging

### Setting up xDebug with PhpStorm

Increase your composer's timeout limit by adding these lines to your
`~/.bashrc` file

```
export COMPOSER_PROCESS_TIMEOUT=1200
export COMPOSER_DISABLE_XDEBUG_WARN=1
```
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"ext-pdo_sqlite": "*",
"phramework/phramework": "^1.3",
"phramework/jsonapi": "1.0.0-RC19",
"ext-pdo_sqlite": "*",
"phramework/basic-authentication": "^0.0.0"
},
"require-dev": {
Expand Down
52 changes: 52 additions & 0 deletions db/db_changes/170605_vm_create_table_review.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
CREATE TABLE review(
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`score` INTEGER NOT NULL CHECK (`score` BETWEEN 1 AND 10),
`status` INTEGER NOT NULL CHECK (`status` BETWEEN 0 AND 1),
`user_exist` INTEGER NOT NULL CHECK (`user_exist` BETWEEN 0 AND 1)
);

CREATE TABLE article_review(
`article_id` INTEGER NOT NULL,
`review_id` INTEGER NOT NULL,
`status` INTEGER NOT NULL CHECK (`status` BETWEEN 0 AND 1),
PRIMARY KEY (article_id,review_id),
FOREIGN KEY (article_id) REFERENCES article(id),
FOREIGN KEY (review_id) REFERENCES review(id)
);

CREATE TABLE user_review(
`user_id` INTEGER NOT NULL,
`review_id` INTEGER NOT NULL,
`status` INTEGER NOT NULL CHECK (`status` BETWEEN 0 AND 1),
PRIMARY KEY (user_id,review_id),
FOREIGN KEY (user_id) REFERENCES user(id),
FOREIGN KEY (review_id) REFERENCES review(id)
);

ALTER TABLE 'user_review' RENAME TO 'user-review';
ALTER TABLE "article_review" RENAME TO "article-review";

DROP TABLE review;
DROP TABLE "article-review";
DROP TABLE "user-review";

CREATE TABLE review (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`score` INTEGER NOT NULL CHECK (`score` BETWEEN 1 AND 10),
`status` INTEGER NOT NULL CHECK (`status` BETWEEN 0 AND 1),
`user_id` INTEGER REFERENCES user (id)
);

CREATE TABLE 'article-review'(
`article_id` INTEGER NOT NULL REFERENCES article(id),
`review_id` INTEGER NOT NULL REFERENCES review(id),
`status` INTEGER NOT NULL CHECK (`status` BETWEEN 0 AND 1),
constraint article_review PRIMARY KEY (article_id,review_id)
);

CREATE TABLE `user-review`(
`user_id` INTEGER NOT NULL REFERENCES user(id),
`review_id` INTEGER NOT NULL REFERENCES review(id),
`status` INTEGER NOT NULL CHECK (`status` BETWEEN 0 AND 1),
CONSTRAINT user_review PRIMARY KEY (user_id,review_id)
);
30 changes: 30 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@
'byIdRelationships',
Phramework::METHOD_ANY
],
[
'review/', //URI
NS . 'ReviewController', //Class
'GET', //Class method
Phramework::METHOD_GET, //HTTP Method
],
[
'review/{id}',
NS . 'ReviewController',
'GETById',
Phramework::METHOD_GET
],
[
'review/',
NS . 'ReviewController',
'POST',
Phramework::METHOD_POST
],
[
'review/{id}',
NS . 'ReviewController',
'PATCH',
Phramework::METHOD_PATCH
],
[
'review/{id}/relationships/{relationship}',
NS . 'ReviewController',
'byIdRelationships',
Phramework::METHOD_ANY
],
]);

//Initialize API with settings and routing
Expand Down
155 changes: 155 additions & 0 deletions src/Controllers/ReviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
/*
* Copyright 2017-2018 Vasilis Manolas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Phramework\Examples\JSONAPI\Controllers;

use Phramework\Examples\JSONAPI\Models\Review;
use Phramework\Phramework;
use Phramework\Examples\JSONAPI\Models\Article;

/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Vasilis Manolas <vasileiosmanolas@gmail.com>
*/
class ReviewController extends \Phramework\Examples\JSONAPI\Controller
{
/**
* Get collection
* `/review/` handler
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
*/
public static function GET(\stdClass $params, string $method, array $headers)
{
static::handleGET(
$params,
Review::class,
[],
[]
);
}

/**
* Get a resource
* `/review/{id}/` handler
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
* @param string $id Resource id
*/
public static function GETById(\stdClass $params, string $method, array $headers, string $id)
{
static::handleGETById(
$params,
$id,
Review::class,
[],
[]
);
}

/**
* Post new resource
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
*/
public static function POST(\stdClass $params, string $method, array $headers)
{
static::handlePOST(
$params,
$method,
$headers,
Review::class
);
}

/**
* Update a resource
* @param $params
* @param $method
* @param $headers
* @param string $id
*/
public static function PATCH(
\stdClass $params,
string $method,
array $headers,
string $id
) {
static::handlePATCH(
$params,
$method,
$headers,
$id,
Review::class
);
}

/**
* Delete a resource
* @param $params
* @param $method
* @param $headers
* @param string $id
*/
public static function DELETE(
\stdClass $params,
string $method,
array $headers,
string $id
) {
static::handleDELETE(
$params,
$method,
$headers,
$id,
Review::class
);
}

/**
* Manage resource's relationships
* `/article/{id}/relationships/{relationship}/` handler
* @param \stdClass $params Request parameters
* @param string $method Request method
* @param array $headers Request headers
* @param string $id Resource id
* @param string $relationship Relationship
*/
public static function byIdRelationships(
$params,
$method,
$headers,
string $id,
string $relationship
) {
static::handleByIdRelationships(
$params,
$method,
$headers,
$id,
$relationship,
Review::class,
[Phramework::METHOD_GET],
[],
[]
);
}
}
37 changes: 37 additions & 0 deletions src/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ public static function getRelationshipTag(
return $ids;
}

/**
* Get all articles with given review id
* @param string $reviewId
* @return string[]
*/
public static function getRelationshipReview(
string $reviewId,
Fields $fields = null,
$flags = Resource::PARSE_DEFAULT
) {
$ids = Database::executeAndFetchAllArray(
'SELECT "article-review"."article_id"
FROM "article-review"
JOIN "article"
ON "article"."id" = "article-review"."article_id"
WHERE
"article-review"."review_id" = ?
AND "article-review"."status" <> 0
AND "article"."status" <> 0',
[$reviewId]
);

return $ids;
}

/**
* Get all articles with given creator id
* @param string $userId
Expand Down Expand Up @@ -224,6 +249,18 @@ public static function getRelationships()
]
],
Relationship::FLAG_DEFAULT | Relationship::FLAG_DATA
),
'review' => new Relationship(
Review::class,
Relationship::TYPE_TO_MANY,
null, //source data attribute
(object) [ //source data callback
Phramework::METHOD_GET => [
Review::class,
'getRelationshipArticle'
]
],
Relationship::FLAG_DEFAULT | Relationship::FLAG_DATA
)
];
}
Expand Down
Loading