Skip to content

Commit

Permalink
Tidied the find method to throw exceptions as expected, added seeder …
Browse files Browse the repository at this point in the history
…and fixed the model factory published date.
  • Loading branch information
zakhenry committed Jul 27, 2015
1 parent 7e3de39 commit 8f0df03
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
19 changes: 12 additions & 7 deletions api/app/Repositories/ArticleRepository.php
Expand Up @@ -22,18 +22,23 @@ class ArticleRepository extends BaseRepository
*/
public function find($id, $columns = ['*'])
{
$result = $this->model->where('permalink', '=', $id)->get()->first();

/** @var ArticlePermalink $permalink */
if (!$result && $permalink = ArticlePermalink::find($id)) {
$result = $permalink->article;
//if the id is a uuid, try that or fail.
if (Uuid::isValid($id)){
return $query = $this->model->findOrFail($id);

This comment has been minimized.

Copy link
@Redjik

Redjik Jul 27, 2015

Contributor

no need for $query

}

if (!$result && Uuid::isValid($id)) {
$result = parent::find($id);
//otherwise attempt treat the id as a permalink and first try the model, then try the history
try {

return $this->model
->where('permalink', '=', $id)
->firstOrFail();

}catch(ModelNotFoundException $e){ //id or permalink not found, try permalink history
return ArticlePermalink::findOrFail($id)->article;
}

return $result;
}


Expand Down
6 changes: 3 additions & 3 deletions api/database/factories/ModelFactory.php
Expand Up @@ -109,12 +109,12 @@
});

$factory->define(App\Models\Article::class, function ($faker) {
$publishedDatetime = $faker->optional(0.9)->dateTimeThisDecade();

return [
'article_id' => $faker->uuid,
'title' => $faker->sentence,
'content' => $content = implode("\n\n", $faker->paragraphs(3)),
// ErrorException: createFromFormat() expects parameter 2 to be string, object given 0_o
//'first_published' => $faker->optional(0.9)->dateTimeThisDecade(),
'first_published' => null,
'first_published' => $publishedDatetime ? $publishedDatetime->format('Y-m-d H:i:s') : null,
];
});
21 changes: 21 additions & 0 deletions api/database/seeds/ArticleSeeder.php
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Seeder;

class ArticleSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Models\Article::class, 10)->create();

// factory(App\Models\Article::class)->create()
// ->permalink()->save(factory(App\Models\ArticlePermalink::class)->make())
// ;

}
}
7 changes: 6 additions & 1 deletion api/database/seeds/DatabaseSeeder.php
Expand Up @@ -12,8 +12,13 @@ class DatabaseSeeder extends Seeder
public function run()
{
$this->call('UserStorySeeder');
$this->command->info('User story seeded!');

$this->call('TestEntitySeeder');
$this->command->info('Test entities seeded!');

$this->call('ArticleSeeder');
$this->command->info('Articles seeded!');

$this->command->info('User table seeded!');
}
}

0 comments on commit 8f0df03

Please sign in to comment.