File size: 2,186 Bytes
5fc700d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 });
        }
    }
}