using Dpz.Core.EnumLibrary;
using Dpz.Core.Public.ViewModel.Request;
using Dpz.Core.Service.ObjectStorage.Services;
using Dpz.Core.Web.Library.Hub;
using Microsoft.AspNetCore.SignalR;

namespace Dpz.Core.Web.Controllers;

public class MumbleController(
    IMumbleService mumbleService,
    IHubContext<Notification> hubContext,
    IObjectStorageOperation objectStorageService
) : Controller
{
    public async Task<IActionResult> Index(int pageIndex = 1, int pageSize = 10)
    {
        this.SetTitle("日常碎碎念");
        ViewBag.Menu = MenuItem.Mumble;
        var list = await mumbleService.GetPagesAsync(pageIndex, pageSize);
        var data = await mumbleService.GetRandomMumblesAsync(5);
        return View(new MumbleIndexModel(list, data));
    }

    [CheckAuthorize]
    [HttpPost]
    public async Task<IActionResult> Publish(string markdown, string id = "")
    {
        var userInfo = User.RequiredUserInfo;

        if (!string.IsNullOrEmpty(id))
        {
            var talk = await mumbleService.FindAsync(id);
            if (userInfo.Id != talk?.Author.Id)
            {
                return Json(new ResultInfo("没有权限修改碎碎念!"));
            }
        }

        if (string.IsNullOrEmpty(id))
        {
            var createRequest = new CreateMumbleRequest
            {
                Author = userInfo,
                CreateTime = DateTime.Now,
                LastUpdateTime = DateTime.Now,
                Markdown = markdown,
            };
            var mumble = await mumbleService.CreateAsync(createRequest);
            await hubContext.Clients.All.SendCoreAsync("pushMessage", [mumble]);
        }
        else
        {
            var editRequest = new EditMumbleRequest { Id = id, Markdown = markdown };
            await mumbleService.EditContentAsync(editRequest);
        }

        return Json(new ResultInfo(true));
    }

    [HttpPost]
    public async Task<IActionResult> Like(string id)
    {
        var talk = await mumbleService.LikeAsync(id);
        return Json(new ResultInfo(talk?.Like));
    }

    [CheckAuthorize]
    [HttpPost]
    public async Task<IActionResult> Upload()
    {
        var file = Request.Form.Files.FirstOrDefault();
        if (file is { Length: > 0 } && file.ContentType.Contains("image"))
        {
            //var userInfo = User.GetIdentity();
            var path = new List<string> { "images", "mumble", DateTime.Now.ToString("yyyy-MM-dd") };
#if DEBUG
            path.Insert(0, "Test");
#endif
            var fileName =
                ObjectId.GenerateNewId() + file.FileName[file.FileName.LastIndexOf('.')..];
            var result = await objectStorageService.UploadAsync(
                file.OpenReadStream(),
                path,
                fileName
            );
            return Json(
                new
                {
                    success = 1,
                    message = "",
                    url = result.AccessUrl,
                }
            );
        }

        return Json(
            new
            {
                success = 0,
                message = "请选择一张图片!",
                url = "",
            }
        );
    }

    [HttpPost, CheckAuthorize]
    public async Task<IActionResult> Delete(string id)
    {
        var talk = await mumbleService.FindAsync(id);
        if (talk == null || User.RequiredUserId != talk.Author.Id)
        {
            return Json(new ResultInfo("碎碎念删除失败,没有权限删除或者不存在!"));
        }

        await mumbleService.DeleteAsync(id);
        return Json(new ResultInfo(true));
    }

    [HttpGet]
    public async Task<IActionResult> Comment(string id = "")
    {
        var talk = await mumbleService.FindAsync(id);
        if (talk == null)
        {
            return NotFound();
        }

        this.SetTitle("碎碎念回复");
        ViewBag.Menu = MenuItem.Mumble;
        return View(talk);
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

MumbleController 功能解析

这个控制器是处理"碎碎念"(类似微博或推文)相关功能的ASP.NET Core控制器。以下是它的主要功能:

1. 页面显示功能

  • Index:
    • 显示碎碎念列表页
    • 设置页面标题为"日常碎碎念"
    • 获取分页的碎碎念列表(默认每页10条)
    • 获取5条随机碎碎念
    • 返回包含这些数据的视图

2. 碎碎念操作功能

  • Publish (需要授权):

    • 发布或编辑碎碎念
    • 如果是编辑操作,检查用户是否有权限
    • 创建新碎碎念时会通过SignalR通知所有客户端
    • 返回操作结果(JSON格式)
  • Like:

    • 点赞功能
    • 返回更新后的点赞数(JSON格式)
  • Delete (需要授权):

    • 删除碎碎念
    • 检查用户是否有权限删除
    • 返回操作结果(JSON格式)

3. 文件上传功能

  • Upload (需要授权):
    • 上传图片到对象存储
    • 检查文件类型和大小
    • 按日期组织存储路径
    • 生成唯一文件名
    • 返回上传结果(包含访问URL)

4. 评论功能

  • Comment:
    • 显示单条碎碎念的评论页
    • 设置页面标题为"碎碎念回复"
    • 如果碎碎念不存在则返回404

技术特点

  1. 使用了依赖注入的服务:

    • IMumbleService: 处理碎碎念业务逻辑
    • IHubContext<Notification>: 用于实时通知
    • IObjectStorageOperation: 处理文件存储
  2. 授权检查:

    • 使用[CheckAuthorize]特性保护敏感操作
  3. 文件处理:

    • 支持图片上传
    • 自动组织存储路径
    • 开发环境有特殊路径前缀
  4. 实时功能:

    • 发布新碎碎念时会通过SignalR推送给所有客户端

这个控制器实现了一个完整的微型博客系统的基本功能,包括发布、编辑、删除、点赞、评论和图片上传等。

评论加载中...