Skip to content

Commit

Permalink
Поправлена выборка на BookService
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Andrew committed Dec 21, 2020
1 parent d846fca commit 3f7f02b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 39 deletions.
21 changes: 17 additions & 4 deletions BookMarket/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,24 @@ public async Task<IActionResult> newBooks()
[HttpGet]
public async Task<IActionResult> topBooks()
{
var topBooks = await context.Book
.Select(i => new IndexBook { RatingBook = i.UserRating.Count != 0 ? i.UserRating.Average(i => i.Mark) : 0, IdAuthor = (int)i.IdAuthor, Id = i.Id, Name = i.Name, PosterBook = i.PosterBook, AuthorNameFamily = i.IdAuthorNavigation.NameFamily })
.OrderByDescending(i => i.RatingBook).Take(4).ToListAsync();

return PartialView("topBooksData", topBooks);
// Получаем айдишники топовых книг
var ids = context.Ratings
.GroupBy(u => u.IdBook)
.Select(g => new
{
g.Key,
MarkAverage = g.Average(s => s.Mark)
})
.OrderByDescending(i => i.MarkAverage).Take(4).Select(g => g.Key).ToList();

var books = context.Book.Where(p => ids.Contains(p.Id))
.Select(i => new IndexBook { RatingBook = i.UserRating.Average(i => i.Mark), IdAuthor = (int)i.IdAuthor, Id = i.Id, Name = i.Name, PosterBook = i.PosterBook, AuthorNameFamily = i.IdAuthorNavigation.NameFamily })
.ToList();


return PartialView("topBooksData", books);

}


