Skip to content

Commit 33e5ce7

Browse files
committed
Added bookshelf filter to get books
1 parent f0e4cdb commit 33e5ce7

13 files changed

+978
-6
lines changed

src/Adapters/Database/MySql/Repositories/Library/BookRepository.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ public async Task<Page<BookModel>> GetBooks(int libraryId, int pageNumber, int p
185185
CategoryFilter = filter.CategoryId,
186186
FavoriteFilter = filter.Favorite,
187187
RecentFilter = filter.Read,
188-
StatusFilter = filter.Status
188+
StatusFilter = filter.Status,
189+
BookShelfId = filter.BookShelfId
189190
};
190191
var sql = @"SELECT b.Id, b.Title, b.seriesIndex, b.DateAdded, r.DateRead
191192
FROM Book b
@@ -197,6 +198,7 @@ FROM Book b
197198
LEFT JOIN Category c ON bc.CategoryId = c.Id
198199
LEFT JOIN FavoriteBooks fb On fb.BookId = b.Id AND fb.AccountId = @AccountId
199200
LEFT JOIN RecentBooks r On r.BookId = b.Id AND r.AccountId = @AccountId
201+
LEFT JOIN BookShelfBook bshf ON bshf.BookId = b.Id
200202
WHERE b.LibraryId = @LibraryId
201203
AND (@AccountId IS NOT NULL OR b.IsPublic = 1)
202204
AND (b.Status = @StatusFilter OR @StatusFilter = 0)
@@ -205,6 +207,7 @@ FROM Book b
205207
AND (bc.CategoryId = @CategoryFilter OR @CategoryFilter IS NULL)
206208
AND (f.AccountId = @AccountId OR @FavoriteFilter IS NULL)
207209
AND (r.AccountId = @AccountId OR @RecentFilter IS NULL)
210+
AND (bshf.BookShelfId = @BookShelfId OR @BookShelfId IS NULL)
208211
GROUP BY b.Id, b.Title, b.seriesIndex, b.DateAdded, r.DateRead " +
209212
$" ORDER BY {sortByQuery} {sortDirection} " +
210213
"LIMIT @PageSize OFFSET @Offset";
@@ -222,6 +225,7 @@ FROM Book b
222225
LEFT OUTER JOIN Category c ON bc.CategoryId = c.Id
223226
LEFT JOIN FavoriteBooks fb On fb.BookId = b.Id AND fb.AccountId = @AccountId
224227
LEFT JOIN RecentBooks r On r.BookId = b.Id AND r.AccountId = @AccountId
228+
LEFT JOIN BookShelfBook bshf ON bshf.BookId = b.Id
225229
WHERE b.LibraryId = @LibraryId
226230
AND (@AccountId IS NOT NULL OR b.IsPublic = 1)
227231
AND (b.Status = @StatusFilter OR @StatusFilter = 0)
@@ -230,6 +234,7 @@ FROM Book b
230234
AND (bc.CategoryId = @CategoryFilter OR @CategoryFilter IS NULL)
231235
AND (f.AccountId = @AccountId OR @FavoriteFilter IS NULL)
232236
AND (r.AccountId = @AccountId OR @RecentFilter IS NULL)
237+
AND (bshf.BookShelfId = @BookShelfId OR @BookShelfId IS NULL)
233238
GROUP BY b.Id) AS bkcnt";
234239
var bookCount = await connection.QuerySingleAsync<int>(new CommandDefinition(sqlCount, param, cancellationToken: cancellationToken));
235240

@@ -263,7 +268,8 @@ public async Task<Page<BookModel>> SearchBooks(int libraryId, string searchText,
263268
CategoryFilter = filter.CategoryId,
264269
FavoriteFilter = filter.Favorite,
265270
RecentFilter = filter.Read,
266-
StatusFilter = filter.Status
271+
StatusFilter = filter.Status,
272+
BookShelfId = filter.BookShelfId
267273
};
268274

