Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Зуев Егор
/
wiki.dev
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Authored by
Dan Brown
2015-12-02 20:22:41 +0000
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
f1c2866fbc24ec6e951071c4886cfa8beee62e29
f1c2866f
1 parent
9f435553
Added Popular books list with relevant tests
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
117 additions
and
8 deletions
app/Http/Controllers/BookController.php
app/Repos/BookRepo.php
app/Services/ViewService.php
resources/views/books/index.blade.php
resources/views/partials/entity-list.blade.php
tests/ActivityTrackingTest.php
tests/TestCase.php
app/Http/Controllers/BookController.php
View file @
f1c2866
...
...
@@ -42,8 +42,9 @@ class BookController extends Controller
public
function
index
()
{
$books
=
$this
->
bookRepo
->
getAllPaginated
(
10
);
$recents
=
$this
->
signedIn
?
$this
->
bookRepo
->
getRecentlyViewed
(
10
,
0
)
:
false
;
return
view
(
'books/index'
,
[
'books'
=>
$books
,
'recents'
=>
$recents
]);
$recents
=
$this
->
signedIn
?
$this
->
bookRepo
->
getRecentlyViewed
(
4
,
0
)
:
false
;
$popular
=
$this
->
bookRepo
->
getPopular
(
4
,
0
);
return
view
(
'books/index'
,
[
'books'
=>
$books
,
'recents'
=>
$recents
,
'popular'
=>
$popular
]);
}
/**
...
...
app/Repos/BookRepo.php
View file @
f1c2866
...
...
@@ -77,6 +77,11 @@ class BookRepo
return
Views
::
getUserRecentlyViewed
(
$count
,
$page
,
$this
->
book
);
}
public
function
getPopular
(
$count
=
10
,
$page
=
0
)
{
return
Views
::
getPopular
(
$count
,
$page
,
$this
->
book
);
}
/**
* Get a book by slug
* @param $slug
...
...
app/Services/ViewService.php
View file @
f1c2866
...
...
@@ -44,6 +44,29 @@ class ViewService
return
1
;
}
/**
* Get the entities with the most views.
* @param int $count
* @param int $page
* @param bool|false $filterModel
*/
public
function
getPopular
(
$count
=
10
,
$page
=
0
,
$filterModel
=
false
)
{
$skipCount
=
$count
*
$page
;
$query
=
$this
->
view
->
select
(
'id'
,
'viewable_id'
,
'viewable_type'
,
\DB
::
raw
(
'SUM(views) as view_count'
))
->
groupBy
(
'viewable_id'
,
'viewable_type'
)
->
orderBy
(
'view_count'
,
'desc'
);
if
(
$filterModel
)
$query
->
where
(
'viewable_type'
,
'='
,
get_class
(
$filterModel
));
$views
=
$query
->
with
(
'viewable'
)
->
skip
(
$skipCount
)
->
take
(
$count
)
->
get
();
$viewedEntities
=
$views
->
map
(
function
(
$item
)
{
return
$item
->
viewable
()
->
getResults
();
});
return
$viewedEntities
;
}
/**
* Get all recently viewed entities for the current user.
* @param int $count
...
...
resources/views/books/index.blade.php
View file @
f1c2866
...
...
@@ -34,11 +34,22 @@
@endif
</div>
<div
class=
"col-sm-4 col-sm-offset-1"
>
<div
id=
"recents"
>
@if($recents)
<div
class=
"margin-top large"
>
</div>
<h3>
Recently Viewed
</h3>
@include('partials/entity-list', ['entities' => $recents])
@endif
</div>
<div
class=
"margin-top large"
>
</div>
@if($recents)
<h3>
Recently Viewed
</h3>
@include('partials/entity-list', ['entities' => $recents])
@endif
<div
id=
"popular"
>
<h3>
Popular Books
</h3>
@if(count($popular) > 0)
@include('partials/entity-list', ['entities' => $popular])
@else
<p
class=
"text-muted"
>
The most popular books will appear here.
</p>
@endif
</div>
</div>
</div>
</div>
...
...
resources/views/partials/entity-list.blade.php
View file @
f1c2866
@if(count($entities) > 0)
@foreach($entities as $entity)
@foreach($entities as $
index => $
entity)
@if($entity->isA('page'))
@include('pages/list-item', ['page' => $entity])
@elseif($entity->isA('book'))
...
...
@@ -8,7 +8,11 @@
@elseif($entity->isA('chapter'))
@include('chapters/list-item', ['chapter' => $entity, 'hidePages' => true])
@endif
<hr>
@if($index !== count($entities) - 1)
<hr>
@endif
@endforeach
@else
<p
class=
"text-muted"
>
...
...
tests/ActivityTrackingTest.php
0 → 100644
View file @
f1c2866
<?php
use
Illuminate\Foundation\Testing\WithoutMiddleware
;
use
Illuminate\Foundation\Testing\DatabaseMigrations
;
use
Illuminate\Foundation\Testing\DatabaseTransactions
;
class
ActivityTrackingTest
extends
TestCase
{
public
function
testRecentlyViewedBooks
()
{
$books
=
\BookStack\Book
::
all
()
->
take
(
10
);
$this
->
asAdmin
()
->
visit
(
'/books'
)
->
dontSeeInElement
(
'#recents'
,
$books
[
0
]
->
name
)
->
dontSeeInElement
(
'#recents'
,
$books
[
1
]
->
name
)
->
visit
(
$books
[
0
]
->
getUrl
())
->
visit
(
$books
[
1
]
->
getUrl
())
->
visit
(
'/books'
)
->
seeInElement
(
'#recents'
,
$books
[
0
]
->
name
)
->
seeInElement
(
'#recents'
,
$books
[
1
]
->
name
);
}
public
function
testPopularBooks
()
{
$books
=
\BookStack\Book
::
all
()
->
take
(
10
);
$this
->
asAdmin
()
->
visit
(
'/books'
)
->
dontSeeInElement
(
'#popular'
,
$books
[
0
]
->
name
)
->
dontSeeInElement
(
'#popular'
,
$books
[
1
]
->
name
)
->
visit
(
$books
[
0
]
->
getUrl
())
->
visit
(
$books
[
1
]
->
getUrl
())
->
visit
(
$books
[
0
]
->
getUrl
())
->
visit
(
'/books'
)
->
seeInNthElement
(
'#popular .book'
,
0
,
$books
[
0
]
->
name
)
->
seeInNthElement
(
'#popular .book'
,
1
,
$books
[
1
]
->
name
);
}
}
tests/TestCase.php
View file @
f1c2866
...
...
@@ -48,4 +48,31 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
$settings
->
put
(
$key
,
$value
);
}
}
/**
* Assert that a given string is seen inside an element.
*
* @param bool|string|null $element
* @param integer $position
* @param string $text
* @param bool $negate
* @return $this
*/
protected
function
seeInNthElement
(
$element
,
$position
,
$text
,
$negate
=
false
)
{
$method
=
$negate
?
'assertNotRegExp'
:
'assertRegExp'
;
$rawPattern
=
preg_quote
(
$text
,
'/'
);
$escapedPattern
=
preg_quote
(
e
(
$text
),
'/'
);
$content
=
$this
->
crawler
->
filter
(
$element
)
->
eq
(
$position
)
->
html
();
$pattern
=
$rawPattern
==
$escapedPattern
?
$rawPattern
:
"(
{
$rawPattern
}
|
{
$escapedPattern
}
)"
;
$this
->
$method
(
"/
$pattern
/i"
,
$content
);
return
$this
;
}
}
...
...
Please
register
or
sign in
to post a comment