-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to select image as primary #42
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it works, there are issues:
- API sub-endpoints are looking wrong.
- DB has no FKs for primary_image.
- It's not clear which image is primary after clicking the icon in UI. I'd either refresh whole page to show new primary in place.
{ | ||
public function safeUp() | ||
{ | ||
$this->addColumn('{{%project}}', 'primary_image_id', $this->integer()->null()->comment('Primary image')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Foreign key is missing:
$this->addForeignKey('fk-project-primary_image_id', '{{%project}}', 'primary_image_id', '{{%image}}', 'id', 'SET NULL');
|
||
public function safeDown() | ||
{ | ||
$this->dropColumn('{{%project}}', 'primary_image_id'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to drop FK here if we add it:
$this->dropForeignKey('fk-project-primary_image_id', '{{%project}}');
models/Image.php
Outdated
@@ -270,6 +287,13 @@ public function afterDelete() | |||
$this->removeFile($this->getBigThumbnailPath()); | |||
$this->removeFile($this->getFullPath()); | |||
$this->removeFile($this->getOriginalPath()); | |||
|
|||
if ($this->project->primary_image_id == $this->id) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this code since we can use foreign key SET NULL
.
models/Project.php
Outdated
@@ -101,12 +104,13 @@ public function rules() | |||
[['url', 'source_url'], 'url'], | |||
[['yii_version'], 'in', 'range' => array_keys(self::versions())], | |||
[['description', 'tagValues'], 'safe'], | |||
['primary_image_id', 'integer'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think exists
validator should be added as well.
@@ -171,22 +193,25 @@ public function getPlaceholderAbsoluteUrl() | |||
return Url::to($this->getPlaceholderRelativeUrl(), 'http'); | |||
} | |||
|
|||
/** | |||
* @return string | |||
*/ | |||
public function getPrimaryImageThumbnailRelativeUrl() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to fall back to any image than to default one:
if ($this->primaryImage) {
return $this->primaryImage->getThumbnailRelativeUrl();
}
if (!empty($this->images)) {
return $this->images[0]->getThumbnailRelativeUrl();
}
return $this->getPlaceholderRelativeUrl();
* @return Project | ||
* @throws NotFoundHttpException | ||
*/ | ||
protected function findProject($projectId, $userId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could take User
instead of ID as argument.
modules/api1/docs.md
Outdated
@@ -57,6 +57,32 @@ You may pass additional parameters when querying a list: | |||
returned. | |||
- `yiiVersion` - version of the framework project built with. Either `1.0` or `1.1`. | |||
|
|||
|
|||
### Get primary image <a href="#projects-view-primary-image" id="projects-view-primary-image">#</a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Primary image ID by itself isn't useful. I'd include this info into GET /project
if it's interesting to expose it via API at all...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it gives image info, I'd add /image
endpoint where deleting an image could be moved as well as getting image info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I saw that the project is already return the link to the primary image as "thumbnail". I removed an additional query to get the primary image.
modules/api1/docs.md
Outdated
- updatedAt: UNIX timestamp indicating when image was updated last time. | ||
|
||
|
||
### Update primary image <a href="#projects-update-primary-image" id="projects-update-primary-image">#</a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it primaryImageID
would be exposed via /project/1
API then it would make sense to update using POST /project/1
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added fixes
- maybe "PUT"?
- In order, I'm using the property name "primary_image_id" and Project::SCENARIO_MANAGE. Probably a bad idea to call fields in api as in a DB, but how flexible)
views/project/screenshots.php
Outdated
@@ -59,6 +61,16 @@ | |||
<i class="fa fa-times fa-stack-1x fa-inverse"></i> | |||
</span> | |||
</span> | |||
<span class="primary-image <?= $model->primary_image_id == $image->id ? 'hide' : '' ?>" | |||
data-image-id="<?= $image->id ?>" | |||
data-url="<?= Url::to(["api/1.0/projects/{$image->project->id}/primary-image"]) ?>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL forming is not correct. Should be:
Url::to(['/api1/project/primary-image', 'id' => $image->project->id])
views/project/screenshots.php
Outdated
<span class="primary-image <?= $model->primary_image_id == $image->id ? 'hide' : '' ?>" | ||
data-image-id="<?= $image->id ?>" | ||
data-url="<?= Url::to(["api/1.0/projects/{$image->project->id}/primary-image"]) ?>" | ||
title="<?= Yii::t('project', 'Make as primary image') ?>"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Use as primary image" or "Mark as primary image" or "Make this image primary".
Added fixes for all items. |
modules/api1/docs.md
Outdated
|
||
> PUT [/projects/1](/en/api/1.0/projects) | ||
|
||
- primary_image_id: id of the primary image. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess more fields are available, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, also available: 'title', 'url', 'is_opensource', 'source_url', 'yii_version', 'description', 'status', 'tagValues'.
I can add them to docs or create other a scenario for update via API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding them would be good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Merged. Thank you very much for taking care of it. |
issue #15