Fixed password re-writing and improved book slug creation

This commit is contained in:
Dan Brown 2015-08-09 10:26:54 +01:00
parent a4f21e9482
commit b2223f1618
3 changed files with 22 additions and 11 deletions

View File

@ -61,11 +61,7 @@ class BookController extends Controller
'description' => 'string|max:1000' 'description' => 'string|max:1000'
]); ]);
$book = $this->bookRepo->newFromInput($request->all()); $book = $this->bookRepo->newFromInput($request->all());
$slug = Str::slug($book->name); $book->slug = $this->bookRepo->findSuitableSlug($book->name);
while($this->bookRepo->countBySlug($slug) > 0) {
$slug .= '1';
}
$book->slug = $slug;
$book->created_by = Auth::user()->id; $book->created_by = Auth::user()->id;
$book->updated_by = Auth::user()->id; $book->updated_by = Auth::user()->id;
$book->save(); $book->save();
@ -111,11 +107,7 @@ class BookController extends Controller
'description' => 'string|max:1000' 'description' => 'string|max:1000'
]); ]);
$book->fill($request->all()); $book->fill($request->all());
$slug = Str::slug($book->name); $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id);
while($this->bookRepo->countBySlug($slug) > 0 && $book->slug != $slug) {
$slug += '1';
}
$book->slug = $slug;
$book->updated_by = Auth::user()->id; $book->updated_by = Auth::user()->id;
$book->save(); $book->save();
return redirect($book->getUrl()); return redirect($book->getUrl());

View File

@ -1,5 +1,6 @@
<?php namespace Oxbow\Repos; <?php namespace Oxbow\Repos;
use Illuminate\Support\Str;
use Oxbow\Book; use Oxbow\Book;
class BookRepo class BookRepo
@ -62,4 +63,22 @@ class BookRepo
return $lastElem ? $lastElem->priority + 1 : 0; return $lastElem ? $lastElem->priority + 1 : 0;
} }
public function doesSlugExist($slug, $currentId = false)
{
$query = $this->book->where('slug', '=', $slug);
if($currentId) {
$query = $query->where('id', '!=', $currentId);
}
return $query->count() > 0;
}
public function findSuitableSlug($name, $currentId = false)
{
$slug = Str::slug($name);
while($this->doesSlugExist($slug, $currentId)) {
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
}
return $slug;
}
} }

View File

@ -24,7 +24,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
* *
* @var array * @var array
*/ */
protected $fillable = ['name', 'email', 'password']; protected $fillable = ['name', 'email'];
/** /**
* The attributes excluded from the model's JSON form. * The attributes excluded from the model's JSON form.