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,
|
||||
`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,13 +2,14 @@
|
||||
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 {
|
||||
|
||||
public async Task<Product?> GetProduct( int ID ) {
|
||||
public async Task<Product?> GetProduct(int ID) {
|
||||
Product? items = null;
|
||||
using( MySqlConnection connection = GetConnection() ) {
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
connection.Open();
|
||||
string command = @"
|
||||
SELECT * FROM Product
|
||||
@@ -18,9 +19,9 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
||||
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||
cmd.Parameters.AddWithValue("@ID", ID);
|
||||
|
||||
using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
|
||||
while( await reader.ReadAsync() ) {
|
||||
if( reader == null ) {
|
||||
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
||||
while (await reader.ReadAsync()) {
|
||||
if (reader == null) {
|
||||
break;
|
||||
}
|
||||
int _id = reader.GetInt32("ID");
|
||||
@@ -48,12 +49,12 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
||||
|
||||
public async Task<List<Product>> GetAllProducts() {
|
||||
List<Product> items = new List<Product>();
|
||||
using( MySqlConnection connection = GetConnection() ) {
|
||||
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 ) {
|
||||
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
||||
while (await reader.ReadAsync()) {
|
||||
if (reader == null) {
|
||||
break;
|
||||
}
|
||||
int _id = reader.GetInt32("ID");
|
||||
@@ -65,60 +66,66 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
||||
|
||||
string[] _imageList = _images.Split('|', StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
items.Add( new Product() {
|
||||
items.Add(new Product() {
|
||||
ID = _id,
|
||||
Name = _name,
|
||||
Description = _description,
|
||||
Cost = _cost,
|
||||
Images = _imageList,
|
||||
URL = _url
|
||||
} );
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
public async Task NewProduct( Product Item ) {
|
||||
using( MySqlConnection connection = GetConnection() ) {
|
||||
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)
|
||||
(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);
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateProduct( Product Item ) {
|
||||
using( MySqlConnection connection = GetConnection() ) {
|
||||
public async Task UpdateProduct(Product Item) {
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user