using Dpz.Core.EnumLibrary;
using Dpz.Core.Public.ViewModel.Request;

namespace Dpz.Core.Web.Controllers;

public class TimelineController(ITimelineService timelineService) : Controller
{
    [Route("timeline")]
    [HttpGet]
    public async Task<IActionResult> Index(
        int pageIndex = 1,
        int pageSize = 10,
        CancellationToken cancellationToken = default
    )
    {
        ViewBag.Menu = MenuItem.Timeline;
        this.SetTitle("时间轴");
        ViewBag.Account = "pengqian";
        var pagedList = await timelineService.GetPageAsync(
            new TimelinePageRequest
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Account = "pengqian",
            },
            cancellationToken: cancellationToken
        );
        return View(pagedList);
    }

    [HttpGet]
    public async Task<IActionResult> LoadMore(
        string account,
        int pageIndex = 1,
        int pageSize = 10,
        CancellationToken cancellationToken = default
    )
    {
        var pagedList = await timelineService.GetPageAsync(
            new TimelinePageRequest
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Account = account,
            },
            cancellationToken: cancellationToken
        );
        return PartialView("_TimelineItemsPartial", pagedList);
    }

    [Route("timeline/{account}.html")]
    public async Task<IActionResult> Timeline2(
        string account,
        int pageIndex = 1,
        int pageSize = 10,
        CancellationToken cancellationToken = default
    )
    {
        ViewBag.Menu = MenuItem.Timeline;
        this.SetTitle($"{account} - 时间轴");
        ViewBag.Account = account;
        var pagedList = await timelineService.GetPageAsync(
            new TimelinePageRequest
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Account = account,
            },
            cancellationToken: cancellationToken
        );
        return View("Index", pagedList);
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

代码解释

这是一个基于 ASP.NET Core MVC 的控制器类,用于处理时间轴(Timeline)相关的 HTTP 请求。

类结构

public class TimelineController(ITimelineService timelineService) : Controller
  • 使用了 C# 12 的主构造函数语法,通过依赖注入获取 ITimelineService 服务
  • 继承自 Controller 基类,提供 MVC 控制器的基础功能

方法详解

1. Index 方法

[Route("timeline")]
[HttpGet]
public async Task<IActionResult> Index(...)

功能:时间轴首页的主入口

  • 路由GET /timeline

  • 参数

    • pageIndex:当前页码(默认为 1)
    • pageSize:每页条数(默认为 10)
    • cancellationToken:用于取消异步操作
  • 逻辑

    1. 设置菜单项为时间轴
    2. 设置页面标题为"时间轴"
    3. 硬编码账户为 "pengqian"(可能是默认账户)
    4. 调用服务层获取分页数据
    5. 返回视图和数据

2. LoadMore 方法

[HttpGet]
public async Task<IActionResult> LoadMore(...)

功能:AJAX 加载更多内容

  • 参数

    • account:账户名称(必需)
    • pageIndex:页码
    • pageSize:每页条数
  • 逻辑

    1. 根据传入的账户名和分页参数获取数据
    2. 返回部分视图 _TimelineItemsPartial(用于 AJAX 追加加载)

3. Timeline2 方法

[Route("timeline/{account}.html")]
public async Task<IActionResult> Timeline2(...)

功能:特定用户的时间轴页面

  • 路由GET /timeline/{account}.html(如:/timeline/john.html

  • 参数

    • account:从 URL 路径中获取的账户名
    • 其他分页参数同上
  • 逻辑

    1. 设置菜单和动态标题(包含账户名)
    2. 将账户名传递给视图
    3. 获取指定账户的时间轴数据
    4. 复用 Index 视图进行展示

设计特点

  1. 异步处理:所有方法都使用 async/await 模式,支持高并发
  2. 取消令牌:支持请求取消,提升性能
  3. 视图复用Timeline2 复用了 Index 视图
  4. 部分视图LoadMore 使用部分视图实现无刷新加载
  5. SEO 友好:使用 .html 后缀的友好 URL

可能的改进点

  • Index 方法中硬编码的 "pengqian" 账户应该从配置或用户上下文中获取
  • 可以添加错误处理和日志记录
  • 可以添加授权验证
评论加载中...