Skip to content

Commit 0d04f75

Browse files
committed
start edit statistics service
1 parent 174b404 commit 0d04f75

File tree

3 files changed

+87
-21
lines changed

3 files changed

+87
-21
lines changed

app/Http/Controllers/StaticPagesController.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,40 @@ public function aboutIndex() {
1717

1818
public function statisticsIndex() {
1919
//Общее количество статей
20-
$postsCount = Post::all()->count();
20+
// $postsCount = Post::all()->count();
2121

2222
//Общее количество новостей
23-
$newsCount = News::all()->count();
23+
// $newsCount = News::all()->count();
2424

2525
//ФИО автора, у которого больше всего статей на сайте
26-
$usersWithMostPosts = getUserWithMaxPosts();
26+
// $usersWithMostPosts = getUserWithMaxPosts();
2727

2828
//Самая длинная статья - название, ссылка на статью и длина статьи в символах
29-
$theLongestPost = getTheLongestPosts();
29+
// $theLongestPost = getTheLongestPosts();
3030

3131
//Самая короткая статья - название, ссылка на статью и длина статьи в символах
32-
$theShortestPost = getTheShortestPosts();
32+
// $theShortestPost = getTheShortestPosts();
3333

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

3737
//Самая непостоянная - название, ссылка на статью, которую меняли больше всего раз
38-
$mostChangingPosts = getMostChangingPosts();
38+
// $mostChangingPosts = getMostChangingPosts();
3939

4040
//Самая обсуждаемая статья - название, ссылка на статью, у которой больше всего комментариев.
41-
$mostCommentPosts = getMostCommentPosts();
42-
43-
$statistics = [
44-
'posts_count' => $postsCount,
45-
'news_count' => $newsCount,
46-
'users_with_most_posts' => $usersWithMostPosts,
47-
'the_longest_posts' => $theLongestPost,
48-
'the_shortest_posts' => $theShortestPost,
49-
'avg_posts_have_active_users' => $avgPostsHaveActiveUsers,
50-
'most_changing_posts' => $mostChangingPosts,
51-
'most_comment_posts' => $mostCommentPosts
52-
];
53-
54-
return view('static.statistics', ['statistics' => $statistics]);
41+
// $mostCommentPosts = getMostCommentPosts();
42+
43+
// $statistics = [
44+
// 'posts_count' => $postsCount,
45+
// 'news_count' => $newsCount,
46+
// 'users_with_most_posts' => $usersWithMostPosts,
47+
// 'the_longest_posts' => $theLongestPost,
48+
// 'the_shortest_posts' => $theShortestPost,
49+
// 'avg_posts_have_active_users' => $avgPostsHaveActiveUsers,
50+
// 'most_changing_posts' => $mostChangingPosts,
51+
// 'most_comment_posts' => $mostCommentPosts
52+
// ];
53+
54+
// return view('static.statistics', ['statistics' => $statistics]);
5555
}
5656
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use App\News;
6+
use App\Post;
7+
use App\User;
8+
use Illuminate\Support\Facades\App;
9+
use Illuminate\Support\Facades\DB;
10+
11+
class GetStatisticService
12+
{
13+
/**
14+
* Количество постов на сайте
15+
* @return int
16+
*/
17+
static public function getPostsCount()
18+
{
19+
return Post::all()->count();
20+
}
21+
22+
/**
23+
* Общее количество новостей
24+
* @return int
25+
*/
26+
static public function getNewsCount()
27+
{
28+
return News::all()->count();
29+
}
30+
31+
32+
/**
33+
* Пользователь с наибольшим количеством постов
34+
* @return User
35+
*/
36+
static public function getUserWithMaxPosts()
37+
{
38+
$maxPosts = DB::table('posts')
39+
->select(DB::raw('count(*) as post_count, owner_id'))
40+
->groupBy('owner_id')
41+
->orderBy('post_count', 'desc')
42+
->take(1)
43+
->get();
44+
45+
$usersWithMaxPostsCount = \App\User::find($maxPosts[0]->owner_id);
46+
47+
return $usersWithMaxPostsCount;
48+
}
49+
50+
static public function getTheLongestPosts()
51+
{
52+
53+
54+
return;
55+
}
56+
57+
58+
59+
}

routes/web.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
use App\News;
44
use App\Post;
55
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Support\Facades\DB;
67
use Illuminate\Support\Facades\Route;
78

89
Route::get('/test', function () {
10+
$posts = DB::table('posts')
11+
->select(DB::raw('MIN(text) as max_text'))
12+
->get();
913

14+
$posts2 = Post::min('text');
15+
16+
dd($posts, $posts2);
1017
});
1118

1219
Route::get('/', function () {

0 commit comments

Comments
 (0)