141 lines
5.6 KiB
C#
Executable File
141 lines
5.6 KiB
C#
Executable File
using MistoxWebsite.Shared.Database;
|
|
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");
|
|
string _images = reader.GetString("Images");
|
|
int _cost = reader.GetInt32("Cost");
|
|
string _url = reader.GetString("URL");
|
|
|
|
string[] _imageList = _images.Split('|', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
items = new Product() {
|
|
ID = _id,
|
|
Name = _name,
|
|
Description = _description,
|
|
Cost = _cost,
|
|
Images = _imageList.ToList(),
|
|
URL = _url
|
|
};
|
|
}
|
|
}
|
|
}
|
|
return items;
|
|
}
|
|
|
|
public async Task<List<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");
|
|
string _images = reader.GetString("Images");
|
|
int _cost = reader.GetInt32("Cost");
|
|
string _url = reader.GetString("URL");
|
|
|
|
string[] _imageList = _images.Split('|', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
items.Add( new Product() {
|
|
ID = _id,
|
|
Name = _name,
|
|
Description = _description,
|
|
Cost = _cost,
|
|
Images = _imageList.ToList(),
|
|
URL = _url
|
|
} );
|
|
}
|
|
}
|
|
}
|
|
return items;
|
|
}
|
|
|
|
public async Task NewProduct( Product Item ) {
|
|
using( MySqlConnection connection = GetConnection() ) {
|
|
connection.Open();
|
|
|
|
string buildingImages = "";
|
|
foreach( string cur in Item.Images ) {
|
|
buildingImages = buildingImages + "|" + cur;
|
|
}
|
|
|
|
string command = @"
|
|
INSERT INTO Product
|
|
(Name, Description, Images, Cost, URL)
|
|
VALUES
|
|
(@Name, @Description, @Images, @Cost, @URL);
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand( command , connection);
|
|
cmd.Parameters.AddWithValue("@Name", Item.Name);
|
|
cmd.Parameters.AddWithValue("@Description", Item.Description);
|
|
cmd.Parameters.AddWithValue("@Images", buildingImages);
|
|
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
|
|
cmd.Parameters.AddWithValue("@URL", Item.URL);
|
|
|
|
await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
}
|
|
|
|
public async Task UpdateProduct( Product Item ) {
|
|
using( MySqlConnection connection = GetConnection() ) {
|
|
connection.Open();
|
|
|
|
string buildingImages = "";
|
|
foreach( string cur in Item.Images ) {
|
|
buildingImages = buildingImages + "|" + cur;
|
|
}
|
|
|
|
string command = @"UPDATE Product SET
|
|
Name = @Name,
|
|
Description = @Description,
|
|
Images = @Images,
|
|
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("@Images", Item.Images);
|
|
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
|
|
cmd.Parameters.AddWithValue("@URL", Item.URL);
|
|
cmd.Parameters.AddWithValue("@ID", Item.ID);
|
|
|
|
await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|