From 463e3c3530e7b9cdd9266d082cb91756e8c88f85 Mon Sep 17 00:00:00 2001 From: adam Date: Sat, 8 Nov 2025 15:14:48 +0100 Subject: [PATCH] final add function --- src/team2.cs | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/team2.cs diff --git a/src/team2.cs b/src/team2.cs new file mode 100644 index 0000000..9a85947 --- /dev/null +++ b/src/team2.cs @@ -0,0 +1,151 @@ +using System; +using Microsoft.Data.Sqlite; + +class AddRecords +{ + public static void AddAuthor(SqliteConnection connection, string name, string surname, DateTime dateOfBirth) + { + string query = "INSERT OR IGNORE INTO Authors (Name, Surname, DateOfBirth) VALUES (@name, @surname, @dob);"; + using (var command = new SqliteCommand(query, connection)) + { + command.Parameters.AddWithValue("@name", name); + command.Parameters.AddWithValue("@surname", surname); + command.Parameters.AddWithValue("@dob", dateOfBirth.ToString("yyyy-MM-dd")); + command.ExecuteNonQuery(); + Console.WriteLine("Author added successfully."); + } + } + + public static void AddPublisher(SqliteConnection connection, string name, string state) + { + string query = "INSERT OR IGNORE INTO Publisher (Name, State) VALUES (@name, @state);"; + using (var command = new SqliteCommand(query, connection)) + { + command.Parameters.AddWithValue("@name", name); + command.Parameters.AddWithValue("@state", state); + command.ExecuteNonQuery(); + } + } + + public static void AddBook(SqliteConnection connection, string name, int authorId, int publisherId, int year, int total, int available) + { + string query = @"INSERT OR IGNORE INTO Books (Name, AuthorID, PublisherID, YearOfRelease, Total, Available) + VALUES (@name, @author, @publisher, @year, @total, @available);"; + + using (var command = new SqliteCommand()) + { + command.Connection = connection; + try + { + // Check author exists + command.CommandText = "SELECT 1 FROM Authors WHERE ID = @Aid;"; + command.Parameters.Clear(); + command.Parameters.AddWithValue("@Aid", authorId); + using (var reader = command.ExecuteReader()) + { + if (!reader.HasRows) + { + Console.WriteLine($"Author with ID {authorId} does not exist."); + return; + } + } + + // Check publisher exists + command.CommandText = "SELECT 1 FROM Publisher WHERE ID = @Pid;"; + command.Parameters.Clear(); + command.Parameters.AddWithValue("@Pid", publisherId); + using (var reader = command.ExecuteReader()) + { + if (!reader.HasRows) + { + Console.WriteLine($"Publisher with ID {publisherId} does not exist."); + return; + } + } + + // Insert book + command.CommandText = query; + command.Parameters.Clear(); + command.Parameters.AddWithValue("@name", name); + command.Parameters.AddWithValue("@year", year); + command.Parameters.AddWithValue("@total", total); + command.Parameters.AddWithValue("@available", available); + command.Parameters.AddWithValue("@author", authorId); + command.Parameters.AddWithValue("@publisher", publisherId); + command.ExecuteNonQuery(); + + } + catch (Exception ex) + { + // Log and return so the exception doesn't bubble to the caller + Console.WriteLine($"AddBook failed: {ex.Message}"); + return; + } + } + } + + public static void AddUser(SqliteConnection connection, string name, string surname) + { + string query = "INSERT OR IGNORE INTO Users (Name, Surname) VALUES (@name, @surname);"; + using (var command = new SqliteCommand(query, connection)) + { + command.Parameters.AddWithValue("@name", name); + command.Parameters.AddWithValue("@surname", surname); + command.ExecuteNonQuery(); + } + } + + public static void AddBorrowedBookRecord(SqliteConnection connection, int bookId, int userId, DateTime borrowedDate, int days = 30) + { + string returnDue = borrowedDate.AddDays(days).ToString("yyyy-MM-dd"); + string query = @"INSERT OR IGNORE INTO Borrows (DateOfBorrow, ReturnDue, UserID, BookID) + VALUES (@borrowed, @due, @user, @book);"; + + using (var command = new SqliteCommand()) + { + command.Connection = connection; + try + { + // Check user exists + command.CommandText = "SELECT 1 FROM Users WHERE ID = @Uid;"; + command.Parameters.Clear(); + command.Parameters.AddWithValue("@Uid", userId); + using (var reader = command.ExecuteReader()) + { + if (!reader.HasRows) + { + Console.WriteLine($"User with ID {userId} does not exist."); + return; + } + } + + // Check book exists + command.CommandText = "SELECT 1 FROM Books WHERE ID = @Bid;"; + command.Parameters.Clear(); + command.Parameters.AddWithValue("@Bid", bookId); + using (var reader = command.ExecuteReader()) + { + if (!reader.HasRows) + { + Console.WriteLine($"Book with ID {bookId} does not exist."); + return; + } + } + + // Insert borrow record + command.CommandText = query; + command.Parameters.Clear(); + command.Parameters.AddWithValue("@borrowed", borrowedDate.ToString("yyyy-MM-dd")); + command.Parameters.AddWithValue("@due", returnDue); + command.Parameters.AddWithValue("@user", userId); + command.Parameters.AddWithValue("@book", bookId); + command.ExecuteNonQuery(); + } + catch (Exception ex) + { + Console.WriteLine($"AddBorrowedBookRecord failed: {ex.Message}"); + return; + } + } + } +}