Skip to content

Commit

Permalink
Added PostContent
Browse files Browse the repository at this point in the history
  • Loading branch information
stanfortonski committed Jun 6, 2021
1 parent c5aec84 commit e7aae0c
Show file tree
Hide file tree
Showing 15 changed files with 507 additions and 314 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/Admin/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Requests\ImageRequest;
use App\Models\Category;
use App\Models\Content;
use App\Services\CategoryContentUrlValidator;
use App\Services\ContentUrlValidator;
use App\Services\ThumbnailManager;
use Exception;
Expand All @@ -16,7 +17,7 @@

class CategoriesController extends Controller
{
use ThumbnailManager, ContentUrlValidator;
use ThumbnailManager, CategoryContentUrlValidator;

/**
* Display a listing of the resource.
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/Admin/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use App\Http\Requests\ImageRequest;
use App\Http\Requests\PostStoreRequest;
use App\Models\Category;
use App\Models\Content;
use App\Models\PostContent;
use App\Models\Post;
use App\Services\ContentUrlValidator;
use App\Services\PostContentUrlValidator;
use App\Services\ThumbnailManager;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
Expand All @@ -17,7 +17,7 @@

class PostsController extends Controller
{
use ThumbnailManager, ContentUrlValidator;
use ThumbnailManager, PostContentUrlValidator;

/**
* Display a listing of the resource.
Expand Down Expand Up @@ -65,7 +65,7 @@ public function store(PostStoreRequest $request)

$contentData = $request->content;
$contentData['lang'] = app()->getLocale();
$content = Content::create($contentData);
$content = PostContent::create($contentData);
$post->contents()->saveMany([$content]);
DB::commit();

Expand Down Expand Up @@ -125,7 +125,7 @@ public function update(PostStoreRequest $request, Post $post)
$contentData = $request->content;
$contentData['lang'] = app()->getLocale();
if (empty($content)){
$content = Content::create($contentData);
$content = PostContent::create($contentData);
$post->contents()->saveMany([$content]);
}
else $content->update($contentData);
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function content()

public function contents()
{
return $this->belongsToMany(Content::class, 'contents_of_posts', 'post_id', 'content_id');
return $this->belongsToMany(PostContent::class, 'contents_of_posts', 'post_id', 'content_id');
}

public function scopeVisible($query)
Expand Down
8 changes: 8 additions & 0 deletions app/Models/PostContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Models;

class PostContent extends Content
{
protected $table = 'post_contents';
}
18 changes: 18 additions & 0 deletions app/Services/CategoryContentUrlValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Services;

use App\Models\Content;

trait CategoryContentUrlValidator
{
use ContentUrlValidator;

public function validateContentUrlWithoutOne($request, $content){
return $this->baseValidateContentUrlWithoutOne(Content::class, $request, $content);
}

public function validateContentUrl($request){
return $this->baseValidateContentUrl(Content::class, $request);
}
}
10 changes: 4 additions & 6 deletions app/Services/ContentUrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
namespace App\Services;

use App\Models\Content;
use Illuminate\Validation\Rule;

trait ContentUrlValidator
{
public function validateContentUrlWithoutOne($request, Content $content){
return Content::where('url', '=', $request->content['url'])->where('lang', '=', app()->getLocale())->where('id', '!=', $content->id)->count() == 0;
public function baseValidateContentUrlWithoutOne($usedClass, $request, $content){
return $usedClass::where('url', '=', $request->content['url'])->where('lang', '=', app()->getLocale())->where('id', '!=', $content->id)->count() == 0;
}

public function validateContentUrl($request){
return Content::where('url', '=', $request->content['url'])->where('lang', '=', app()->getLocale())->count() == 0;
public function baseValidateContentUrl($usedClass, $request){
return $usedClass::where('url', '=', $request->content['url'])->where('lang', '=', app()->getLocale())->count() == 0;
}
}

18 changes: 18 additions & 0 deletions app/Services/PostContentUrlValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Services;

use App\Models\PostContent;

trait PostContentUrlValidator
{
use ContentUrlValidator;

public function validateContentUrlWithoutOne($request, $content){
return $this->baseValidateContentUrlWithoutOne(PostContent::class, $request, $content);
}

public function validateContentUrl($request){
return $this->baseValidateContentUrl(PostContent::class, $request);
}
}
Loading

0 comments on commit e7aae0c

Please sign in to comment.