-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseManager.cs
More file actions
153 lines (149 loc) · 6.43 KB
/
Copy pathDatabaseManager.cs
File metadata and controls
153 lines (149 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using Microsoft.Data.Sqlite;
public class DatabaseManager
{
private List<StockItem> _stocks = new List<StockItem>();
private StockFetcher _fetcher = new StockFetcher();
private const string DatabaseFile = "Data/StockData.db";
public DatabaseManager()
{
using(var connection = new SqliteConnection($"Data Source={DatabaseFile}")){
connection.Open();
string tableCommand = @"CREATE TABLE IF NOT EXISTS StockData (
id INTEGER PRIMARY KEY AUTOINCREMENT,
symbol TEXT PRIMARY KEY NOT NULL,
amount INTEGER NOT NULL
)";
using(var command = new SqliteCommand(tableCommand, connection)){
command.ExecuteNonQuery();
}
}
}
public void SaveStock(StockItem stock){
//Save stock to the database
try{
using (var connection = new SqliteConnection($"Data Source={DatabaseFile}"))
{
connection.Open();
string insertCommand = @"INSERT INTO StockData (symbol) VALUES (@symbol)";
using (var command = new SqliteCommand(insertCommand, connection))
{
command.Parameters.AddWithValue("@symbol", stock.Name);
command.ExecuteNonQuery();
}
}
}
catch (Exception ex){
Console.WriteLine($"Error saving stock: {ex.Message}");
}
}
public void RemoveStock(StockItem stock){
//Remove stock from the database
using(var connection = new SqliteConnection($"Data Source={DatabaseFile}")){
connection.Open();
string deleteCommand = @"DELETE FROM StockData WHERE symbol = @symbol AND amount = 0";
using(var command = new SqliteCommand(deleteCommand, connection)){
command.Parameters.AddWithValue("@symbol", stock.Name);
command.ExecuteNonQuery();
}
}
}
public async Task LoadData()
{
//Load following stocks from the database
using(var connection = new SqliteConnection($"Data Source={DatabaseFile}"))
{
connection.Open();
string selectCommand = @"SELECT symbol FROM StockData";
using(var command = new SqliteCommand(selectCommand, connection))
{
using(var reader = command.ExecuteReader())
{
while(reader.Read())
{
string symbol = reader.GetString(0);
// Await the fetch for each stock symbol
await _fetcher.FetchStockData(symbol);
// Check if data was found for this symbol
if (_fetcher.ItemFound != null)
{
// Create a new StockItem with proper property access
StockItem stock = new StockItem(
_fetcher.ItemFound.Name,
_fetcher.ItemFound.X,
_fetcher.ItemFound.Y,
_fetcher.ItemFound.High,
_fetcher.ItemFound.Low,
_fetcher.ItemFound.Open,
_fetcher.ItemFound.Current
);
_stocks.Add(stock);
}
else
{
Console.WriteLine($"No data found for {symbol}");
}
}
}
}
}
}
public async Task GetWallet(){
/// <summary>
/// Retrieves all items from the database with amounts greater than 0
/// and loads them into the _stocks list.
/// </summary>
using(var connection = new SqliteConnection($"Data Source={DatabaseFile}")){
connection.Open();
string selectCommand = @"SELECT symbol,amount FROM StockData WHERE amount > 0";
using(var command = new SqliteCommand(selectCommand, connection)){
using(var reader = command.ExecuteReader()){
while(reader.Read()){
string symbol = reader.GetString(0);
int amountIndex = reader.GetOrdinal("amount");
int amount = reader.GetInt32(amountIndex);
await _fetcher.FetchStockData(symbol);
if(_fetcher.ItemFound != null){
StockItem stock = new StockItem(
_fetcher.ItemFound.Name,
_fetcher.ItemFound.X,
_fetcher.ItemFound.Y,
_fetcher.ItemFound.High,
_fetcher.ItemFound.Low,
_fetcher.ItemFound.Open,
_fetcher.ItemFound.Current,
amount);
_stocks.Add(stock);
}
}
}
}
}
}
public void SaveWallet(StockItem stock){
//Update the amount of the stock in the database
using(var connection = new SqliteConnection($"Data Source={DatabaseFile}")){
connection.Open();
string updateCommand = @"Update StockData SET amount = @amount WHERE symbol = @symbol";
using(var command = new SqliteCommand(updateCommand, connection)){
command.Parameters.AddWithValue("@amount", stock.Quantity);
command.Parameters.AddWithValue("@symbol", stock.Name);
command.ExecuteNonQuery();
}
}
}
public StockItem LoadStock(string symbol){
//Load a stock from the database
_fetcher.FetchStockData(symbol).Wait();
StockItem stock = new StockItem(
_fetcher.ItemFound.Name,
_fetcher.ItemFound.X,
_fetcher.ItemFound.Y,
_fetcher.ItemFound.High,
_fetcher.ItemFound.Low,
_fetcher.ItemFound.Open,
_fetcher.ItemFound.Current);
return stock;
}
public List<StockItem> Stocks{get=>_stocks;}
}