2025-11-03 17:03:57 +08:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using HardwarePerformance.Infrastructure.Data;
|
|
|
|
|
|
|
|
|
|
namespace HardwarePerformance.API.Controllers;
|
|
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class TestController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly SimpleAppDbContext _context;
|
|
|
|
|
private readonly ILogger<TestController> _logger;
|
|
|
|
|
|
|
|
|
|
public TestController(SimpleAppDbContext context, ILogger<TestController> logger)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("database-status")]
|
|
|
|
|
public async Task<IActionResult> GetDatabaseStatus()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var isConnected = await _context.TestConnectionAsync();
|
|
|
|
|
|
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
IsConnected = isConnected,
|
|
|
|
|
Message = isConnected ? "数据库连接成功" : "数据库连接失败",
|
|
|
|
|
Timestamp = DateTime.Now
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "检查数据库连接时发生错误");
|
|
|
|
|
return StatusCode(500, new
|
|
|
|
|
{
|
|
|
|
|
IsConnected = false,
|
|
|
|
|
Message = $"检查数据库连接时发生错误: {ex.Message}",
|
|
|
|
|
Timestamp = DateTime.Now
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("initialize-database")]
|
|
|
|
|
public async Task<IActionResult> InitializeDatabase()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _context.InitializeDatabaseAsync();
|
|
|
|
|
await _context.SeedInitialDataAsync();
|
|
|
|
|
|
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
Message = "数据库初始化成功,种子数据已添加",
|
|
|
|
|
Timestamp = DateTime.Now
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "初始化数据库时发生错误");
|
|
|
|
|
return StatusCode(500, new
|
|
|
|
|
{
|
|
|
|
|
Message = $"初始化数据库时发生错误: {ex.Message}",
|
|
|
|
|
Timestamp = DateTime.Now
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("categories")]
|
2025-11-03 19:47:36 +08:00
|
|
|
public async Task<IActionResult> GetCategories()
|
2025-11-03 17:03:57 +08:00
|
|
|
{
|
2025-11-03 19:47:36 +08:00
|
|
|
try
|
2025-11-03 17:03:57 +08:00
|
|
|
{
|
2025-11-03 19:47:36 +08:00
|
|
|
var categories = await _context.GetCategoriesAsync();
|
|
|
|
|
return Ok(new { success = true, data = categories });
|
2025-11-03 17:03:57 +08:00
|
|
|
}
|
2025-11-03 19:47:36 +08:00
|
|
|
catch (Exception ex)
|
2025-11-03 17:03:57 +08:00
|
|
|
{
|
2025-11-03 19:47:36 +08:00
|
|
|
_logger.LogError(ex, "获取类别列表时发生错误");
|
|
|
|
|
return StatusCode(500, new { success = false, message = ex.Message });
|
|
|
|
|
}
|
2025-11-03 17:03:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("products")]
|
2025-11-03 19:47:36 +08:00
|
|
|
public async Task<IActionResult> GetProducts([FromQuery] int categoryId = 0, [FromQuery] int page = 1, [FromQuery] int pageSize = 10)
|
2025-11-03 17:03:57 +08:00
|
|
|
{
|
2025-11-03 19:47:36 +08:00
|
|
|
try
|
2025-11-03 17:03:57 +08:00
|
|
|
{
|
2025-11-03 19:47:36 +08:00
|
|
|
var products = await _context.GetProductsAsync(categoryId, page, pageSize);
|
|
|
|
|
return Ok(new { success = true, data = products });
|
2025-11-03 17:03:57 +08:00
|
|
|
}
|
2025-11-03 19:47:36 +08:00
|
|
|
catch (Exception ex)
|
2025-11-03 17:03:57 +08:00
|
|
|
{
|
2025-11-03 19:47:36 +08:00
|
|
|
_logger.LogError(ex, "获取产品列表时发生错误");
|
|
|
|
|
return StatusCode(500, new { success = false, message = ex.Message });
|
|
|
|
|
}
|
2025-11-03 17:03:57 +08:00
|
|
|
}
|
|
|
|
|
}
|