82 lines
3.1 KiB
C#
Executable File
82 lines
3.1 KiB
C#
Executable File
using BoredCareers.Entities;
|
|
using MySql.Data.MySqlClient;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
|
|
namespace BoredCareers.Services.DatabaseService {
|
|
public partial class DatabaseService {
|
|
|
|
public async Task<Cart[]> GetCart( int accountID ) {
|
|
List<Cart> list = new List<Cart>();
|
|
using( MySqlConnection connection = GetConnection() ) {
|
|
connection.Open();
|
|
string command = @"
|
|
SELECT * FROM Cart
|
|
WHERE AccountID = @AccountID;
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
|
cmd.Parameters.AddWithValue("@AccountID", accountID);
|
|
|
|
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
|
|
while( await reader.ReadAsync() ) {
|
|
if( reader == null ) {
|
|
break;
|
|
}
|
|
int _id = reader.GetInt32("ID");
|
|
int _accountid = reader.GetInt32("AccountID");
|
|
int _productid = reader.GetInt32("ProductID");
|
|
list.Add( new Cart() {
|
|
ID = _id,
|
|
AccountID = _accountid,
|
|
ProductID = _productid
|
|
} );
|
|
}
|
|
}
|
|
}
|
|
return list.ToArray();
|
|
}
|
|
|
|
public async Task AddToCart( Cart item ) {
|
|
using( MySqlConnection connection = GetConnection() ) {
|
|
connection.Open();
|
|
string command = @"
|
|
INSERT INTO Cart
|
|
(AccountID, ProductID)
|
|
VALUES
|
|
(@AccountID, @ProductID);
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand( command , connection);
|
|
cmd.Parameters.AddWithValue("@AccountID", item.AccountID);
|
|
cmd.Parameters.AddWithValue("@ProductID", item.ProductID);
|
|
|
|
await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
}
|
|
|
|
public async Task RemoveFromCart( Cart item ) {
|
|
using( MySqlConnection connection = GetConnection() ) {
|
|
connection.Open();
|
|
string command = "DELETE FROM Cart WHERE AccountID=" + item.AccountID + " AND ProductID=" + item.ProductID + ";";
|
|
MySqlCommand cmd = new MySqlCommand( command , connection);
|
|
await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
}
|
|
|
|
public async Task ClearCart( int accountID ) {
|
|
using( MySqlConnection connection = GetConnection() ) {
|
|
connection.Open();
|
|
string command = @"
|
|
DELETE FROM Cart
|
|
WHERE AccountID = @AccountID;
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand( command , connection);
|
|
cmd.Parameters.AddWithValue("@AccountID", accountID);
|
|
|
|
await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
}
|
|
}
|
|
} |