using System.Collections; using Microsoft.Data.SqlClient; namespace Library; public class BookListData : IEnumerable { private readonly List m_Elements = new( ); public IEnumerator GetEnumerator( ) { return m_Elements.GetEnumerator( ); } IEnumerator IEnumerable.GetEnumerator( ) { return m_Elements.GetEnumerator( ); } static public int GetCategoryCount( SqlConnection connection , int category_id ) { return new SQLBuilder( connection , @"SELECT count(*) FROM v_repository WHERE category_id=@category and include_category_id=@category" ) .AddParameter( "@category" , category_id ) .ExecuteAndReadInteger( 0 ); } public static int GetTotalSearchCount( SqlConnection connection , string term ) { return new SQLBuilder( connection , "SELECT count(*) FROM v_repository_search" ) .AddLike( "search_index" , term ) .ExecuteAndReadInteger( 0 ); } public void LoadSearch( SqlConnection connection , string term , int page , int items_per_page ) { using SqlDataReader reader = new SQLBuilder( connection , "SELECT * FROM v_repository_search" , "title" ) .AddLike( "search_index" , term ) .SetPaging( page , items_per_page ) .ExecuteReader( ); while ( reader.Read( ) ) { BookData book = new( ) { ID = reader.Int( "id" ) , UUID = reader.StringSafe( "uuid" ) , CategoryID = reader.Int( "category_id" ) , CategoryName = reader.StringSafe( "category_name" ) , BookTypeID = reader.Int( "book_type_id" ) , BookTypeName = reader.StringSafe( "book_type_name" ) , Title = reader.StringSafe( "title" ) , Author = reader.StringSafe( "author" ) , Access = reader.StringSafe( "access" ) , Keywords = reader.StringSafe( "keywords" ) , /* Publisher = reader.StringSafe( "publisher" ) , PublishYear = reader.StringSafe( "publish_year" ) , Keywords = reader.StringSafe( "keywords" ) , Description = reader.StringSafe( "description" ) , CreateDate = reader.DateTime( "create_date" ) , Opisanie = reader.StringSafe( "opisanie" ) , KodDrugi = reader.StringSafe( "koddrugi" ) , */ }; m_Elements.Add( book ); } } public void LoadCategory( SqlConnection connection , int category_id , int page , int books_per_page ) { using SqlDataReader reader = new SQLBuilder( connection , // "select * from v_repository where include_category_id=category_id" @"SELECT * FROM v_repository WHERE category_id=@category and include_category_id=@category ORDER BY sort, title OFFSET @offset ROWS FETCH NEXT @rows ROWS ONLY" ) .AddParameter( "@category" , category_id ) .AddParameter( "@offset" , page * books_per_page ) .AddParameter( "@rows" , books_per_page ) .ExecuteReader( ); while ( reader.Read( ) ) { BookData book = new( ) { ID = reader.Int( "id" ) , UUID = reader.StringSafe( "uuid" ) , CategoryID = reader.Int( "category_id" ) , CategoryName = reader.StringSafe( "category_name" ) , BookTypeID = reader.Int( "book_type_id" ) , BookTypeName = reader.StringSafe( "book_type_name" ) , Title = reader.StringSafe( "title" ) , Author = reader.StringSafe( "author" ) , Access = reader.StringSafe( "access" ) , /* Publisher = reader.StringSafe( "publisher" ) , PublishYear = reader.StringSafe( "publish_year" ) , Keywords = reader.StringSafe( "keywords" ) , Description = reader.StringSafe( "description" ) , CreateDate = reader.DateTime( "create_date" ) , Opisanie = reader.StringSafe( "opisanie" ) , KodDrugi = reader.StringSafe( "koddrugi" ) , */ }; m_Elements.Add( book ); } } }