Change backend for better image loading
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using MistoxWebsite.Server.Services.DatabaseService;
|
||||
using MistoxWebsite.Server.Entities;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MistoxWebsite.Server.Controllers {
|
||||
[ApiController]
|
||||
@@ -9,8 +10,6 @@ namespace MistoxWebsite.Server.Controllers {
|
||||
|
||||
DatabaseService _databaseService;
|
||||
|
||||
public static Product[] CatalogItems = [];
|
||||
|
||||
public ProductController( DatabaseService databaseService ) {
|
||||
_databaseService = databaseService;
|
||||
}
|
||||
@@ -72,7 +71,6 @@ namespace MistoxWebsite.Server.Controllers {
|
||||
}
|
||||
obj.Images = building.ToArray();
|
||||
await _databaseService.NewProduct(obj);
|
||||
await UpdateStore();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
@@ -84,7 +82,6 @@ namespace MistoxWebsite.Server.Controllers {
|
||||
public async Task<ActionResult<bool>> UpdateProduct( [FromBody] Product obj ) {
|
||||
try {
|
||||
await _databaseService.UpdateProduct( obj );
|
||||
await UpdateStore();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
@@ -93,31 +90,45 @@ namespace MistoxWebsite.Server.Controllers {
|
||||
|
||||
[Route( "api/product/get" )]
|
||||
[HttpPost]
|
||||
public ActionResult<Product> GetProduct( [FromBody] Product product ) {
|
||||
public async Task<ActionResult<Product>> GetProduct( [FromBody] int productID ) {
|
||||
try {
|
||||
foreach( Product? prod in CatalogItems ) {
|
||||
if( product.ID == prod.ID ) {
|
||||
return prod;
|
||||
}
|
||||
Product? x = await _databaseService.GetProduct(productID);
|
||||
if (x != null) {
|
||||
return x;
|
||||
} else {
|
||||
return NotFound();
|
||||
}
|
||||
product.ID = -1;
|
||||
return product;
|
||||
} catch {
|
||||
return new Product();
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[Route( "api/product/getall" )]
|
||||
[HttpPost]
|
||||
public ActionResult<Product[]> GetAllProducts() {
|
||||
public async Task<Product[]> GetAllProducts() {
|
||||
try {
|
||||
return CatalogItems.ToArray();
|
||||
return await _databaseService.GetAllProducts();
|
||||
} catch {
|
||||
return new Product[0];
|
||||
return Array.Empty<Product>();
|
||||
}
|
||||
}
|
||||
|
||||
[Route( "api/product/getowned" )]
|
||||
[Route( "api/productimage/get" )]
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetProductImage( int ProductID, int ImageID ) {
|
||||
try {
|
||||
ProductImage? img = await _databaseService.GetImage(ProductID, ImageID);
|
||||
if (img != null) {
|
||||
return File(img.Image, "Image/*");
|
||||
} else {
|
||||
return NotFound();
|
||||
}
|
||||
} catch {
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
|
||||
[Route("api/product/getowned")]
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<Receipt[]>> GetOwnedProduct() {
|
||||
try {
|
||||
@@ -180,24 +191,6 @@ namespace MistoxWebsite.Server.Controllers {
|
||||
}
|
||||
}
|
||||
|
||||
[Route( "api/product/hotreload" )]
|
||||
[HttpPost] // Not implimented in admin panel
|
||||
public async Task UpdateStore() {
|
||||
await HotReload( _databaseService );
|
||||
}
|
||||
|
||||
public static async Task HotReload( DatabaseService ds ) {
|
||||
try {
|
||||
CatalogItems = await ds.GetAllProducts();
|
||||
} catch {
|
||||
CatalogItems = new Product[]{
|
||||
new Product() { ID = 0, Name = "offline prod1", Cost = 100, Description = "offline desc" },
|
||||
new Product() { ID = 1, Name = "offline prod2", Cost = 100, Description = "offline desc" },
|
||||
new Product() { ID = 2, Name = "offline prod3", Cost = 100, Description = "offline desc" }
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ namespace MistoxWebsite.Server.Entities {
|
||||
public int ID { get; set; } // PK
|
||||
public string Name { get; set; } = "";
|
||||
public string Description { get; set; } = "";
|
||||
public int CurShowingIMG = 0;
|
||||
public ProductImage[] Images { get; set; } = [];
|
||||
public int Cost { get; set; }
|
||||
public string URL { get; set; } = "";
|
||||
|
||||
@@ -6,6 +6,42 @@ using System.Data.Common;
|
||||
namespace MistoxWebsite.Server.Services.DatabaseService {
|
||||
public partial class DatabaseService {
|
||||
|
||||
public async Task<ProductImage?> GetImage(int ProductID, int ImageID) {
|
||||
ProductImage? item = null;
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
connection.Open();
|
||||
string command = @"
|
||||
SELECT * FROM ProductImage
|
||||
WHERE ProductID = @ProductID AND ImageID = @ImageID;
|
||||
";
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||
cmd.Parameters.AddWithValue("@ProductID", ProductID);
|
||||
cmd.Parameters.AddWithValue("@ImageID", ImageID);
|
||||
|
||||
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"];
|
||||
string _Name = reader.GetString("Name");
|
||||
|
||||
item = new ProductImage() {
|
||||
ImageID = _ImageID,
|
||||
ProductID = _ProductID,
|
||||
Image = _Image,
|
||||
Name = _Name
|
||||
};
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
public async Task<ProductImage[]> GetAllImages(int ProductID) {
|
||||
List<ProductImage> items = new List<ProductImage>();
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
@@ -25,13 +61,11 @@ namespace MistoxWebsite.Server.Services.DatabaseService {
|
||||
}
|
||||
int _ImageID = reader.GetInt32("ImageID");
|
||||
int _ProductID = reader.GetInt32("ProductID");
|
||||
byte[] _Image = (byte[])reader["Image"];
|
||||
string _Name = reader.GetString("Name");
|
||||
|
||||
items.Add(new ProductImage() {
|
||||
ImageID = _ImageID,
|
||||
ProductID = _ProductID,
|
||||
Image = _Image,
|
||||
Name = _Name
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user