using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using ToolHub.Models; using ToolHub.Services; namespace ToolHub.Controllers { public class HomeController : Controller { private readonly ILogger _logger; private readonly IToolService _toolService; public HomeController(ILogger logger, IToolService toolService) { _logger = logger; _toolService = toolService; } public async Task Index() { ViewBag.Categories = await _toolService.GetCategoriesAsync(); ViewBag.HotTools = await _toolService.GetHotToolsAsync(); ViewBag.NewTools = await _toolService.GetNewToolsAsync(); return View(); } public async Task Tools(int categoryId = 0, int page = 1, string? search = null) { ViewBag.Categories = await _toolService.GetCategoriesAsync(); ViewBag.CurrentCategory = categoryId; ViewBag.Search = search; List tools; if (!string.IsNullOrEmpty(search)) { tools = await _toolService.SearchToolsAsync(search, page); } else { tools = await _toolService.GetToolsByCategoryAsync(categoryId, page); } return View(tools); } public async Task Tool(int id) { var tool = await _toolService.GetToolByIdAsync(id); if (tool == null) { return NotFound(); } // 增加浏览次数 await _toolService.IncrementViewCountAsync(id); return View(tool); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }