Skip to content
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

Merged
merged 10 commits into from
Jun 9, 2017
Merged

Conversation

prodex
Copy link
Contributor

@prodex prodex commented Jun 8, 2017

issue #15

Copy link
Owner

@samdark samdark left a 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'));
Copy link
Owner

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');
Copy link
Owner

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) {
Copy link
Owner

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.

@@ -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']
Copy link
Owner

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()
Copy link
Owner

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)
Copy link
Owner

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.

@@ -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>
Copy link
Owner

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...

Copy link
Owner

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.

Copy link
Contributor Author

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.

- 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>
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added fixes

  1. maybe "PUT"?
  2. 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)

@@ -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"]) ?>"
Copy link
Owner

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])

<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') ?>">
Copy link
Owner

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".

@prodex
Copy link
Contributor Author

prodex commented Jun 9, 2017

Added fixes for all items.


> PUT [/projects/1](/en/api/1.0/projects)

- primary_image_id: id of the primary image.
Copy link
Owner

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?

Copy link
Contributor Author

@prodex prodex Jun 9, 2017

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.

Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@samdark samdark merged commit 8c23cb6 into samdark:master Jun 9, 2017
@samdark
Copy link
Owner

samdark commented Jun 9, 2017

Merged. Thank you very much for taking care of it.

@prodex prodex deleted the main-image branch June 9, 2017 07:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants