139 lines
5.3 KiB
C#
Executable File
139 lines
5.3 KiB
C#
Executable File
using MistoxWebsite.Server.Entities;
|
|
using MySql.Data.MySqlClient;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
|
|
namespace MistoxWebsite.Server.Services.DatabaseService {
|
|
public partial class DatabaseService {
|
|
|
|
public async Task<Product?> GetProduct(int ID) {
|
|
Product? items = null;
|
|
using (MySqlConnection connection = GetConnection()) {
|
|
connection.Open();
|
|
string command = @"
|
|
SELECT * FROM Product
|
|
WHERE ID = @ID;
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
|
cmd.Parameters.AddWithValue("@ID", ID);
|
|
|
|
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
|
while (await reader.ReadAsync()) {
|
|
if (reader == null) {
|
|
break;
|
|
}
|
|
int _id = reader.GetInt32("ID");
|
|
string _name = reader.GetString("Name");
|
|
string _description = reader.GetString("Description");
|
|
int _cost = reader.GetInt32("Cost");
|
|
string _url = reader.GetString("URL");
|
|
|
|
ProductImage[] images = await GetAllImages(_id);
|
|
|
|
items = new Product() {
|
|
ID = _id,
|
|
Name = _name,
|
|
Images = images,
|
|
Description = _description,
|
|
Cost = _cost,
|
|
URL = _url
|
|
};
|
|
}
|
|
}
|
|
}
|
|
return items;
|
|
}
|
|
|
|
public async Task<Product[]> GetAllProducts() {
|
|
List<Product> items = new List<Product>();
|
|
using (MySqlConnection connection = GetConnection()) {
|
|
connection.Open();
|
|
MySqlCommand cmd = new MySqlCommand("SELECT * FROM Product", connection);
|
|
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
|
while (await reader.ReadAsync()) {
|
|
if (reader == null) {
|
|
break;
|
|
}
|
|
int _id = reader.GetInt32("ID");
|
|
string _name = reader.GetString("Name");
|
|
string _description = reader.GetString("Description");
|
|
int _cost = reader.GetInt32("Cost");
|
|
string _url = reader.GetString("URL");
|
|
|
|
ProductImage[] images = await GetAllImages(_id);
|
|
|
|
items.Add(new Product() {
|
|
ID = _id,
|
|
Name = _name,
|
|
Images = images,
|
|
Description = _description,
|
|
Cost = _cost,
|
|
URL = _url
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return items.ToArray();
|
|
}
|
|
|
|
public async Task NewProduct(Product Item) {
|
|
using (MySqlConnection connection = GetConnection()) {
|
|
connection.Open();
|
|
string command = @"
|
|
INSERT INTO Product
|
|
(Name, Description, Cost, URL)
|
|
VALUES
|
|
(@Name, @Description, @Cost, @URL);
|
|
|
|
SELECT ID FROM Product
|
|
WHERE Name = @Name;
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
|
cmd.Parameters.AddWithValue("@Name", Item.Name);
|
|
cmd.Parameters.AddWithValue("@Description", Item.Description);
|
|
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
|
|
cmd.Parameters.AddWithValue("@URL", Item.URL);
|
|
|
|
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
|
while (await reader.ReadAsync()) {
|
|
if (reader == null) {
|
|
break;
|
|
}
|
|
Item.ID = reader.GetInt32("ID");
|
|
}
|
|
}
|
|
|
|
await AddAllImages(Item);
|
|
}
|
|
}
|
|
|
|
public async Task UpdateProduct(Product Item) {
|
|
using (MySqlConnection connection = GetConnection()) {
|
|
connection.Open();
|
|
|
|
string command = @"UPDATE Product SET
|
|
Name = @Name,
|
|
Description = @Description,
|
|
Cost = @Cost,
|
|
URL = @URL
|
|
WHERE ID = @ID;
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
|
cmd.Parameters.AddWithValue("@Name", Item.Name);
|
|
cmd.Parameters.AddWithValue("@Description", Item.Description);
|
|
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
|
|
cmd.Parameters.AddWithValue("@URL", Item.URL);
|
|
cmd.Parameters.AddWithValue("@ID", Item.ID);
|
|
|
|
await cmd.ExecuteNonQueryAsync();
|
|
|
|
await DeleteAllImages(Item.ID);
|
|
await AddAllImages(Item);
|
|
}
|
|
}
|
|
}
|
|
}
|