Expand Down
2 changes: 1 addition & 1 deletion BookMarket/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53887",
"sslPort": 44305
"sslPort": 0
}
},
"profiles": {
Expand Down
61 changes: 40 additions & 21 deletions BookMarket/Services/Books/BookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,51 @@ public async Task<List<FavoriteBook>> GetFavoritesBooks(string userName)
/// </summary>
/// <param name="CountCommentary">Количество комментариев</param>
/// <returns>Выборка последних комментариев</returns>
public async Task<IEnumerable<KeyValuePair<BookViewModel, Rating>>> GetLastCommentaries(int CountCommentary)
public async Task <IEnumerable<BookViewModel>> GetLastCommentaries(int CountCommentary)
{
var lastComments = (await db.Book
.Where(i => i.UserRating.Count() != 0)
/* var lastComments = (await db.Book
.Where(i => i.UserRating.Count() != 0)
.Include("UserRating")
.Include("IdAuthorNavigation")
.Include("IdCategoryNavigation")
.ToDictionaryAsync(i =>
new BookViewModel
{
Id = i.Id,
Name = i.Name,
AuthorNameFamily = i.IdAuthorNavigation.NameFamily,
CategoryName = i.IdCategoryNavigation.Name,
IdAuthor = (int)i.IdAuthor,
PosterBook = i.PosterBook,
RatingBook = i.UserRating.Count != 0 ? i.UserRating.Average(i => i.Mark) : 0
},
s => s.UserRating.OrderByDescending(i => i.Id).FirstOrDefault()))
.OrderByDescending(i => i.Value.DateCreated)
.Take(4);*/


// Получаем номера книг последних комментируемых
var ids = await db.Ratings.OrderByDescending(i => i.DateCreated).Select(i => i.IdBook).Take(CountCommentary).ToListAsync();

var books = await db.Book
.Include("UserRating")
.Include("IdAuthorNavigation")
.Include("IdCategoryNavigation")
.ToDictionaryAsync(i =>
new BookViewModel
{
Id = i.Id,
Name = i.Name,
AuthorNameFamily = i.IdAuthorNavigation.NameFamily,
CategoryName = i.IdCategoryNavigation.Name,
IdAuthor = (int)i.IdAuthor,
PosterBook = i.PosterBook,
RatingBook = i.UserRating.Count != 0 ? i.UserRating.Average(i => i.Mark) : 0
},
s => s.UserRating.OrderByDescending(i => i.Id).FirstOrDefault()))
.OrderByDescending(i => i.Value.DateCreated)
.Take(4);



.Where(i => ids.Contains(i.Id))
.Select(i =>
new BookViewModel
{
Id = i.Id,
Name = i.Name,
AuthorNameFamily = i.IdAuthorNavigation.NameFamily,
CategoryName = i.IdCategoryNavigation.Name,
IdAuthor = (int)i.IdAuthor,
PosterBook = i.PosterBook,
RatingBook = i.UserRating.Count != 0 ? i.UserRating.Average(i => i.Mark) : 0
})
.ToListAsync();

return lastComments;
return books;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion BookMarket/Services/Books/IBookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IBookService
/// </summary>
/// <param name="CountCommentary">Количество комментариев</param>
/// <returns>Выборка последних комментариев</returns>
public Task<IEnumerable<KeyValuePair<BookViewModel, Rating>>> GetLastCommentaries(int CountCommentary);
public Task<IEnumerable<BookViewModel>> GetLastCommentaries(int CountCommentary);

/// <summary>
/// Выборка последних комментариев юзера
Expand Down
18 changes: 9 additions & 9 deletions BookMarket/Views/Home/newComments.cshtml
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
@model IEnumerable<KeyValuePair<BookMarket.Models.ViewModels.SearchBook.BookViewModel, BookMarket.Models.DataBase.Rating>>
@model IEnumerable<BookMarket.Models.ViewModels.SearchBook.BookViewModel>


@foreach (var item in Model)
{
<div class="bookData col-xs-12 col-sm-6 col-md-3 col-centered col-min">
<div class="bookData__image">
<a asp-action="AboutBook" asp-controller="Books" asp-route-id="@item.Key.Id">
<img src="data:image;base64, @System.Convert.ToBase64String(item.Key.PosterBook)" />
<a asp-action="AboutBook" asp-controller="Books" asp-route-id="@item.Id">
<img src="data:image;base64, @System.Convert.ToBase64String(item.PosterBook)" />
</a>
</div>
<div class="bookData__name">
<a asp-action="AboutBook" asp-controller="Books" asp-route-id="@item.Key.Id" asp-route-name="@item.Key.Name">
<h3>@item.Key.Name</h3>
<a asp-action="AboutBook" asp-controller="Books" asp-route-id="@item.Id" asp-route-name="@item.Name">
<h3>@item.Name</h3>
</a>
</div>
<div class="bookData__rating">
@if (item.Key.RatingBook != 0)
@if (item.RatingBook != 0)
{
<h6>Рейтинг @item.Key.RatingBook.ToString("0.00") <span>★</span></h6>
<h6>Рейтинг @item.RatingBook.ToString("0.00") <span>★</span></h6>
}
else
{
<h6>Нет рейтинга</h6>
}
</div>
<div class="bookData_author">
<a asp-action="Details" asp-controller="Authors" asp-route-id="@item.Key.IdAuthor" asp-route-name="@item.Key.AuthorNameFamily">
<h4>@item.Key.AuthorNameFamily</h4>
<a asp-action="Details" asp-controller="Authors" asp-route-id="@item.IdAuthor" asp-route-name="@item.AuthorNameFamily">
<h4>@item.AuthorNameFamily</h4>
</a>
</div>
</div>
Expand Down
8 changes: 5 additions & 3 deletions BookMarket/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>market-books - @ViewData["Title"]</title>
<title>Электронная бесплатная онлайн библиотека - @ViewData["Title"]</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
<link href="~/css/mainpage.css" rel="stylesheet" />
<link href="~/css/test.css" rel="stylesheet" />
<link rel="canonical" href="https://market-books.ru" />
<meta name="author" content="Андрей Радюкевич">
<meta name="robots" content="index, follow"/>
<meta name="copyright" content="Все права принадлежат Андрею Радюкевичу">
<meta name="keywords" content="библиотека, электронная библиотека, книги онлайн, книги, читать, онлайн библиотека, book market">
<meta name="description" content="Электронная онлайн библиотека book-market.ru">
<meta name="keywords" content="читать книги онлайн без регистрации, читать книги про бизнес, читать любовные романы, бесплатная электронная библиотека">
<meta name="description" content="Электронная бесплатная онлайн библиотека | читать бесплатно книги">
@RenderSection("Css", required: false)
<!-- Yandex.Metrika counter -->
<script type="text/javascript">
Expand Down

0 comments on commit 3f7f02b

Please sign in to comment.