269275
var sql = @"SELECT b.Id, b.Title, b.seriesIndex, b.DateAdded
@@ -276,6 +282,7 @@ FROM Book b
276282
LEFT JOIN Category c ON bc.CategoryId = c.Id
277283
LEFT JOIN FavoriteBooks fb On fb.BookId = b.Id
278284
LEFT JOIN RecentBooks r On b.Id = r.BookId
285+
LEFT JOIN BookShelfBook bshf ON bshf.BookId = b.Id
279286
WHERE b.LibraryId = @LibraryId
280287
AND b.Title Like @Query
281288
AND (@AccountId IS NOT NULL OR b.IsPublic = 1)
@@ -285,6 +292,7 @@ AND b.Title Like @Query
285292
AND (f.AccountId = @AccountId OR @FavoriteFilter IS NULL)
286293
AND (r.AccountId = @AccountId OR @RecentFilter IS NULL)
287294
AND (bc.CategoryId = @CategoryFilter OR @CategoryFilter IS NULL)
295+
AND (bshf.BookShelfId = @BookShelfId OR @BookShelfId IS NULL)
288296
GROUP BY b.Id, b.Title, b.seriesIndex, b.DateAdded " +
289297
$" ORDER BY {sortByQuery} {sortDirection} " +
290298
@"LIMIT @PageSize OFFSET @Offset";
@@ -303,6 +311,7 @@ FROM Book b
303311
LEFT OUTER JOIN Category c ON bc.CategoryId = c.Id
304312
LEFT OUTER JOIN FavoriteBooks fb On fb.BookId = b.Id
305313
LEFT OUTER JOIN RecentBooks r On b.Id = r.BookId
314+
LEFT JOIN BookShelfBook bshf ON bshf.BookId = b.Id
306315
WHERE b.LibraryId = @LibraryId
307316
AND b.Title Like @Query
308317
AND (@AccountId IS NOT NULL OR b.IsPublic = 1)
@@ -312,6 +321,7 @@ AND b.Title Like @Query
312321
AND (f.AccountId = @AccountId OR @FavoriteFilter IS NULL)
313322
AND (r.AccountId = @AccountId OR @RecentFilter IS NULL)
314323
AND (bc.CategoryId = @CategoryFilter OR @CategoryFilter IS NULL)
324+
AND (bshf.BookShelfId = @BookShelfId OR @BookShelfId IS NULL)
315325
GROUP BY b.Id) AS bkcnt";
316326

317327
var bookCount = await connection.QuerySingleAsync<int>(new CommandDefinition(sqlCount, param, cancellationToken: cancellationToken));

src/Adapters/Database/MySql/Repositories/Library/BookShelfRepository.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ FROM BookShelfBook
107107
WHERE BookShelfId = b.Id) AS BookCount
108108
FROM BookShelf AS b
109109
LEFT OUTER JOIN `File` f ON f.Id = b.ImageId
110-
WHERE b.LibraryId = @LibraryId
111-
AND b.IsPublic = @Public
110+
WHERE b.LibraryId = @LibraryId
112111
AND ((@Public = 1 AND b.AccountId <> @AccountId)
113112
OR (@Public = 0 AND b.AccountId = @AccountId))
114113
ORDER BY b.Name

src/Inshapardaz.Api/Controllers/LibraryController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public async Task<IActionResult> GetLibraryById(int libraryId, CancellationToken
6666
}
6767

6868
[HttpPost("libraries", Name = nameof(LibraryController.CreateLibrary))]
69-
//[Authorize(Role.Admin)]
7069
public async Task<IActionResult> CreateLibrary([FromBody] LibraryView library, CancellationToken token)
7170
{
7271
if (!ModelState.IsValid)

tests/Inshapardaz.Api.Tests/Framework/DataBuilders/BooksDataBuilder.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ private class AccountItemCountSpec
7171
public IFileTestRepository _fileRepository;
7272
public IChapterTestRepository _chapterRepository;
7373
public ICategoryTestRepository _categoryRepository;
74+
public IBookShelfTestRepository _bookShelfTestRepository;
75+
private int? _bookshelfId;
7476

7577
public BooksDataBuilder(IFileStorage fileStorage,
7678
AuthorsDataBuilder authorBuilder,
@@ -80,7 +82,8 @@ public BooksDataBuilder(IFileStorage fileStorage,
8082
IFileTestRepository fileRepository,
8183
IChapterTestRepository chapterRepository,
8284
IBookPageTestRepository bookPageRepository,
83-
ICategoryTestRepository categoryRepository)
85+
ICategoryTestRepository categoryRepository,
86+
IBookShelfTestRepository bookShelfTestRepository)
8487
{
8588
_fileStorage = fileStorage as FakeFileStorage;
8689
_authorBuilder = authorBuilder;
@@ -91,6 +94,7 @@ public BooksDataBuilder(IFileStorage fileStorage,
9194
_chapterRepository = chapterRepository;
9295
_bookPageRepository = bookPageRepository;
9396
_categoryRepository = categoryRepository;
97+
_bookShelfTestRepository = bookShelfTestRepository;
9498
}
9599

96100
public BooksDataBuilder HavingSeries()
@@ -164,6 +168,12 @@ public BooksDataBuilder WithSeries(SeriesDto series)
164168
_series = series;
165169
return this;
166170
}
171+
172+
public BooksDataBuilder InBookShelf(int bookshelfId)
173+
{
174+
_bookshelfId = bookshelfId;
175+
return this;
176+
}
167177

168178
public BooksDataBuilder WithContent()
169179
{
@@ -493,6 +503,10 @@ public IEnumerable<BookDto> Build(int numberOfBooks)
493503
}
494504
}
495505

