Start work for proper database file storage

This commit is contained in:
2025-06-27 23:10:02 -07:00
parent c493513847
commit 8151567be2
4 changed files with 72 additions and 31 deletions
+7 -1
View File
@@ -23,13 +23,19 @@ CREATE TABLE IF NOT EXISTS `Product` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(45) DEFAULT NULL,
`Description` text DEFAULT NULL,
`Images` longtext DEFAULT NULL,
`Cost` int(11) DEFAULT NULL,
`URL` varchar(200) DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `ID_UNIQUE` (`ID`)
) AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `ProductImage` (
`ImageID` int(11) NOT NULL AUTO_INCREMENT,
`ProductID` int(11) NOT NULL,
'Image' MEDIUMBLOB DEFAULT NULL,
PRIMARY KEY (`ImageID`,`ProductID`)
)
CREATE TABLE IF NOT EXISTS `Cart` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`AccountID` int(11) DEFAULT NULL,
@@ -3,7 +3,7 @@ export class Product {
public name: string = "";
public description: string = "";
public curShowingIMG: number = 0;
public images: string[] = [];
public images: File[] = [];
public cost: number = 0;
public url: string = "";
}
@@ -25,11 +25,17 @@ namespace MistoxWebsite.Server.Entities {
public string Name { get; set; } = "";
public string Description { get; set; } = "";
public int CurShowingIMG = 0;
public string[] Images { get; set; } = new string[0];
public FormFile[] Images { get; set; } = [];
public int Cost { get; set; }
public string URL { get; set; } = "";
}
public class ProductImage {
public int ImageID { get; set; } // PK
public int ProductID { get; set; }
public byte[] Image { get; set; } = [];
}
public class Cart {
public int ID { get; set; }
public int AccountID { get; set; }
@@ -2,6 +2,7 @@
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
using System.Net.Http.Formatting;
namespace MistoxWebsite.Server.Services.DatabaseService {
public partial class DatabaseService {
@@ -83,26 +84,33 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
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)
(Name, Description, Cost, URL)
VALUES
(@Name, @Description, @Images, @Cost, @URL);
(@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("@Images", buildingImages);
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
cmd.Parameters.AddWithValue("@URL", Item.URL);
await cmd.ExecuteNonQueryAsync();
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
while (await reader.ReadAsync()) {
if (reader == null) {
break;
}
Item.ID = reader.GetInt32("ID");
}
}
foreach (FormFile cur in Item.Images) {
await AddImage( Item, cur );
}
}
}
@@ -111,14 +119,13 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
connection.Open();
string buildingImages = "";
foreach( string cur in Item.Images ) {
foreach (FormFile 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;
@@ -127,7 +134,6 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
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);
@@ -136,5 +142,28 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
}
}
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() {
}
}
}