|
|
using System.Diagnostics; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using ToolHub.Models; |
|
|
using ToolHub.Services; |
|
|
|
|
|
namespace ToolHub.Controllers |
|
|
{ |
|
|
public class HomeController : Controller |
|
|
{ |
|
|
private readonly ILogger<HomeController> _logger; |
|
|
private readonly IToolService _toolService; |
|
|
|
|
|
public HomeController(ILogger<HomeController> logger, IToolService toolService) |
|
|
{ |
|
|
_logger = logger; |
|
|
_toolService = toolService; |
|
|
} |
|
|
|
|
|
public async Task<IActionResult> Index() |
|
|
{ |
|
|
ViewBag.Categories = await _toolService.GetCategoriesAsync(); |
|
|
ViewBag.HotTools = await _toolService.GetHotToolsAsync(); |
|
|
ViewBag.NewTools = await _toolService.GetNewToolsAsync(); |
|
|
|
|
|
return View(); |
|
|
} |
|
|
|
|
|
public async Task<IActionResult> Tools(int categoryId = 0, int page = 1, string? search = null) |
|
|
{ |
|
|
ViewBag.Categories = await _toolService.GetCategoriesAsync(); |
|
|
ViewBag.CurrentCategory = categoryId; |
|
|
ViewBag.Search = search; |
|
|
|
|
|
List<Tool> tools; |
|
|
if (!string.IsNullOrEmpty(search)) |
|
|
{ |
|
|
tools = await _toolService.SearchToolsAsync(search, page); |
|
|
} |
|
|
else |
|
|
{ |
|
|
tools = await _toolService.GetToolsByCategoryAsync(categoryId, page); |
|
|
} |
|
|
|
|
|
return View(tools); |
|
|
} |
|
|
|
|
|
public async Task<IActionResult> 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 }); |
|
|
} |
|
|
} |
|
|
} |
|
|
|