506+
if (_bookshelfId.HasValue)
507+
{
508+
_bookShelfTestRepository.AddBooksToBookShelf(_bookshelfId.Value, _books.Select(x => x.Id));
509+
}
496510
return _books;
497511
}
498512

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using Inshapardaz.Api.Tests.Framework.Asserts;
6+
using Inshapardaz.Api.Tests.Framework.Dto;
7+
using Inshapardaz.Api.Views.Library;
8+
using Inshapardaz.Domain.Models;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using NUnit.Framework;
11+
12+
namespace Inshapardaz.Api.Tests.Library.Book.GetBooksByBookShelf
13+
{
14+
[TestFixture]
15+
public class WhenGettingBooksByBookShelfAsReader
16+
: TestBase
17+
18+
{
19+
private HttpResponseMessage _response;
20+
private PagingAssert<BookView> _assert;
21+
private IEnumerable<BookDto> _bookShelfBooks;
22+
private BookShelfDto _bookShelf;
23+
24+
public WhenGettingBooksByBookShelfAsReader() : base(Role.Reader)
25+
{
26+
}
27+
28+
[OneTimeSetUp]
29+
public async Task Setup()
30+
{
31+
_bookShelf = BookShelfBuilder.WithLibrary(LibraryId).ForAccount(AccountId).Build();
32+
_bookShelfBooks = BookBuilder.WithLibrary(LibraryId).InBookShelf(_bookShelf.Id).IsPublic().Build(5);
33+
CategoryBuilder.WithLibrary(LibraryId).WithBooks(3).Build();
34+
35+
_response = await Client.GetAsync($"/libraries/{LibraryId}/books?pageNumber=1&pageSize=10&bookShelfId={_bookShelf.Id}");
36+
37+
_assert = Services.GetService<PagingAssert<BookView>>().ForResponse(_response);
38+
}
39+
40+
[OneTimeTearDown]
41+
public void Teardown()
42+
{
43+
Cleanup();
44+
}
45+
46+
[Test]
47+
public void ShouldReturnOk()
48+
{
49+
_response.ShouldBeOk();
50+
}
51+
52+
[Test]
53+
public void ShouldHaveSelfLink()
54+
{
55+
_assert.ShouldHaveSelfLink($"/libraries/{LibraryId}/books");
56+
}
57+
58+
[Test]
59+
public void ShouldNotHaveCreateLink()
60+
{
61+
_assert.ShouldNotHaveCreateLink();
62+
}
63+
64+
[Test]
65+
public void ShouldNotHaveNextLink()
66+
{
67+
_assert.ShouldNotHaveNextLink();
68+
}
69+
70+
[Test]
71+
public void ShouldNotHavePreviousLink()
72+
{
73+
_assert.ShouldNotHavePreviousLink();
74+
}
75+
76+
[Test]
77+
public void ShouldReturnCorrectPage()
78+
{
79+
_assert.ShouldHavePage(1)
80+
.ShouldHavePageSize(10)
81+
.ShouldHaveTotalCount(_bookShelfBooks.Count())
82+
.ShouldHaveItems(5);
83+
}
84+
85+
[Test]
86+
public void ShouldReturnExpectedBooks()
87+
{
88+
var expectedItems = _bookShelfBooks.OrderBy(a => a.Title).Take(10).ToArray();
89+
for (int i = 0; i < _assert.Data.Count(); i++)
90+
{
91+
var actual = _assert.Data.ElementAt(i);
92+
var expected = expectedItems[i];
93+
Services.GetService<BookAssert>().ForView(actual).ForLibrary(LibraryId)
94+
.ShouldBeSameAs(expected)
95+
.ShouldHaveCorrectLinks()
96+
.ShouldNotHaveEditLinks()
97+
.ShouldNotHaveImageUpdateLink()
98+
.ShouldNotHaveCreateChaptersLink()
99+
.ShouldNotHaveAddContentLink()
100+
.ShouldHaveChaptersLink()
101+
.ShouldHavePublicImageLink();
102+
}
103+
}
104+
}
105+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using Inshapardaz.Api.Tests.Framework.Asserts;
6+
using Inshapardaz.Api.Tests.Framework.Dto;
7+
using Inshapardaz.Api.Views.Library;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using NUnit.Framework;
10+
11+
namespace Inshapardaz.Api.Tests.Library.Book.GetBooksByBookShelf
12+
{
13+
[TestFixture]
14+
public class WhenGettingBooksByBookShelfAsUnauthorized
15+
: TestBase
16+
17+
{
18+
private HttpResponseMessage _response;
19+
private PagingAssert<BookView> _assert;
20+
private BookShelfDto _bookShelf;
21+
private IEnumerable<BookDto> _bookShelfBooks;
22+
23+
[OneTimeSetUp]
24+
public async Task Setup()
25+
{
26+
var account = AccountBuilder.Build();
27+
_bookShelf = BookShelfBuilder.WithLibrary(LibraryId).ForAccount(account.Id).Build();
28+
_bookShelfBooks = BookBuilder.WithLibrary(LibraryId).InBookShelf(_bookShelf.Id).IsPublic().Build(5);
29+
30+
_response = await Client.GetAsync($"/libraries/{LibraryId}/books?pageNumber=1&pageSize=10&bookShelfId={_bookShelf.Id}");
31+
32+
_assert = Services.GetService<PagingAssert<BookView>>().ForResponse(_response);
33+
}
34+
35+
[OneTimeTearDown]
36+
public void Teardown()
37+
{
38+
Cleanup();
39+
}
40+
41+
[Test]
42+
public void ShouldReturnOk()
43+
{
44+
_response.ShouldBeOk();
45+
}
46+
47+
[Test]
48+
public void ShouldHaveSelfLink()
49+
{
50+
_assert.ShouldHaveSelfLink($"/libraries/{LibraryId}/books");
51+
}
52+
53+
[Test]
54+
public void ShouldNotHaveNextLink()
55+
{
56+
_assert.ShouldNotHaveNextLink();
57+
}
58+
59+
[Test]
60+
public void ShouldNotHavePreviousLink()
61+
{
62+
_assert.ShouldNotHavePreviousLink();
63+
}
64+
65+
[Test]
66+
public void ShouldNotHaveCreateLink()
67+
{
68+
_assert.ShouldNotHaveCreateLink();
69+
}
70+
71+
[Test]
72+
public void ShouldReturnCorrectPage()
73+
{
74+
_assert.ShouldHavePage(1)
75+
.ShouldHavePageSize(10)
76+
.ShouldHaveTotalCount(_bookShelfBooks.Count())
77+
.ShouldHaveItems(5);
78+
}
79+
80+
[Test]
81+
public void ShouldReturnExpectedBooks()
82+
{
83+
var expectedItems = _bookShelfBooks.OrderBy(a => a.Title).Take(10).ToArray();
84+
for (int i = 0; i < _assert.Data.Count(); i++)
85+
{
86+
var actual = _assert.Data.ElementAt(i);
87+
var expected = expectedItems[i];
88+
Services.GetService<BookAssert>().ForView(actual).ForLibrary(LibraryId)
89+
.ShouldBeSameAs(expected)
90+
.ShouldHaveCorrectLinks()
91+
.ShouldNotHaveEditLinks()
92+
.ShouldNotHaveImageUpdateLink()
93+
.ShouldNotHaveCreateChaptersLink()
94+
.ShouldNotHaveAddContentLink()
95+
.ShouldHaveChaptersLink()
96+
.ShouldHavePublicImageLink();
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)