From 39bcdb3970cc133f851edb5393c77e063630f938 Mon Sep 17 00:00:00 2001 From: lvas463 Date: Tue, 24 Oct 2023 10:46:25 -0400 Subject: [PATCH] Commit before possibly breaking code. --- PcgsInvUi/App.axaml.cs | 7 +- PcgsInvUi/Services/Database.cs | 149 ++++++++++++++++++++++++++++++++- 2 files changed, 153 insertions(+), 3 deletions(-) diff --git a/PcgsInvUi/App.axaml.cs b/PcgsInvUi/App.axaml.cs index 5a54503..24ec2fd 100644 --- a/PcgsInvUi/App.axaml.cs +++ b/PcgsInvUi/App.axaml.cs @@ -4,6 +4,7 @@ using Avalonia.Markup.Xaml; using PcgsInvUi.Services; using PcgsInvUi.ViewModels; using PcgsInvUi.Views; +using PcgsInvUi.Models; namespace PcgsInvUi; @@ -14,7 +15,11 @@ public partial class App : Application { public override void OnFrameworkInitializationCompleted() { base.OnFrameworkInitializationCompleted(); - + + // Create a connection to SQLite + var database = new Database(); + var coin = database.GetCoin(1003, "AG-3"); + if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { var coins = new Services.CoinCollection(); diff --git a/PcgsInvUi/Services/Database.cs b/PcgsInvUi/Services/Database.cs index 9b8bc19..82bb139 100644 --- a/PcgsInvUi/Services/Database.cs +++ b/PcgsInvUi/Services/Database.cs @@ -1,5 +1,150 @@ -namespace PcgsInvUi.Services; +using System; +using System.Collections.Generic; +using System.Data.SQLite; +using PcgsInvUi.Models; + +namespace PcgsInvUi.Services; public class Database { - public Database() { } + public SQLiteConnection Connection { get; set; } + private Boolean IsConnected { get; set; } + + public Database() { + // Create a connection to SQLite + Connection = CreateConnection(); + // Create a base table. + CreateCollection("CollectionTable"); + Console.WriteLine("Connected to database."); + } + + private SQLiteConnection CreateConnection() { + // Create a connection. + SQLiteConnection connection = + new SQLiteConnection("DataSource=C:\\Users\\lvas4\\coin_collection"); + + // Open the connection. + try { + connection.Open(); + IsConnected = true; + } + catch (Exception e) { + Console.WriteLine(e.Message); + } + + return connection; + } + + public void CreateCollection(string collectionName) { + // Create a table to store the collection. + if (!IsConnected) { + // TODO Create a NotConnectedToDatabaseException + Console.WriteLine("Not connected to database."); + return; + } + + // Create a SQL command. + SQLiteCommand cmd = Connection.CreateCommand(); + cmd.CommandText = + $"CREATE TABLE IF NOT EXISTS {collectionName} (" + + "PCGS_NUMBER INTEGER," + + "GRADE TEXT" + + "NAME TEXT," + + "YEAR INTEGER," + + "DENOMINATION TEXT," + + "MINT_MARK TEXT," + + "PRICE_GUIDE_VALUE REAL," + + "COIN_FACTS_LINK TEXT," + + "MAJOR_VARIETY TEXT," + + "MINOR_VARIETY TEXT," + + "DIE_VARIETY TEXT," + + "SERIES_NAME TEXT," + + "CATEGORY TEXT," + + "DESIGNATION TEXT," + + "CERT_NUM TEXT," + + "QUANTITY INTEGER," + + "PRIMARY KEY(PCGS_NUMBER, GRADE))"; + + // Execute the command. + cmd.ExecuteNonQuery(); + } + + public void InsertCoin(Coin newCoin) { + // Use our existing connection to insert a coin to the database. + if (!IsConnected) { + // TODO Create a NotConnectedToDatabaseException + Console.WriteLine("Not connected to database."); + return; + } + + // Create a SQL command. + SQLiteCommand cmd = Connection.CreateCommand(); + cmd.CommandText = + "INSERT INTO CollectionTable (PCGS_NUMBER, GRADE, NAME, YEAR," + + "DENOMINATION, MINT_MARK, PRICE_GUIDE_VALUE, COIN_FACTS_LINK," + + "MAJOR_VARIETY, MINOR_VARIETY, DIE_VARIETY, SERIES_NAME, CATEGORY, DESIGNATION," + + "CERT_NUM, QUANTITY) " + + "VALUES (@PcgsNumber, @Name, @Grade, @Value, @Year, @Denomination, @MintMark, @PriceGuideValue," + + "@CoinFactsLink, @MajorVariety, @MinorVariety, @DieVariety, @SeriesName, @Category, @Designation," + + "@CertificateNumber, @Quantity)"; + cmd.Parameters.AddWithValue("@PcgsNumber", newCoin.PCGSNo); + cmd.Parameters.AddWithValue("@Name", newCoin.Name); + cmd.Parameters.AddWithValue("@Grade", newCoin.Grade); + cmd.Parameters.AddWithValue("@Value", newCoin.PriceGuideValue); + cmd.Parameters.AddWithValue("@Year", newCoin.Year); + cmd.Parameters.AddWithValue("@Denomination", newCoin.Denomination); + cmd.Parameters.AddWithValue("@MintMark", newCoin.MintMark); + cmd.Parameters.AddWithValue("@PriceGuideValue", newCoin.PriceGuideValue); + cmd.Parameters.AddWithValue("@CoinFactsLink", newCoin.CoinFactsLink); + cmd.Parameters.AddWithValue("@MajorVariety", newCoin.MajorVariety); + cmd.Parameters.AddWithValue("@MinorVariety", newCoin.MinorVariety); + cmd.Parameters.AddWithValue("@DieVariety", newCoin.DieVariety); + cmd.Parameters.AddWithValue("@SeriesName", newCoin.SeriesName); + cmd.Parameters.AddWithValue("@Category", newCoin.Category); + cmd.Parameters.AddWithValue("@Designation", newCoin.Designation); + cmd.Parameters.AddWithValue("@CertificateNumber", newCoin.CertificateNumber); + cmd.Parameters.AddWithValue("@Quantity", newCoin.Quantity); + + // Execute the command. + cmd.ExecuteNonQuery(); + } + + public List GetCollection(string collectionName) { + SQLiteCommand cmd = Connection.CreateCommand(); + cmd.CommandText = $"SELECT * FROM {collectionName}"; + + var coins = new List(); + + using (var reader = cmd.ExecuteReader()) { + while (reader.Read()) { + // Create coin from reader then add to list. + Coin coin = new Coin(); + coins.Add(coin); + } + } + + return coins; + } + + public Coin GetCoin(int pcgsNumber, string grade) { + // Get the coin based off of the pcgs number. + SQLiteCommand cmd = Connection.CreateCommand(); + cmd.CommandText = "SELECT * FROM CollectionTable WHERE Id = @Id AND Grade = @Grade"; + cmd.Parameters.AddWithValue("@Id", pcgsNumber); + cmd.Parameters.AddWithValue("@Grade", grade); + + // Execute the command. + using (var reader = cmd.ExecuteReader()) { + while (reader.Read()) { + // TODO Create a coin from the reader. + Console.WriteLine(reader.ToString()); + Coin coin = new Coin(); + } + } + + return new Coin(); + } + + public void DeleteCoin(Coin delCoin) { + + } } \ No newline at end of file