Impliment ProductImages
This commit is contained in:
@@ -11,9 +11,6 @@ Server:
|
|||||||
Emails:
|
Emails:
|
||||||
Dont follow theme of website
|
Dont follow theme of website
|
||||||
|
|
||||||
ProductController:
|
|
||||||
GetProduct, GetAllProducts, UpdateProduct dont update the images in the new SQL table anymore
|
|
||||||
|
|
||||||
Client:
|
Client:
|
||||||
Program
|
Program
|
||||||
Probably need to turn on cors at some point
|
Probably need to turn on cors at some point
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ 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 FormFile[] Images { get; set; } = [];
|
public ProductImage[] Images { get; set; } = [];
|
||||||
public int Cost { get; set; }
|
public int Cost { get; set; }
|
||||||
public string URL { get; set; } = "";
|
public string URL { get; set; } = "";
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ namespace MistoxWebsite.Server.Entities {
|
|||||||
public class ProductImage {
|
public class ProductImage {
|
||||||
public int ImageID { get; set; } // PK
|
public int ImageID { get; set; } // PK
|
||||||
public int ProductID { get; set; }
|
public int ProductID { get; set; }
|
||||||
public byte[] Image { get; set; } = [];
|
public FormFile? Image { get; set; } = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Cart {
|
public class Cart {
|
||||||
|
|||||||
@@ -29,9 +29,12 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
|||||||
int _cost = reader.GetInt32("Cost");
|
int _cost = reader.GetInt32("Cost");
|
||||||
string _url = reader.GetString("URL");
|
string _url = reader.GetString("URL");
|
||||||
|
|
||||||
|
ProductImage[] images = await GetAllImages(_id);
|
||||||
|
|
||||||
items = new Product() {
|
items = new Product() {
|
||||||
ID = _id,
|
ID = _id,
|
||||||
Name = _name,
|
Name = _name,
|
||||||
|
Images = images,
|
||||||
Description = _description,
|
Description = _description,
|
||||||
Cost = _cost,
|
Cost = _cost,
|
||||||
URL = _url
|
URL = _url
|
||||||
@@ -42,7 +45,7 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<Product>> GetAllProducts() {
|
public async Task<Product[]> GetAllProducts() {
|
||||||
List<Product> items = new List<Product>();
|
List<Product> items = new List<Product>();
|
||||||
using (MySqlConnection connection = GetConnection()) {
|
using (MySqlConnection connection = GetConnection()) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
@@ -58,9 +61,12 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
|||||||
int _cost = reader.GetInt32("Cost");
|
int _cost = reader.GetInt32("Cost");
|
||||||
string _url = reader.GetString("URL");
|
string _url = reader.GetString("URL");
|
||||||
|
|
||||||
|
ProductImage[] images = await GetAllImages(_id);
|
||||||
|
|
||||||
items.Add(new Product() {
|
items.Add(new Product() {
|
||||||
ID = _id,
|
ID = _id,
|
||||||
Name = _name,
|
Name = _name,
|
||||||
|
Images = images,
|
||||||
Description = _description,
|
Description = _description,
|
||||||
Cost = _cost,
|
Cost = _cost,
|
||||||
URL = _url
|
URL = _url
|
||||||
@@ -68,13 +74,12 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return items;
|
return items.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task NewProduct(Product Item) {
|
public async Task NewProduct(Product Item) {
|
||||||
using (MySqlConnection connection = GetConnection()) {
|
using (MySqlConnection connection = GetConnection()) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
string command = @"
|
string command = @"
|
||||||
INSERT INTO Product
|
INSERT INTO Product
|
||||||
(Name, Description, Cost, URL)
|
(Name, Description, Cost, URL)
|
||||||
@@ -99,9 +104,8 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
|||||||
Item.ID = reader.GetInt32("ID");
|
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()) {
|
using (MySqlConnection connection = GetConnection()) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
string buildingImages = "";
|
|
||||||
foreach (FormFile cur in Item.Images) {
|
|
||||||
buildingImages = buildingImages + "|" + cur;
|
|
||||||
}
|
|
||||||
|
|
||||||
string command = @"UPDATE Product SET
|
string command = @"UPDATE Product SET
|
||||||
Name = @Name,
|
Name = @Name,
|
||||||
Description = @Description,
|
Description = @Description,
|
||||||
@@ -130,31 +129,10 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
|||||||
cmd.Parameters.AddWithValue("@ID", Item.ID);
|
cmd.Parameters.AddWithValue("@ID", Item.ID);
|
||||||
|
|
||||||
await cmd.ExecuteNonQueryAsync();
|
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() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
using MistoxWebsite.Server.Entities;
|
||||||
|
using MySql.Data.MySqlClient;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
|
|
||||||
|
namespace MistoxWebsite.Server.Services.DatabaseService {
|
||||||
|
public partial class DatabaseService {
|
||||||
|
|
||||||
|
public async Task<ProductImage[]> GetAllImages(int ProductID) {
|
||||||
|
List<ProductImage> items = new List<ProductImage>();
|
||||||
|
using (MySqlConnection connection = GetConnection()) {
|
||||||
|
connection.Open();
|
||||||
|
string command = @"
|
||||||
|
SELECT * FROM ProductImage
|
||||||
|
WHERE ProductID = @ProductID;
|
||||||
|
";
|
||||||
|
|
||||||
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||||
|
cmd.Parameters.AddWithValue("@ProductID", ProductID);
|
||||||
|
|
||||||
|
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
||||||
|
while (await reader.ReadAsync()) {
|
||||||
|
if (reader == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int _ImageID = reader.GetInt32("ImageID");
|
||||||
|
int _ProductID = reader.GetInt32("ProductID");
|
||||||
|
byte[] _Image = (byte[])reader["Image"];
|
||||||
|
|
||||||
|
items.Add(new ProductImage() {
|
||||||
|
ImageID = _ImageID,
|
||||||
|
ProductID = _ProductID,
|
||||||
|
Image = BytesToFile(_Image)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return items.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task AddAllImages(Product Item) {
|
||||||
|
using (MySqlConnection connection = GetConnection()) {
|
||||||
|
connection.Open();
|
||||||
|
foreach (ProductImage cur in Item.Images) {
|
||||||
|
if (cur.Image != null) {
|
||||||
|
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", await FileToBytes( cur.Image ));
|
||||||
|
await cmd.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAllImages(int ItemID) {
|
||||||
|
using (MySqlConnection connection = GetConnection()) {
|
||||||
|
connection.Open();
|
||||||
|
string command = @"
|
||||||
|
DELETE FROM ProductImage
|
||||||
|
WHERE ProductID = @ProductID;
|
||||||
|
";
|
||||||
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||||
|
cmd.Parameters.AddWithValue("@ProductID", ItemID);
|
||||||
|
|
||||||
|
await cmd.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<byte[]> FileToBytes(IFormFile file) {
|
||||||
|
using (var memoryStream = new MemoryStream()) {
|
||||||
|
await file.CopyToAsync(memoryStream);
|
||||||
|
return memoryStream.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public FormFile BytesToFile(byte[] fileBytes) {
|
||||||
|
var stream = new MemoryStream(fileBytes);
|
||||||
|
return new FormFile(stream, 0, fileBytes.Length, "file", Guid.NewGuid().ToString()) {
|
||||||
|
Headers = new HeaderDictionary()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user