Here's an example of how to create an SQLite database in C# with the given requirements, including inserts, deletes, updates, and a GetOrAdd method for both users and cards:
First, we need to install the required NuGet packages to use SQLite in C#. Open the Package Manager Console and execute the following command:
Install-Package System.Data.SQLite.Core
Then, create a class that represents a user:
public class User
{
public string Id { get; set; }
public List<Card> Cards { get; set; }
}
And another class for cards:
public class Card
{
public string Id { get; set; }
public double Units { get; set; }
public int Remaining { get; set; }
public int Timestamp { get; set; }
}
To create the SQLite database and tables, we can use the following code:
using System.Data.SQLite;
const string connectionString = "Data Source=database.db;Version=3;";
using var connection = new SQLiteConnection(connectionString);
connection.Open();
var createUserTableQuery = @"
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS cards (
id TEXT PRIMARY KEY,
units REAL NOT NULL,
remaining INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
user_id TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
";
using var command = new SQLiteCommand(createUserTableQuery, connection);
command.ExecuteNonQuery();
This code creates two tables - "users" and "cards" - that are related by a foreign key. The "cards" table includes a reference to the "users" table via the "user_id" column.
Next, we can implement the GetOrAdd method for users:
public static User GetOrAddUser(string userId)
{
using var command = new SQLiteCommand(connection);
command.CommandText = "SELECT COUNT(*) FROM users WHERE id = @userId";
command.Parameters.AddWithValue("@userId", userId);
var userExists = Convert.ToInt32(command.ExecuteScalar()) > 0;
if (userExists)
{
return GetUser(userId);
}
else
{
var newUser = new User { Id = userId, Cards = new List<Card>() };
InsertUser(newUser);
return newUser;
}
}
This code checks if a user with the given ID already exists in the database. If not, it creates a new user and inserts it into the database.
To implement the GetOrAdd method for cards, we can use a similar approach:
public static Card GetOrAddCard(string cardId, double units, int remaining, int timestamp, User user)
{
using var command = new SQLiteCommand(connection);
command.CommandText = "SELECT COUNT(*) FROM cards WHERE id = @cardId";
command.Parameters.AddWithValue("@cardId", cardId);
var cardExists = Convert.ToInt32(command.ExecuteScalar()) > 0;
if (cardExists)
{
return GetCard(cardId);
}
else
{
var newCard = new Card { Id = cardId, Units = units, Remaining = remaining, Timestamp = timestamp };
InsertCard(newCard, user);
return newCard;
}
}
This code checks if a card with the given ID already exists in the database. If not, it creates a new card and inserts it into the database with a reference to the specified user.
To insert a new user into the database, we can use the following code:
public static void InsertUser(User user)
{
using var command = new SQLiteCommand(connection);
command.CommandText = "INSERT INTO users (id) VALUES (@userId)";
command.Parameters.AddWithValue("@userId", user.Id);
command.ExecuteNonQuery();
}
And to insert a new card:
public static void InsertCard(Card card, User user)
{
using var command = new SQLiteCommand(connection);
command.CommandText = "INSERT INTO cards (id, units, remaining, timestamp, user_id) VALUES (@cardId, @units, @remaining, @timestamp, @userId)";
command.Parameters.AddWithValue("@cardId", card.Id);
command.Parameters.AddWithValue("@units", card.Units);
command.Parameters.AddWithValue("@remaining", card.Remaining);
command.Parameters.AddWithValue("@timestamp", card.Timestamp);
command.Parameters.AddWithValue("@userId", user.Id);
command.ExecuteNonQuery();
}
To get a user from the database by ID:
public static User GetUser(string userId)
{
using var command = new SQLiteCommand(connection);
command.CommandText = "SELECT * FROM users WHERE id = @userId";
command.Parameters.AddWithValue("@userId", userId);
using var reader = command.ExecuteReader();
if (!reader.Read())
{
throw new InvalidOperationException("User not found.");
}
return new User { Id = reader.GetString(0) };
}
And to get a card by ID:
public static Card GetCard(string cardId)
{
using var command = new SQLiteCommand(connection);
command.CommandText = "SELECT * FROM cards WHERE id = @cardId";
command.Parameters.AddWithValue("@cardId", cardId);
using var reader = command.ExecuteReader();
if (!reader.Read())
{
throw new InvalidOperationException("Card not found.");
}
return new Card { Id = reader.GetString(0), Units = reader.GetDouble(1), Remaining = reader.GetInt32(2), Timestamp = reader.GetInt32(3) };
}
Finally, to update a card in the database:
public static void UpdateCard(Card card)
{
using var command = new SQLiteCommand(connection);
command.CommandText = "UPDATE cards SET units = @units, remaining = @remaining, timestamp = @timestamp WHERE id = @cardId";
command.Parameters.AddWithValue("@cardId", card.Id);
command.Parameters.AddWithValue("@units", card.Units);
command.Parameters.AddWithValue("@remaining", card.Remaining);
command.Parameters.AddWithValue("@timestamp", card.Timestamp);
command.ExecuteNonQuery();
}
And to delete a card from the database:
public static void DeleteCard(Card card)
{
using var command = new SQLiteCommand(connection);
command.CommandText = "DELETE FROM cards WHERE id = @cardId";
command.Parameters.AddWithValue("@cardId", card.Id);
command.ExecuteNonQuery();
}
With these methods, you can use SQLite in C# to create a database with users and cards, and perform CRUD operations on them.