diff --git a/src/team4.cs b/src/team4.cs index 2d86756..a08c679 100644 --- a/src/team4.cs +++ b/src/team4.cs @@ -2,259 +2,263 @@ using System; using Microsoft.Data.Sqlite; using Database.Dto; -public class Team4 + +namespace Team4 { - public static List GetBooksByName(SqliteConnection conn, string name) + public class Team4 { - try + public static List GetBooksByName(SqliteConnection conn, string name) { - using (var command = conn.CreateCommand()) + try { - command.CommandText = @" - SELECT book.ID, book.Name, book.YearOfRelease, book.Total, - author.ID, author.Name, author.Surname, author.DateOfBirth, - publisher.ID, publisher.Name, publisher.State - FROM Books book - LEFT JOIN Authors author ON book.AuthorID = author.ID - LEFT JOIN Publisher publisher ON book.PublisherID = publisher.ID - WHERE book.Name = @name;"; - command.Parameters.AddWithValue("@name", name); - - var books = new List(); - using (var reader = command.ExecuteReader()) + using (var command = conn.CreateCommand()) { - while (reader.Read()) + command.CommandText = @" + SELECT book.ID, book.Name, book.YearOfRelease, book.Total, + author.ID, author.Name, author.Surname, author.DateOfBirth, + publisher.ID, publisher.Name, publisher.State + FROM Books book + LEFT JOIN Authors author ON book.AuthorID = author.ID + LEFT JOIN Publisher publisher ON book.PublisherID = publisher.ID + WHERE book.Name = @name;"; + command.Parameters.AddWithValue("@name", name); + + var books = new List(); + using (var reader = command.ExecuteReader()) { - var book = new BookDto + while (reader.Read()) { - Id = reader.GetInt32(0), - Name = reader.GetString(1), - YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), - Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3), - Author = new AuthorDto + var book = new BookDto { - Id = reader.IsDBNull(4) ? 0 : reader.GetInt32(4), - Name = reader.IsDBNull(5) ? "" : reader.GetString(5), - Surname = reader.IsDBNull(6) ? "" : reader.GetString(6), - DateOfBirth = reader.IsDBNull(7) ? DateTime.MinValue : reader.GetDateTime(7) - }, - Publisher = new Publisher + Id = reader.GetInt32(0), + Name = reader.GetString(1), + YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), + Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3), + Author = new AuthorDto + { + Id = reader.IsDBNull(4) ? 0 : reader.GetInt32(4), + Name = reader.IsDBNull(5) ? "" : reader.GetString(5), + Surname = reader.IsDBNull(6) ? "" : reader.GetString(6), + DateOfBirth = reader.IsDBNull(7) ? DateTime.MinValue : reader.GetDateTime(7) + }, + Publisher = new Publisher + { + Id = reader.IsDBNull(8) ? 0 : reader.GetInt32(8), + Name = reader.IsDBNull(9) ? "" : reader.GetString(9), + State = reader.IsDBNull(10) ? "" : reader.GetString(10) + } + }; + books.Add(book); + } + } + return books; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error in GetBooksByName: {ex.Message}"); + throw; + } + } + public static List GetBooksByAuthor(SqliteConnection conn, AuthorDto author) + { + try + { + using (var command = conn.CreateCommand()) + { + command.CommandText = @" + SELECT book.ID, book.Name, book.YearOfRelease, book.Total, + publisher.ID, publisher.Name, publisher.State + FROM Books book + LEFT JOIN Publisher publisher ON book.PublisherID = publisher.ID + WHERE book.AuthorID = @id;"; + command.Parameters.AddWithValue("@id", author.Id); + + var books = new List(); + + using (var reader = command.ExecuteReader()) + { + while (reader.Read()) + { + books.Add(new BookDto { - Id = reader.IsDBNull(8) ? 0 : reader.GetInt32(8), - Name = reader.IsDBNull(9) ? "" : reader.GetString(9), - State = reader.IsDBNull(10) ? "" : reader.GetString(10) - } - }; - books.Add(book); + Id = reader.GetInt32(0), + Name = reader.GetString(1), + YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), + Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3), + Author = author, + Publisher = new Publisher + { + Id = reader.IsDBNull(4) ? 0 : reader.GetInt32(4), + Name = reader.IsDBNull(5) ? "" : reader.GetString(5), + State = reader.IsDBNull(6) ? "" : reader.GetString(6) + } + }); + } } + return books; } - return books; + } + catch (Exception ex) + { + Console.WriteLine($"Error in GetBooksByAuthor: {ex.Message}"); + throw; } } - catch (Exception ex) + + public static List GetBooksByAvailableBooks(SqliteConnection conn, int minStock = 1) { - Console.WriteLine($"Error in GetBooksByName: {ex.Message}"); - throw; - } - } - public static List GetBooksByAuthor(SqliteConnection conn, AuthorDto author) - { - try - { - using (var command = conn.CreateCommand()) + try { - command.CommandText = @" - SELECT book.ID, book.Name, book.YearOfRelease, book.Total, - publisher.ID, publisher.Name, publisher.State - FROM Books book - LEFT JOIN Publisher publisher ON book.PublisherID = publisher.ID - WHERE book.AuthorID = @id;"; - command.Parameters.AddWithValue("@id", author.Id); - - var books = new List(); - - using (var reader = command.ExecuteReader()) + using (var command = conn.CreateCommand()) { - while (reader.Read()) + command.CommandText = @" + SELECT book.ID, book.Name, book.YearOfRelease, book.Total + FROM Books book + WHERE book.Total >= @min;"; + command.Parameters.AddWithValue("@min", minStock); + + var books = new List(); + + using (var reader = command.ExecuteReader()) { - books.Add(new BookDto + while (reader.Read()) { - Id = reader.GetInt32(0), - Name = reader.GetString(1), - YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), - Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3), - Author = author, - Publisher = new Publisher + books.Add(new BookDto { - Id = reader.IsDBNull(4) ? 0 : reader.GetInt32(4), - Name = reader.IsDBNull(5) ? "" : reader.GetString(5), - State = reader.IsDBNull(6) ? "" : reader.GetString(6) - } - }); + Id = reader.GetInt32(0), + Name = reader.GetString(1), + YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), + Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3) + }); + } } + return books; } - return books; } - } - catch (Exception ex) - { - Console.WriteLine($"Error in GetBooksByAuthor: {ex.Message}"); - throw; - } - } - - public static List GetBooksByAvailableBooks(SqliteConnection conn, int minStock = 1) - { - try - { - using (var command = conn.CreateCommand()) + catch (Exception ex) { - command.CommandText = @" - SELECT book.ID, book.Name, book.YearOfRelease, book.Total - FROM Books book - WHERE book.Total >= @min;"; - command.Parameters.AddWithValue("@min", minStock); - - var books = new List(); - - using (var reader = command.ExecuteReader()) - { - while (reader.Read()) - { - books.Add(new BookDto - { - Id = reader.GetInt32(0), - Name = reader.GetString(1), - YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), - Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3) - }); - } - } - return books; + Console.WriteLine($"Error in GetBooksByAvailableBooks: {ex.Message}"); + throw; } } - catch (Exception ex) - { - Console.WriteLine($"Error in GetBooksByAvailableBooks: {ex.Message}"); - throw; - } - } - public static List GetStolenBooks(SqliteConnection conn) - { - try + public static List GetStolenBooks(SqliteConnection conn) { - using (var command = conn.CreateCommand()) + try { - command.CommandText = @" - SELECT book.ID, book.Name, book.YearOfRelease, book.Total - FROM Borrows borrow - INNER JOIN Books book ON borrow.BookID = book.ID - WHERE borrow.DateOfReturn IS NULL AND borrow.ReturnDue < DATE('now');"; - - var books = new List(); - using (var reader = command.ExecuteReader()) + using (var command = conn.CreateCommand()) { - while (reader.Read()) - { - books.Add(new BookDto - { - Id = reader.GetInt32(0), - Name = reader.GetString(1), - YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), - Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3) - }); - } - } - return books; - } - } - catch (Exception ex) - { - Console.WriteLine($"Error in GetStolenBooks: {ex.Message}"); - throw; - } - } + command.CommandText = @" + SELECT book.ID, book.Name, book.YearOfRelease, book.Total + FROM Borrows borrow + INNER JOIN Books book ON borrow.BookID = book.ID + WHERE borrow.DateOfReturn IS NULL AND borrow.ReturnDue < DATE('now');"; - public static List GetStolenBooksByUser(SqliteConnection conn, UserDto user) - { - try - { - using (var command = conn.CreateCommand()) + var books = new List(); + using (var reader = command.ExecuteReader()) + { + while (reader.Read()) + { + books.Add(new BookDto + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), + Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3) + }); + } + } + return books; + } + } + catch (Exception ex) { - command.CommandText = @" - SELECT book.ID, book.Name, book.YearOfRelease, book.Total - FROM Borrows borrow - INNER JOIN Books book ON borrow.BookID = book.ID - WHERE borrow.UserID = @uid AND borrow.DateOfReturn IS NULL AND borrow.ReturnDue < DATE('now');"; - command.Parameters.AddWithValue("@uid", user.Id); - - var books = new List(); - using (var reader = command.ExecuteReader()) - { - while (reader.Read()) - { - books.Add(new BookDto - { - Id = reader.GetInt32(0), - Name = reader.GetString(1), - YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), - Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3) - }); - } - } - return books; + Console.WriteLine($"Error in GetStolenBooks: {ex.Message}"); + throw; } } - catch (Exception ex) - { - Console.WriteLine($"Error in GetStolenBooksByUser: {ex.Message}"); - throw; - } - } - public static AuthorDto GetAuthorById(SqliteConnection conn, int authorID) - { - try + public static List GetStolenBooksByUser(SqliteConnection conn, UserDto user) { - using (var command = conn.CreateCommand()) + try { - command.CommandText = @" - SELECT author.ID - FROM Authors author - WHERE author.ID = @aid;"; - command.Parameters.AddWithValue("@aid", authorID); - - using (var reader = command.ExecuteReader()) + using (var command = conn.CreateCommand()) { - while (reader.Read()) + command.CommandText = @" + SELECT book.ID, book.Name, book.YearOfRelease, book.Total + FROM Borrows borrow + INNER JOIN Books book ON borrow.BookID = book.ID + WHERE borrow.UserID = @uid AND borrow.DateOfReturn IS NULL AND borrow.ReturnDue < DATE('now');"; + command.Parameters.AddWithValue("@uid", user.Id); + + var books = new List(); + using (var reader = command.ExecuteReader()) { - return new AuthorDto + while (reader.Read()) { - Id = reader.GetInt32(0), - Name = reader.GetString(1), - Surname = reader.GetString(2), - DateOfBirth = reader.GetDateTime(3) - }; + books.Add(new BookDto + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + YearOfRelease = reader.IsDBNull(2) ? 0 : reader.GetInt32(2), + Total = reader.IsDBNull(3) ? 0 : reader.GetInt32(3) + }); + } } + return books; } - return null; + } + catch (Exception ex) + { + Console.WriteLine($"Error in GetStolenBooksByUser: {ex.Message}"); + throw; } } - catch (Exception ex) + + public static AuthorDto GetAuthorById(SqliteConnection conn, int authorID) { - Console.WriteLine($"Error in GetAuthorById: {ex.Message}"); - throw; + try + { + using (var command = conn.CreateCommand()) + { + command.CommandText = @" + SELECT author.ID + FROM Authors author + WHERE author.ID = @aid;"; + command.Parameters.AddWithValue("@aid", authorID); + + using (var reader = command.ExecuteReader()) + { + while (reader.Read()) + { + return new AuthorDto + { + Id = reader.GetInt32(0), + Name = reader.GetString(1), + Surname = reader.GetString(2), + DateOfBirth = reader.GetDateTime(3) + }; + } + } + return null; + } + } + catch (Exception ex) + { + Console.WriteLine($"Error in GetAuthorById: {ex.Message}"); + throw; + } + } + + public static void GetBooksBy(SqliteConnection conn) + { + throw new NotImplementedException(); + } + + public static void GetBorrowLogBy(SqliteConnection conn) + { + throw new NotImplementedException(); } } - - public static void GetBooksBy(SqliteConnection conn) - { - throw new NotImplementedException(); - } - - public static void GetBorrowLogBy(SqliteConnection conn) - { - throw new NotImplementedException(); - } -} +} \ No newline at end of file