Start work for proper database file storage
This commit is contained in:
+7
-1
@@ -23,13 +23,19 @@ CREATE TABLE IF NOT EXISTS `Product` (
|
|||||||
`ID` int(11) NOT NULL AUTO_INCREMENT,
|
`ID` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`Name` varchar(45) DEFAULT NULL,
|
`Name` varchar(45) DEFAULT NULL,
|
||||||
`Description` text DEFAULT NULL,
|
`Description` text DEFAULT NULL,
|
||||||
`Images` longtext DEFAULT NULL,
|
|
||||||
`Cost` int(11) DEFAULT NULL,
|
`Cost` int(11) DEFAULT NULL,
|
||||||
`URL` varchar(200) DEFAULT NULL,
|
`URL` varchar(200) DEFAULT NULL,
|
||||||
PRIMARY KEY (`ID`),
|
PRIMARY KEY (`ID`),
|
||||||
UNIQUE KEY `ID_UNIQUE` (`ID`)
|
UNIQUE KEY `ID_UNIQUE` (`ID`)
|
||||||
) AUTO_INCREMENT=1;
|
) 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` (
|
CREATE TABLE IF NOT EXISTS `Cart` (
|
||||||
`ID` int(11) NOT NULL AUTO_INCREMENT,
|
`ID` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`AccountID` int(11) DEFAULT NULL,
|
`AccountID` int(11) DEFAULT NULL,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ export class Product {
|
|||||||
public name: string = "";
|
public name: string = "";
|
||||||
public description: string = "";
|
public description: string = "";
|
||||||
public curShowingIMG: number = 0;
|
public curShowingIMG: number = 0;
|
||||||
public images: string[] = [];
|
public images: File[] = [];
|
||||||
public cost: number = 0;
|
public cost: number = 0;
|
||||||
public url: string = "";
|
public url: string = "";
|
||||||
}
|
}
|
||||||
@@ -25,11 +25,17 @@ namespace MistoxWebsite.Server.Entities {
|
|||||||
public string Name { get; set; } = "";
|
public string Name { get; set; } = "";
|
||||||
public string Description { get; set; } = "";
|
public string Description { get; set; } = "";
|
||||||
public int CurShowingIMG = 0;
|
public int CurShowingIMG = 0;
|
||||||
public string[] Images { get; set; } = new string[0];
|
public FormFile[] Images { get; set; } = [];
|
||||||
public int Cost { get; set; }
|
public int Cost { get; set; }
|
||||||
public string URL { 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 class Cart {
|
||||||
public int ID { get; set; }
|
public int ID { get; set; }
|
||||||
public int AccountID { get; set; }
|
public int AccountID { get; set; }
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
|
using System.Net.Http.Formatting;
|
||||||
|
|
||||||
namespace MistoxWebsite.Server.Services.DatabaseService {
|
namespace MistoxWebsite.Server.Services.DatabaseService {
|
||||||
public partial class DatabaseService {
|
public partial class DatabaseService {
|
||||||
@@ -83,26 +84,33 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
|||||||
using (MySqlConnection connection = GetConnection()) {
|
using (MySqlConnection connection = GetConnection()) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
string buildingImages = "";
|
|
||||||
foreach( string cur in Item.Images ) {
|
|
||||||
buildingImages = buildingImages + "|" + cur;
|
|
||||||
}
|
|
||||||
|
|
||||||
string command = @"
|
string command = @"
|
||||||
INSERT INTO Product
|
INSERT INTO Product
|
||||||
(Name, Description, Images, Cost, URL)
|
(Name, Description, Cost, URL)
|
||||||
VALUES
|
VALUES
|
||||||
(@Name, @Description, @Images, @Cost, @URL);
|
(@Name, @Description, @Cost, @URL);
|
||||||
|
|
||||||
|
SELECT ID FROM Product
|
||||||
|
WHERE Name = @Name;
|
||||||
";
|
";
|
||||||
|
|
||||||
MySqlCommand cmd = new MySqlCommand(command, connection);
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||||
cmd.Parameters.AddWithValue("@Name", Item.Name);
|
cmd.Parameters.AddWithValue("@Name", Item.Name);
|
||||||
cmd.Parameters.AddWithValue("@Description", Item.Description);
|
cmd.Parameters.AddWithValue("@Description", Item.Description);
|
||||||
cmd.Parameters.AddWithValue("@Images", buildingImages);
|
|
||||||
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
|
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
|
||||||
cmd.Parameters.AddWithValue("@URL", Item.URL);
|
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();
|
connection.Open();
|
||||||
|
|
||||||
string buildingImages = "";
|
string buildingImages = "";
|
||||||
foreach( string cur in Item.Images ) {
|
foreach (FormFile cur in Item.Images) {
|
||||||
buildingImages = buildingImages + "|" + cur;
|
buildingImages = buildingImages + "|" + cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
string command = @"UPDATE Product SET
|
string command = @"UPDATE Product SET
|
||||||
Name = @Name,
|
Name = @Name,
|
||||||
Description = @Description,
|
Description = @Description,
|
||||||
Images = @Images,
|
|
||||||
Cost = @Cost,
|
Cost = @Cost,
|
||||||
URL = @URL
|
URL = @URL
|
||||||
WHERE ID = @ID;
|
WHERE ID = @ID;
|
||||||
@@ -127,7 +134,6 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
|||||||
MySqlCommand cmd = new MySqlCommand(command, connection);
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||||
cmd.Parameters.AddWithValue("@Name", Item.Name);
|
cmd.Parameters.AddWithValue("@Name", Item.Name);
|
||||||
cmd.Parameters.AddWithValue("@Description", Item.Description);
|
cmd.Parameters.AddWithValue("@Description", Item.Description);
|
||||||
cmd.Parameters.AddWithValue("@Images", Item.Images);
|
|
||||||
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
|
cmd.Parameters.AddWithValue("@Cost", Item.Cost);
|
||||||
cmd.Parameters.AddWithValue("@URL", Item.URL);
|
cmd.Parameters.AddWithValue("@URL", Item.URL);
|
||||||
cmd.Parameters.AddWithValue("@ID", Item.ID);
|
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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user