初始化
Some checks failed
CI/CD Pipeline / 测试 (18.x) (push) Has been cancelled
CI/CD Pipeline / 测试 (20.x) (push) Has been cancelled
CI/CD Pipeline / 安全检查 (push) Has been cancelled
CI/CD Pipeline / 部署 (push) Has been cancelled
CI/CD Pipeline / 通知 (push) Has been cancelled

This commit is contained in:
2025-11-03 19:47:36 +08:00
parent 7a04b85667
commit f25b0307db
454 changed files with 37064 additions and 4544 deletions

View File

@@ -1,6 +1,7 @@
using System.Data;
using MySql.Data.MySqlClient;
using HardwarePerformance.Core.Entities;
using Microsoft.Extensions.Configuration;
namespace HardwarePerformance.Infrastructure.Data;
@@ -202,4 +203,63 @@ public class SimpleAppDbContext
cmd.CommandText = insertSpecifications;
await cmd.ExecuteNonQueryAsync();
}
public async Task<List<object>> GetCategoriesAsync()
{
var categories = new List<object>();
using var connection = new MySqlConnection(_connectionString);
await connection.OpenAsync();
using var cmd = new MySqlCommand("SELECT * FROM Categories", connection);
using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
categories.Add(new
{
Id = reader.GetInt32("Id"),
Name = reader.GetString("Name"),
Description = reader.IsDBNull("Description") ? null : reader.GetString("Description"),
CreatedAt = reader.GetDateTime("CreatedAt")
});
}
return categories;
}
public async Task<List<object>> GetProductsAsync(int categoryId = 0, int page = 1, int pageSize = 10)
{
var products = new List<object>();
using var connection = new MySqlConnection(_connectionString);
await connection.OpenAsync();
var offset = (page - 1) * pageSize;
var query = categoryId > 0
? $"SELECT p.*, c.Name as CategoryName FROM Products p LEFT JOIN Categories c ON p.CategoryId = c.Id WHERE p.CategoryId = {categoryId} LIMIT {pageSize} OFFSET {offset}"
: $"SELECT p.*, c.Name as CategoryName FROM Products p LEFT JOIN Categories c ON p.CategoryId = c.Id LIMIT {pageSize} OFFSET {offset}";
using var cmd = new MySqlCommand(query, connection);
using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
products.Add(new
{
Id = reader.GetInt32("Id"),
Name = reader.GetString("Name"),
Model = reader.IsDBNull("Model") ? null : reader.GetString("Model"),
Manufacturer = reader.IsDBNull("Manufacturer") ? null : reader.GetString("Manufacturer"),
ImageUrl = reader.IsDBNull("ImageUrl") ? null : reader.GetString("ImageUrl"),
ReleaseDate = reader.IsDBNull("ReleaseDate") ? (DateTime?)null : reader.GetDateTime("ReleaseDate"),
CategoryId = reader.IsDBNull("CategoryId") ? (int?)null : reader.GetInt32("CategoryId"),
CategoryName = reader.IsDBNull("CategoryName") ? null : reader.GetString("CategoryName"),
CurrentRank = reader.IsDBNull("CurrentRank") ? (int?)null : reader.GetInt32("CurrentRank"),
CreatedAt = reader.GetDateTime("CreatedAt")
});
}
return products;
}
}