Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rating): add rating #58

Merged
merged 2 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions app/Http/Controllers/RatingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers;

use App\User;

class RatingController extends Controller
{
public function index()
{
$users = User::withCount('readChapters')
->orderBy('read_chapters_count', 'DESC')
->get();

if (request()->wantsJson()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api никогда не надо смешивать с обычным html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрал

return $users;
}

return view('rating.index', [
'users' => $users,
]);
}
}
1 change: 0 additions & 1 deletion database/factories/ReadChapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
$factory->define(ReadChapter::class, function (Faker $faker) {
return [
'chapter_id' => factory(Chapter::class)->create()->id,
'user_id' => factory(User::class)->create()->id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот это непонятно. У readChapter должен быть пользователь.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вернул

];
});
5 changes: 5 additions & 0 deletions resources/lang/en/layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'rating_page' => 'Rating',
];
8 changes: 8 additions & 0 deletions resources/lang/en/rating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'page_title' => 'Users rating',
'positions' => 'Positions',
'user' => 'User',
'number_of_points' => 'Number of points'
];
5 changes: 5 additions & 0 deletions resources/lang/ru/layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'rating_page' => 'Рейтинг',
];
8 changes: 8 additions & 0 deletions resources/lang/ru/rating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'page_title' => 'Рейтинг пользователей',
'positions' => 'Позиция',
'user' => 'Пользователь',
'number_of_points' => 'Количество очков'
];
7 changes: 6 additions & 1 deletion resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<!-- Left Side Of Navbar -->
<ul class="navbar-nav mr-auto">
<a href="https://github.com/Hexlet/hexlet-sicp">Source Code</a>
<li class="nav-item">
<a class="nav-link" href="https://github.com/Hexlet/hexlet-sicp">{{ __('layout.rating_page') }}</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://github.com/Hexlet/hexlet-sicp">Source Code</a>
</li>
</ul>

<!-- Right Side Of Navbar -->
Expand Down
30 changes: 30 additions & 0 deletions resources/views/rating/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@extends('layouts.app')

@section('content')
<div class="row my-4">
<div class="col-12 my-4">
<h3>{{ __('rating.page_title') }}</h3>

<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>{{ __('rating.positions') }}</th>
<th>{{ __('rating.user') }}</th>
<th>{{ __('rating.number_of_points') }}</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->read_chapters_count }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@endsection
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ static function () {
Route::resource('users.chapters', 'UserChapterController')->only('store');
});

Route::resource('ratings', 'RatingController')->only('index');

Route::get('/home', 'HomeController@index')->name('home');
32 changes: 32 additions & 0 deletions tests/Feature/Http/Controllers/RatingControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Feature\Http\Controllers;

use App\ReadChapter;
use App\User;
use Tests\TestCase;

class RatingControllerTest extends TestCase
{
public function testIndex()
{
$userWithThree = factory(User::class)->create();
factory(ReadChapter::class, 3)->create([
'user_id' => $userWithThree->id,
]);

factory(User::class)->create();

$userWithTwo = factory(User::class)->create();
factory(ReadChapter::class, 2)->create([
'user_id' => $userWithTwo->id,
]);

$response = $this->getJson(route('ratings.index'))
->assertStatus(200);

$responseData = $response->json();

$this->assertEquals([3, 2, 0], array_column($responseData, 'read_chapters_count'));
}
}