Commit e7955f22 authored by Nikita Stavitskiy's avatar Nikita Stavitskiy

сделяль

parent 1f6d26a0
......@@ -10,14 +10,11 @@
</component>
<component name="ChangeListManager">
<list default="true" id="98968c1b-50bf-419a-b85b-aea52b53c206" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/WebApplication10.sln" afterDir="false" />
<change afterPath="$PROJECT_DIR$/WebApplication10/Program.cs" afterDir="false" />
<change afterPath="$PROJECT_DIR$/WebApplication10/Properties/launchSettings.json" afterDir="false" />
<change afterPath="$PROJECT_DIR$/WebApplication10/WebApplication10.csproj" afterDir="false" />
<change afterPath="$PROJECT_DIR$/WebApplication10/appsettings.Development.json" afterDir="false" />
<change afterPath="$PROJECT_DIR$/WebApplication10/appsettings.json" afterDir="false" />
<change afterPath="$PROJECT_DIR$/global.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.WebApplication10/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.WebApplication10/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/WebApplication10/BookShelf.cs" beforeDir="false" afterPath="$PROJECT_DIR$/WebApplication10/BookShelf.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/WebApplication10/Controllers/BooksController.cs" beforeDir="false" afterPath="$PROJECT_DIR$/WebApplication10/Controllers/BooksController.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/WebApplication10/wwwroot/index.html" beforeDir="false" afterPath="$PROJECT_DIR$/WebApplication10/wwwroot/index.html" afterDir="false" />
<change beforePath="$PROJECT_DIR$/WebApplication10/wwwroot/scripts/script.js" beforeDir="false" afterPath="$PROJECT_DIR$/WebApplication10/wwwroot/scripts/script.js" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -39,6 +36,7 @@
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MetaFilesCheckinStateConfiguration" checkMetaFiles="true" />
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 1
}]]></component>
......@@ -128,13 +126,14 @@
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1719312434870</updated>
<workItem from="1719312436011" duration="9020000" />
<workItem from="1719312436011" duration="12422000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="UnityCheckinConfiguration" checkUnsavedScenes="true" />
<component name="UnityProjectConfiguration" hasMinimizedUI="false" />
<component name="VcsManagerConfiguration">
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
......
......@@ -21,6 +21,22 @@ public class BookShelf
Add(new Book { Id = 11, Title = "The Name Of The Wind", Author = "Patrick Rothfuss"});
}
public IEnumerable<Book> GetBooks(string authorPrefix, int page, int pageSize)
{
var query = _books.AsQueryable();
if (!string.IsNullOrEmpty(authorPrefix))
query = query.Where(b => b.Value.Author.StartsWith(authorPrefix));
return query.Skip((page - 1) * pageSize).Take(pageSize).Select(kv => kv.Value).ToList();
}
public int GetCount(string authorPrefix)
{
var query = _books.AsQueryable();
if (!string.IsNullOrEmpty(authorPrefix))
query = query.Where(b => b.Value.Author.StartsWith(authorPrefix));
return query.Count();
}
public IEnumerable<Book> GetAll()
{
return _books.Values.ToList();
......
......@@ -8,6 +8,32 @@ public class BooksController: ControllerBase
{
private readonly BookShelf _bookShelf;
[HttpGet("books")]
public IActionResult GetFilteredPageBooks([FromQuery] string author = "", [FromQuery] int page = 1,
[FromQuery] int pageSize = 10)
{
if (page <= 0 || pageSize <= 0)
return BadRequest("Page and pagesize must be greater than zero");
var totalCount = _bookShelf.GetCount(author);
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
if (page > totalPages && (totalCount == 0 && page == 1))
return BadRequest("Page number exceeds total pages.");
var books = _bookShelf.GetBooks(author, page, pageSize);
var result = new
{
TotalCount = totalCount,
Page = page,
PageSize = pageSize,
Books = books
};
return Ok(result);
}
public BooksController(BookShelf bookShelf)
{
_bookShelf = bookShelf;
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BookShelf</title>
<link rel="stylesheet" href="style.css">
<script src="scripts/script.js"></script>
</head>
<body>
<h1>List of Books in the Shelf</h1>
<div id="filterForm">
<h3>Фильтрация книг</h3>
<p>
<label>Автор:</label>
<input type="text" id="author">
</p>
<button id="fetchBooksButton">Применить фильтр</button>
</div>
<div class="books-list" id="booksList">
<!-- Сюда будет загружаться список книг -->
</div>
<div id="paginationControls">
<button id="paginationPrevPage">Предыдущая</button>
<button id="paginationNextPage">Следующая</button>
</div>
<!-- Модальное окно для редактирования книги -->
<div id="editBookModal" class="modalBook">
<div class="modal-content">
<span class="close" onclick="BookLibrary.CloseBookEdit()">&times;</span>
<h2>Edit Book</h2>
<form id="editBookForm">
<label for="editTitle">Title:</label><br>
<input type="text" id="editTitle" name="editTitle" required><br>
<label for="editAuthor">Author:</label><br>
<input type="text" id="editAuthor" name="editAuthor" required><br><br>
<input type="submit" value="Save">
</form>
</div>
</div>
<script>
function saveEditedBook(event) {
event.preventDefault(); // Предотвращаем отправку формы по умолчанию
BookLibrary.SaveEditedBook();
}
document.getElementById('editBookForm').addEventListener('submit', saveEditedBook);
let currentPage = 1;
let pageSize = 1;
function filterBooks() {
fetchBooks(1);
}
async function fetchBooks(page) {
const author = document.getElementById('author').value;
const url = new URL('/books/books', window.location.origin);
const params = { page, pageSize };
if (author) {
params.author = author;
}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
const response = await fetch(url);
if (response.ok === true) {
const data = await response.json();
displayBooks(data);
updatePagination(data);
} else {
console.error('Error fetching books:', response.status);
}
}
function prevPage() {
if (currentPage > 1) {
fetchBooks(currentPage - 1);
}
}
function nextPage() {
fetchBooks(currentPage + 1);
}
function displayBooks(data) {
const targetElement = document.getElementById('booksList');
targetElement.innerHTML = '';
data.books.forEach(book => {
BookLibrary.RenderBook(targetElement, book);
});
}
function updatePagination(data) {
currentPage = data.page;
const totalPages = Math.ceil(data.totalCount / data.pageSize);
const prevButton = document.querySelector('#paginationControls button:first-child');
const nextButton = document.querySelector('#paginationControls button:last-child');
prevButton.disabled = currentPage <= 1;
nextButton.disabled = currentPage >= totalPages;
}
document.getElementById('fetchBooksButton').addEventListener('click', filterBooks);
document.getElementById('paginationPrevPage').addEventListener('click', prevPage);
document.getElementById('paginationNextPage').addEventListener('click', nextPage);
</script>
</body>
</html>
\ No newline at end of file
......@@ -258,6 +258,9 @@ const BookLibrary = (function () {
GetAllBooksAndRender: () => {
getAllBooksAndRender();
},
RenderBook: (targetElement, book)=> {
renderBook(targetElement, book)
},
CloseBookEdit: () => {
closeBookEdit();
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment