Skip to content

Commit 7580fa3

Browse files
committed
correct StatisticService and its methods
1 parent 7b0bb7c commit 7580fa3

File tree

4 files changed

+155
-126
lines changed

4 files changed

+155
-126
lines changed

app/Http/Controllers/StaticPagesController.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5-
use App\Services\GetStatisticService;
5+
use App\Services\StatisticService;
66

77
class StaticPagesController extends Controller
88
{
@@ -16,28 +16,30 @@ public function aboutIndex() {
1616

1717
public function statisticsIndex() {
1818
//Общее количество статей
19-
$postsCount = GetStatisticService::getPostsCount();
19+
$statisticService = new StatisticService();
20+
21+
$postsCount = $statisticService->getPostsCount();
2022

2123
//Общее количество новостей
22-
$newsCount = GetStatisticService::getNewsCount();
24+
$newsCount = $statisticService->getNewsCount();
2325

2426
//ФИО автора, у которого больше всего статей на сайте
25-
$usersWithMostPosts = GetStatisticService::getUserWithMaxPosts();
27+
$usersWithMostPosts = $statisticService->getUserWithMaxPosts();
2628

2729
//Самая длинная статья - название, ссылка на статью и длина статьи в символах
28-
$theLongestPost = GetStatisticService::getTheLongestPosts();
30+
$theLongestPost = $statisticService->getTheLongestPosts();
2931

3032
//Самая короткая статья - название, ссылка на статью и длина статьи в символах
31-
$theShortestPost = GetStatisticService::getTheShortestPosts();
33+
$theShortestPost = $statisticService->getTheShortestPosts();
3234

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

3638
//Самая непостоянная - название, ссылка на статью, которую меняли больше всего раз
37-
$mostChangingPosts = GetStatisticService::getMostChangingPosts();
39+
$mostChangingPosts = $statisticService->getMostChangingPosts();
3840

3941
//Самая обсуждаемая статья - название, ссылка на статью, у которой больше всего комментариев.
40-
$mostCommentPosts = GetStatisticService::getMostCommentPosts();
42+
$mostCommentPosts = $statisticService->getMostCommentPosts();
4143

4244
$statistics = [
4345
'posts_count' => $postsCount,

app/Services/GetStatisticService.php

Lines changed: 0 additions & 116 deletions
This file was deleted.

app/Services/StatisticService.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\News;
6+
use App\Post;
7+
use App\User;
8+
use Illuminate\Database\Eloquent\Collection;
9+
use Illuminate\Support\Facades\DB;
10+
11+
class StatisticService
12+
{
13+
/**
14+
* Количество постов на сайте
15+
* @return int
16+
*/
17+
public function getPostsCount()
18+
{
19+
return Post::count();
20+
}
21+
22+
/**
23+
* Общее количество новостей
24+
* @return int
25+
*/
26+
public function getNewsCount()
27+
{
28+
return News::count();
29+
}
30+
31+
32+
/**
33+
* @return Collection
34+
*/
35+
36+
public function getUserWithMaxPosts()
37+
{
38+
// $users = User::has('posts')->withCount('posts')->get();
39+
// $usersWithMostPostsCount = $users->where('posts_count', $users->max('posts_count'));
40+
41+
$users = DB::table('users')
42+
->join('posts', 'users.id', '=', 'owner_id')
43+
->select('users.name', DB::raw('Count(*) as posts_count'))
44+
->groupBy('users.name')
45+
->orderByDesc('posts_count')
46+
->first();
47+
48+
$usersWithMostPostsCount = User::where('name', $users->name)->withCount('posts')->get();
49+
50+
return $usersWithMostPostsCount;
51+
}
52+
53+
54+
/**
55+
* @return Collection
56+
*/
57+
58+
public function getTheLongestPosts()
59+
{
60+
$theLongestPosts = Post::where(DB::raw('Length(text)'), function($query){
61+
$query->select(DB::raw('MAX(Length(text))'))
62+
->from(DB::table('posts'));
63+
})->get();
64+
65+
return $theLongestPosts;
66+
}
67+
68+
/**
69+
* @return Collection
70+
*/
71+
72+
public function getTheShortestPosts()
73+
{
74+
$theShortestPosts = Post::where(DB::raw('Length(text)'), function($query){
75+
$query->select(DB::raw('MIN(Length(text))'))
76+
->from(DB::table('posts'));
77+
})->get();
78+
79+
return $theShortestPosts;
80+
}
81+
82+
/**
83+
* @return int
84+
*/
85+
86+
public function getAveragePosts()
87+
{
88+
// $posts = User::has('posts', '>', 1)->withCount('posts')->get();
89+
// $averagePosts = intval(round($posts->avg('posts_count')));
90+
91+
$averagePosts = DB::select('select AVG(u.posts_count) as avg_posts_count from (select users.*, (select Count(*) from posts where users.id = posts.owner_id) as posts_count from users) as u')[0];
92+
93+
$averagePosts = intval(round($averagePosts->avg_posts_count));
94+
95+
return $averagePosts;
96+
}
97+
98+
/**
99+
* @return Collection
100+
*/
101+
public function getMostChangingPosts()
102+
{
103+
// $posts = Post::has('history', '>=', 1)->withCount('history')->get();
104+
// $maxChangingPosts = $posts->where('history_count', $posts->max('history_count'));
105+
106+
$maxHistoryCount = DB::table('posts')
107+
->join('histories as h', 'posts.id', '=', 'h.post_id')
108+
->select('posts.name', DB::raw('Count(*) as history_count'))
109+
->groupBy('posts.name')
110+
->orderByDesc('history_count')
111+
->first()
112+
;
113+
114+
$maxChangingPosts = Post::where('name', $maxHistoryCount->name)->withCount('history')->get();
115+
116+
return $maxChangingPosts;
117+
}
118+
119+
/**
120+
* @return Collection
121+
*/
122+
123+
public function getMostCommentPosts()
124+
{
125+
// $posts = Post::has('comments', '>=', 1)->withCount('comments')->get();
126+
// $mostCommentPosts = $posts->where('comments_count', $posts->max('comments_count'));
127+
128+
$maxComment = DB::table('posts')
129+
->join('comments as c', function ($join) {
130+
$join->on('posts.id', '=', 'c.commentable_id')
131+
->where('c.commentable_type', '=', 'App\Post');
132+
})
133+
->select('posts.name', DB::raw('Count(*) as comments_count'))
134+
->groupBy('posts.name')
135+
->orderByDesc('comments_count')
136+
->first()
137+
;
138+
139+
$mostCommentPosts = Post::where('name', $maxComment->name)->withCount('comments')->get();
140+
141+
return $mostCommentPosts;
142+
}
143+
}

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
Route::get('/test', function () {
99

10-
});
10+
});
1111

1212
Route::get('/', function () {
1313
$posts = Post::with('tags')->where('published', 1)->latest()->take(4)->get();

0 commit comments

Comments
 (0)