Skip to content

Commit

Permalink
Add administration + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xety committed Mar 12, 2018
1 parent 305712e commit 9e3074f
Show file tree
Hide file tree
Showing 23 changed files with 741 additions and 17 deletions.
10 changes: 10 additions & 0 deletions app/Http/Controllers/Admin/Blog/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

class ArticleController extends Controller
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();

$this->breadcrumbs->addCrumb('Blog', route('admin.blog.article.index'));
}

/**
* Show all articles.
*
Expand Down
10 changes: 10 additions & 0 deletions app/Http/Controllers/Admin/Blog/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

class CategoryController extends Controller
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();

$this->breadcrumbs->addCrumb('Blog', route('admin.blog.article.index'));
}

/**
* Show all categories.
*
Expand Down
138 changes: 138 additions & 0 deletions app/Http/Controllers/Admin/Discuss/CategoryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
namespace Xetaravel\Http\Controllers\Admin\Discuss;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Xetaravel\Http\Controllers\Admin\Controller;
use Xetaravel\Models\DiscussCategory;
use Xetaravel\Models\Repositories\DiscussCategoryRepository;
use Xetaravel\Models\Validators\DiscussCategoryValidator;

class CategoryController extends Controller
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();

$this->breadcrumbs->addCrumb('Discuss', route('admin.discuss.category.index'));
}

/**
* Show all categories.
*
* @return \Illuminate\View\View
*/
public function index(): View
{
$categories = DiscussCategory::paginate(config('xetaravel.pagination.discuss.conversation_per_page'));

$this->breadcrumbs->addCrumb('Manage Categories', route('admin.discuss.category.index'));

return view(
'Admin::Discuss.category.index',
['categories' => $categories, 'breadcrumbs' => $this->breadcrumbs]
);
}

/**
* Show the caterory create form.
*
* @return \Illuminate\View\View
*/
public function showCreateForm(): View
{
$breadcrumbs = $this->breadcrumbs
->addCrumb('Manage Categories', route('admin.discuss.category.index'))
->addCrumb("Create", route('admin.discuss.category.create'));

return view('Admin::Discuss.category.create', ['breadcrumbs' => $this->breadcrumbs]);
}

