网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Dpz.Core.EnumLibrary;
using Dpz.Core.Infrastructure;
using Dpz.Core.Public.Entity;

namespace Dpz.Core.Public.ViewModel;

/// <summary>
/// 评论 平铺
/// </summary>
public class VmCommentFlat : IHaveCustomMapping
{
    public string Id { get; set; }

    /// <summary>
    /// 评论类型
    /// </summary>
    public CommentNode Node { get; set; }

    /// <summary>
    /// 关联
    /// </summary>
    public string Relation { get; set; }

    /// <summary>
    /// 回复时间
    /// </summary>
    public DateTime PublishTime { get; set; }

    /// <summary>
    /// 回复内容
    /// </summary>
    public string CommentText { get; set; }

    /// <summary>
    /// 回复ID
    /// </summary>
    public List<string> Replies { get; set; }

    /// <summary>
    /// 昵称
    /// </summary>
    public string NickName { get; set; }

    /// <summary>
    /// 头像
    /// </summary>
    public string Avatar { get; set; }

    /// <summary>
    /// 身份标识
    /// </summary>
    public string Identity { get; set; }

    /// <summary>
    /// 邮箱
    /// </summary>
    public string Email { get; set; }

    /// <summary>
    /// 网站
    /// </summary>
    public string Site { get; set; }

    /// <summary>
    /// 是否匿名评论
    /// </summary>
    public bool IsGuest { get; set; }
    
    /// <summary>
    /// 是否删除
    /// </summary>
    public bool? IsDelete { get; set; }


    public void CreateMappings(MapperConfigurationExpression cfg)
    {
        cfg.CreateMap<Comment, VmCommentFlat>().ConvertUsing((entity, _) =>
        {
            var viewModel = new VmCommentFlat
            {
                Id = entity.Id.ToString(),
                Node = entity.Node,
                Relation = entity.Relation,
                PublishTime = entity.PublishTime.ToLocalTime(),
                CommentText = entity.CommentText,
                Replies = entity.Replies == null
                    ? new List<string>()
                    : entity.Replies.Select(x => x.ToString()).ToList(),
                NickName = entity.Commenter.NickName,
                IsDelete = entity.IsDelete
            };
            if (entity.Commenter is GuestCommenter guestCommenter)
            {
                viewModel.IsGuest = true;
                viewModel.Email = guestCommenter.Email;
                viewModel.Site = guestCommenter.Site;
            }
            else if (entity.Commenter is MembleCommenter membleCommenter)
            {
                viewModel.IsGuest = false;
                viewModel.Identity = membleCommenter.Identity;
                viewModel.Avatar = membleCommenter.Avatar;
            }
            return viewModel;
        });
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

上述代码定义了一个名为 VmCommentFlat 的视图模型类,主要用于表示评论的扁平化结构。这个类实现了 IHaveCustomMapping 接口,允许使用 AutoMapper 进行自定义映射。以下是代码的主要功能和组成部分的详细解释:

类的属性

  1. Id: 评论的唯一标识符。
  2. Node: 评论的类型,使用 CommentNode 枚举表示。
  3. Relation: 评论关联的对象或内容的标识。
  4. PublishTime: 评论的发布时间,类型为 DateTime
  5. CommentText: 评论的内容。
  6. Replies: 一个字符串列表,表示该评论的回复 ID。
  7. NickName: 评论者的昵称。
  8. Avatar: 评论者的头像 URL。
  9. Identity: 评论者的身份标识,通常用于识别注册用户。
  10. Email: 评论者的电子邮件地址。
  11. Site: 评论者的网站链接。
  12. IsGuest: 布尔值,指示评论者是否为匿名用户。
  13. IsDelete: 可空布尔值,指示评论是否已被删除。

CreateMappings 方法

CreateMappings 方法用于配置 AutoMapper 的映射规则,将 Comment 实体映射到 VmCommentFlat 视图模型。具体步骤如下:

  1. 创建映射: 使用 cfg.CreateMap<Comment, VmCommentFlat>() 创建从 CommentVmCommentFlat 的映射。
  2. 自定义转换: 使用 ConvertUsing 方法定义自定义转换逻辑。
  3. 映射属性: 在转换过程中,将 Comment 实体的属性映射到 VmCommentFlat 的相应属性。
    • Id 被转换为字符串。
    • PublishTime 被转换为本地时间。
    • Replies 列表被处理为字符串列表。
    • NickNameIsDelete 直接映射。
  4. 处理评论者类型: 根据评论者的类型(GuestCommenterMembleCommenter)设置不同的属性:
    • 如果评论者是 GuestCommenter,则设置 IsGuesttrue,并映射 EmailSite
    • 如果评论者是 MembleCommenter,则设置 IsGuestfalse,并映射 IdentityAvatar

总结

这个类的主要功能是将 Comment 实体转换为一个扁平化的视图模型 VmCommentFlat,以便在前端展示评论信息。通过使用 AutoMapper,代码实现了高效的属性映射和类型处理,简化了数据转换的过程。

loading