Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_PREFIX=apsky_laravel_database_

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
Expand Down
4 changes: 3 additions & 1 deletion app/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace App;

use App\Traits\CacheModelActions;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
use HasFactory;
use CacheModelActions;

protected $table = 'comments';

protected $guarded = [''];
public static $cacheTags = 'comments';

public function commentable()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ class PostCreated

public Post $post;

/**
* Create a new event instance.
*
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
Expand Down
File renamed without changes.
11 changes: 9 additions & 2 deletions app/Http/Controllers/AdministrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\News;
use App\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class AdministrationController extends Controller
{
Expand All @@ -18,12 +19,18 @@ public function index() {
}

public function posts() {
$posts = Post::with('tags')->latest()->get();
$posts = Cache::tags(['posts', 'tags', 'comments'])->remember('posts', 3600, function () {
return Post::with(['tags', 'comments'])->latest()->get();
});

return view('/admin.posts', compact('posts'));
}

public function news() {
$news = News::latest()->get();
$news = Cache::tags(['news', 'tags', 'comments'])->remember('news', 3600, function () {
return News::with(['tags', 'comments'])->latest()->get();
});

return view('/admin.news', compact('news'));
}

Expand Down
11 changes: 9 additions & 2 deletions app/Http/Controllers/FeedbacksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace App\Http\Controllers;

use App\Feedback;
use Illuminate\Filesystem\Cache;
use Illuminate\Http\Request;

class FeedbacksController extends Controller
{
public function __construct()
{
$this->middleware('role:admin');
$this->middleware('role:admin')->except('store');
}

public function index()
Expand All @@ -28,6 +29,12 @@ public function store(Request $request)

Feedback::create($request->all());

return redirect('/admin/feedbacks');
flash('Feedback successfully send (:', 'success');

if (auth()->user() && auth()->user()->hasRole('admin')) {
return redirect('/admin/feedbacks');
} else {
return back();
}
}
}
8 changes: 4 additions & 4 deletions app/Http/Controllers/NewsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace App\Http\Controllers;

use App\News;
use App\NewTag;
use App\Services\TagsCreatorService;
use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\Rule;

class NewsController extends Controller
Expand All @@ -27,7 +27,9 @@ public function validateRequest($request, $new)

public function index()
{
$news = News::with(['tags', 'comments'])->latest()->get();
$news = Cache::tags(['news', 'tags', 'comments'])->remember('news', 3600, function () {
return News::with(['tags', 'comments'])->latest()->get();
});

return view('news.index', compact('news'));
}
Expand Down Expand Up @@ -75,8 +77,6 @@ public function update(Request $request, News $new)
$updater = new TagsCreatorService($new, $request);
$updater->updateTags();

// updateTags($new, $request);

flash( 'New updated successfully');

return back();
Expand Down
7 changes: 5 additions & 2 deletions app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use App\Notifications\PostDeleted;
use App\Notifications\PostEdited;
use App\Post;
use App\PostTag;
use App\Services\TagsCreatorService;
use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Validation\Rule;

class PostsController extends Controller
Expand All @@ -31,7 +31,10 @@ public function validateRequest($request, $post)

public function index()
{
$posts = auth()->user()->posts()->with(['tags', 'comments'])->latest()->get();
$posts = Cache::tags(['posts', 'tags', 'comments', 'user_posts'])->remember('user_posts|' . auth()->id(), 3600, function () {
return auth()->user()->posts()->with(['tags', 'comments'])->latest()->get();
});

return view('/posts.index', compact('posts'));
}

Expand Down
71 changes: 37 additions & 34 deletions app/Http/Controllers/StaticPagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Services\StatisticService;
use Illuminate\Support\Facades\Cache;

class StaticPagesController extends Controller
{
Expand All @@ -22,40 +23,42 @@ public function aboutIndex() {
}

public function statisticsIndex() {
//Общее количество статей
$postsCount = $this->statisticService->getPostsCount();

//Общее количество новостей
$newsCount = $this->statisticService->getNewsCount();

//ФИО автора, у которого больше всего статей на сайте
$userWithMostPosts = $this->statisticService->getUserWithMaxPosts();

//Самая длинная статья - название, ссылка на статью и длина статьи в символах
$theLongestPost = $this->statisticService->getTheLongestPost();

//Самая короткая статья - название, ссылка на статью и длина статьи в символах
$theShortestPost = $this->statisticService->getTheShortestPost();

//Средние количество статей у “активных” пользователей, при этом активным пользователь считается, если у него есть более 1-й статьи
$avgPostsHaveActiveUsers = $this->statisticService->getAveragePosts();

//Самая непостоянная - название, ссылка на статью, которую меняли больше всего раз
$mostChangingPost = $this->statisticService->getMostChangingPost();

//Самая обсуждаемая статья - название, ссылка на статью, у которой больше всего комментариев.
$mostCommentPost = $this->statisticService->getMostCommentPost();

$statistics = [
'posts_count' => $postsCount,
'news_count' => $newsCount,
'user_with_most_posts' => $userWithMostPosts,
'the_longest_post' => $theLongestPost,
'the_shortest_post' => $theShortestPost,
'avg_posts_have_active_users' => $avgPostsHaveActiveUsers,
'most_changing_post' => $mostChangingPost,
'most_comment_post' => $mostCommentPost
];
$statistics = Cache::tags(['news', 'posts', 'tags', 'statistics'])->remember('statistics_data', 3600, function () {
//Общее количество статей
$postsCount = $this->statisticService->getPostsCount();

//Общее количество новостей
$newsCount = $this->statisticService->getNewsCount();

//ФИО автора, у которого больше всего статей на сайте
$userWithMostPosts = $this->statisticService->getUserWithMaxPosts();

//Самая длинная статья - название, ссылка на статью и длина статьи в символах
$theLongestPost = $this->statisticService->getTheLongestPost();

//Самая короткая статья - название, ссылка на статью и длина статьи в символах
$theShortestPost = $this->statisticService->getTheShortestPost();

//Средние количество статей у “активных” пользователей, при этом активным пользователь считается, если у него есть более 1-й статьи
$avgPostsHaveActiveUsers = $this->statisticService->getAveragePosts();

//Самая непостоянная - название, ссылка на статью, которую меняли больше всего раз
$mostChangingPost = $this->statisticService->getMostChangingPost();

//Самая обсуждаемая статья - название, ссылка на статью, у которой больше всего комментариев.
$mostCommentPost = $this->statisticService->getMostCommentPost();

return [
'posts_count' => $postsCount,
'news_count' => $newsCount,
'user_with_most_posts' => $userWithMostPosts,
'the_longest_post' => $theLongestPost,
'the_shortest_post' => $theShortestPost,
'avg_posts_have_active_users' => $avgPostsHaveActiveUsers,
'most_changing_post' => $mostChangingPost,
'most_comment_post' => $mostCommentPost
];
});

return view('static.statistics', ['statistics' => $statistics]);
}
Expand Down
11 changes: 9 additions & 2 deletions app/Http/Controllers/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class TagsController extends Controller
{
public function index(Tag $tag)
{
$posts = $tag->posts()->with('tags')->get();
$news = $tag->news()->with('tags')->get();
$posts = Cache::tags(['posts', 'posts_by_tag'])->remember('posts_by_tag', 3600, function () use ($tag) {
return $tag->posts()->with('tags')->get();
});

$news = Cache::tags(['news', 'news_by_tag'])->remember('news_by_tag', 3600, function () use ($tag) {
return $tag->news()->with('tags')->get();
});

return view('layouts.tags.index', compact('posts', 'news'));
}
}
6 changes: 4 additions & 2 deletions app/Listeners/AddHistoryOnUpdatePost.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public function handle(PostUpdated $event)
'text' => $result
];

$event->post->history()->create($values);
if (!empty($result)) {
$event->post->history()->create($values);

event(new PostUpdatedAdminChat($event->post, $result));
event(new PostUpdatedAdminChat($event->post, $result));
}
}
}
3 changes: 3 additions & 0 deletions app/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace App;

use App\Traits\CacheModelActions;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class News extends Model
{
use HasFactory;
use CacheModelActions;

protected $guarded = [];
public static $cacheTags = 'news';

public function tags()
{
Expand Down
7 changes: 6 additions & 1 deletion app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
namespace App;

use App\Events\PostUpdated;
use App\Mail\PostCreated;
use App\Traits\CacheModelActions;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasFactory;
use CacheModelActions;

protected $table = 'posts';

protected $guarded = [];

public static $cacheTags = 'posts';

protected $dispatchesEvents = [
'created' => PostCreated::class,
'updated' => PostUpdated::class,
];

Expand Down
6 changes: 5 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Services\StatisticService;
use App\Tag;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -12,11 +13,14 @@ class AppServiceProvider extends ServiceProvider
* Register any application services.
*
* @return void
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function register()
{
view()->composer('layouts.aside-tags', function ($view) {
$tags = Tag::whereHas('posts')->orWhereHas('news')->get();
$tags = Cache::tags(['posts', 'news', 'tags'])->remember('tags', 3600, function () {
return Tag::whereHas('posts')->orWhereHas('news')->get();
});

$view->with('tagsCloud', $tags);
});
Expand Down
5 changes: 0 additions & 5 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

namespace App\Providers;

use App\Events\PostCreated;
use App\Events\PostUpdated;
use App\Listeners\AddHistoryOnUpdatePost;
use App\Listeners\SendPostCreateNotification;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
Expand All @@ -35,7 +32,5 @@ class EventServiceProvider extends ServiceProvider
public function boot()
{
parent::boot();

//
}
}
Loading