/**
* Handle a category create request for the application.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function create(Request $request): RedirectResponse
{
DiscussCategoryValidator::create($request->all())->validate();
DiscussCategoryRepository::create($request->all());

return redirect()
->route('admin.discuss.category.index')
->with('success', 'Your category has been created successfully !');
}

/**
* Show the category update form.
*
* @param string $slug The slug of the category.
* @param int $id The id of the category.
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
public function showUpdateForm(string $slug, int $id)
{
$category = DiscussCategory::findOrFail($id);

$breadcrumbs = $this->breadcrumbs
->addCrumb('Manage Categories', route('admin.discuss.category.index'))
->addCrumb(
"Update : " . e(str_limit($category->title, 30)),
route(
'admin.discuss.category.index',
['slug' => $category->slug, 'id' => $category->id]
)
);

return view('Admin::Discuss.category.update', compact('category', 'breadcrumbs'));
}

/**
* Handle an category update request for the application.
*
* @param \Illuminate\Http\Request $request
* @param int $id The id of the category.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, int $id): RedirectResponse
{
$category = DiscussCategory::findOrFail($id);

DiscussCategoryValidator::update($request->all(), $id)->validate();
DiscussCategoryRepository::update($request->all(), $category);

return redirect()
->route('admin.discuss.category.index')
->with('success', 'Your category has been updated successfully !');
}

/**
* Handle the delete request for the category.
*
* @param int $id The id of the category to delete.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete(int $id): RedirectResponse
{
$category = DiscussCategory::findOrFail($id);

if ($category->delete()) {
return redirect()
->route('admin.discuss.category.index')
->with('success', 'This category has been deleted successfully !');
}

return redirect()
->route('admin.discuss.category.index')
->with('danger', 'An error occurred while deleting this category !');
}
}
10 changes: 10 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ class Category extends Model
use Sluggable,
CategoryPresenter;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'description'
];

/**
* The accessors to append to the model's array form.
*
Expand Down
24 changes: 23 additions & 1 deletion app/Models/DiscussCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class DiscussCategory extends Model
use Sluggable,
DiscussCategoryPresenter;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'color',
'is_locked',
'description'
];

/**
* The accessors to append to the model's array form.
*
Expand All @@ -33,6 +45,16 @@ protected static function boot()
static::updating(function ($model) {
$model->generateSlug();
});

// Reset the last_conversation_id field and handle the conversations deleting.
static::deleting(function ($model) {
$model->last_conversation_id = null;
$model->save();

foreach ($model->conversations as $conversation) {
$conversation->delete();
}
});
}

/**
Expand All @@ -52,7 +74,7 @@ public function slugStrategy(): string
*/
public function conversations()
{
return $this->hasMany(DiscussConversation::class);
return $this->hasMany(DiscussConversation::class, 'category_id', 'id');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Repositories/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ArticleRepository
*/
public static function sidebar(): Collection
{
return Article::latest()->take(5)->get();
return Article::latest()->take(config('xetaravel.blog.articles_sidebar'))->get();
}

/**
Expand Down
15 changes: 7 additions & 8 deletions app/Models/Repositories/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ class CategoryRepository
*/
public static function sidebar(): Collection
{
return Category::take(25)->orderBy('title', 'asc')->get();
return Category::take(config('xetaravel.blog.categories_sidebar'))->orderBy('title', 'asc')->get();
}

/**
* Create the new category and save it.
*
* @param array $data The data used to create the category.
*
* @return bool
* @return \Xetaravel\Models\Category
*/
public static function create(array $data): bool
public static function create(array $data): Category
{
$category = new Category;
$category->title = $data['title'];
$category->description = $data['description'];

return $category->save();
return Category::create([
'title' => $data['title'],
'description' => $data['description']
]);
}

/**
Expand Down
36 changes: 35 additions & 1 deletion app/Models/Repositories/DiscussCategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Illuminate\Support\Collection;
use Xetaravel\Models\DiscussCategory;
use Xetaravel\Models\DiscussConversation;

class DiscussCategoryRepository
{
Expand All @@ -16,4 +15,39 @@ public static function sidebar(): Collection
{
return DiscussCategory::take(config('xetaravel.discuss.categories_sidebar'))->orderBy('title', 'asc')->get();
}

/**
* Create the new category and save it.
*
* @param array $data The data used to create the category.
*
* @return \Xetaravel\Models\DiscussCategory
*/
public static function create(array $data) : DiscussCategory
{
return DiscussCategory::create([
'title' => $data['title'],
'color' => $data['color'],
'is_locked' => isset($data['is_locked']) ? true : false,
'description' => $data['description']
]);
}

/**
* Update the category data and save it.
*
* @param array $data The data used to update the category.
* @param \Xetaravel\Models\DiscussCategory $category The category to update.
*
* @return bool
*/
public static function update(array $data, DiscussCategory $category) : bool
{
$category->title = $data['title'];
$category->color = $data['color'];
$category->is_locked = isset($data['is_locked']) ? true : false;
$category->description = $data['description'];

return $category->save();
}
}
2 changes: 1 addition & 1 deletion app/Models/Validators/CategoryValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function create(array $data): Validator
{
$rules = [
'title' => 'required|min:5',
'slug' => 'unique:articles',
'slug' => 'unique:categories',
'description' => 'required|min:10'
];
$data['slug'] = Slug::fromTitle($data['title']);
Expand Down
53 changes: 53 additions & 0 deletions app/Models/Validators/DiscussCategoryValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace Xetaravel\Models\Validators;

use Eloquence\Behaviours\Slug;
use Illuminate\Support\Facades\Validator as FacadeValidator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

class DiscussCategoryValidator
{
/**
* Get a validator for an incoming create request.
*
* @param array $data The data to validate.
*
* @return \Illuminate\Validation\Validator
*/
public static function create(array $data): Validator
{
$rules = [
'title' => 'required|min:5',
'slug' => 'unique:discuss_categories',
'color' => 'min:7|max:7',
'description' => 'required|min:10'
];
$data['slug'] = Slug::fromTitle($data['title']);

return FacadeValidator::make($data, $rules);
}

/**
* Get a validator for an incoming update request.
*
* @param array $data The data to validate.
* @param int $id The actual category id to ignore the slug rule.
*
* @return \Illuminate\Validation\Validator
*/
public static function update(array $data, int $id): Validator
{
$rules = [
'title' => 'required|min:5',
'slug' => [
Rule::unique('discuss_categories')->ignore($id)
],
'color' => 'min:7|max:7',
'description' => 'required|min:10'
];
$data['slug'] = Slug::fromTitle($data['title']);

return FacadeValidator::make($data, $rules);
}
}
Loading

0 comments on commit 9e3074f

Please sign in to comment.