Impliment ProductImages

This commit is contained in:
2025-06-28 09:41:46 -07:00
parent 3999bbed78
commit 0323215a70
4 changed files with 106 additions and 40 deletions
@@ -29,9 +29,12 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
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
@@ -42,7 +45,7 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
return items;
}
public async Task<List<Product>> GetAllProducts() {
public async Task<Product[]> GetAllProducts() {
List<Product> items = new List<Product>();
using (MySqlConnection connection = GetConnection()) {
connection.Open();
@@ -58,9 +61,12 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
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
@@ -68,13 +74,12 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
}
}
}
return items;
return items.ToArray();
}
public async Task NewProduct(Product Item) {
using (MySqlConnection connection = GetConnection()) {
connection.Open();
string command = @"
INSERT INTO Product
(Name, Description, Cost, URL)
@@ -99,9 +104,8 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
Item.ID = reader.GetInt32("ID");
}
}
foreach (FormFile cur in Item.Images) {
await AddImage( Item, cur );
}
await AddAllImages(Item);
}
}
@@ -109,11 +113,6 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
using (MySqlConnection connection = GetConnection()) {
connection.Open();
string buildingImages = "";
foreach (FormFile cur in Item.Images) {
buildingImages = buildingImages + "|" + cur;
}
string command = @"UPDATE Product SET
Name = @Name,
Description = @Description,
@@ -130,31 +129,10 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
cmd.Parameters.AddWithValue("@ID", Item.ID);
await cmd.ExecuteNonQueryAsync();
await DeleteAllImages(Item.ID);
await AddAllImages(Item);
}
}
public async Task AddImage(Product Item, FormFile Image) {
using (MySqlConnection connection = GetConnection()) {
connection.Open();
string command = @"
INSERT INTO ProductImage
(ProductID, Image)
VALUES
(@ProductID, @Image);
";
MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@ProductID", Item.ID);
cmd.Parameters.AddWithValue("@Image", Image);
await cmd.ExecuteNonQueryAsync();
}
}
public async Task RemoveImage() {
}
}
}