Skip to content

Commit

Permalink
Unique URL bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislawfortonski committed Apr 10, 2021
1 parent aaec9fc commit c3fb3af
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
9 changes: 6 additions & 3 deletions app/Http/Controllers/Admin/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function create()
*/
public function store(CategoryStoreRequest $request)
{
$this->validateContentUrl($request);
if (!$this->validateContentUrl($request))
return redirect()->back()->withError(__('This url already exists.'));

DB::beginTransaction();
try {
Expand Down Expand Up @@ -100,10 +101,12 @@ public function update(CategoryStoreRequest $request, Category $category)
{
$content = $category->content()->first();
if (!empty($content)){
$this->validateContentUrlWithoutOne($request, $content);
if (!$this->validateContentUrlWithoutOne($request, $content))
return redirect()->back()->withError(__('This url already exists.'));
}
else {
$this->validateContentUrl($request);
if (!$this->validateContentUrl($request))
return redirect()->back()->withError(__('This url already exists.'));
}

DB::beginTransaction();
Expand Down
9 changes: 6 additions & 3 deletions app/Http/Controllers/Admin/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function create()
*/
public function store(PostStoreRequest $request)
{
$this->validateContentUrl($request);
if (!$this->validateContentUrl($request))
return redirect()->back()->withError(__('This url already exists.'));

DB::beginTransaction();
try {
Expand Down Expand Up @@ -106,10 +107,12 @@ public function update(PostStoreRequest $request, Post $post)
{
$content = $post->content()->first();
if (!empty($content)){
$this->validateContentUrlWithoutOne($request, $content);
if (!$this->validateContentUrlWithoutOne($request, $content))
return redirect()->back()->withError(__('This url already exists.'));
}
else {
$this->validateContentUrl($request);
if (!$this->validateContentUrl($request))
return redirect()->back()->withError(__('This url already exists.'));
}

if ($post->user_id == auth()->user()->id || auth()->user()->hasOneOfRoles(['admin', 'mod'])){
Expand Down
5 changes: 3 additions & 2 deletions app/Services/ContentUrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
trait ContentUrlValidator
{
public function validateContentUrlWithoutOne($request, Content $content){
$this->validate($request, ['content.url' => Rule::unique('contents', 'url')->ignore($content->id)]);
return Content::where('url', '=', $request->content['url'])->where('lang', '=', app()->getLocale())->where('id', '!=', $content->id)->count() == 0;
}

public function validateContentUrl($request){
$this->validate($request, ['content.url' => Rule::unique('contents', 'url')]);
return Content::where('url', '=', $request->content['url'])->where('lang', '=', app()->getLocale())->count() == 0;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up()
$table->string('lang', 2);
$table->string('title');
$table->text('content');
$table->string('url')->unique();
$table->string('url');
$table->timestamps();
});
}
Expand Down
3 changes: 2 additions & 1 deletion resources/lang/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@
"No users.": "Brak użytkowników.",
"Tags": "Tagi",
"Other posts that may interest you": "Inne posty ktore mogą cię zainteresować",
"Toggle navigation": "Przełącz nawigację"
"Toggle navigation": "Przełącz nawigację",
"This url already exists.": "Taki link już istnieje."
}

0 comments on commit c3fb3af

Please sign in to comment.