示例#1
0
        /// <summary>
        /// 获取留言
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public async Task <ActionResult> GetMsgs([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15, int cid = 0)
        {
            if (cid != 0)
            {
                var message = await LeaveMessageService.GetByIdAsync(cid) ?? throw new NotFoundException("留言未找到");

                var single = new[] { message.Root() };
                foreach (var m in single.Flatten())
                {
                    m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                    if (!CurrentUser.IsAdmin)
                    {
                        m.Email    = null;
                        m.IP       = null;
                        m.Location = null;
                    }
                }

                return(ResultData(new
                {
                    total = 1,
                    parentTotal = 1,
                    page,
                    size,
                    rows = single.Mapper <IList <LeaveMessageViewModel> >()
                }));
            }

            var parent = await LeaveMessageService.GetPagesAsync(page, size, m => m.ParentId == 0 && (m.Status == Status.Published || CurrentUser.IsAdmin), m => m.PostDate, false);

            if (!parent.Data.Any())
            {
                return(ResultData(null, false, "没有留言"));
            }
            var total = parent.TotalCount;

            parent.Data.Flatten().ForEach(m =>
            {
                m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                if (!CurrentUser.IsAdmin)
                {
                    m.Email    = null;
                    m.IP       = null;
                    m.Location = null;
                }
            });
            if (total > 0)
            {
                return(ResultData(new
                {
                    total,
                    parentTotal = total,
                    page,
                    size,
                    rows = Mapper.Map <List <LeaveMessageViewModel> >(parent.Data)
                }));
            }

            return(ResultData(null, false, "没有留言"));
        }
示例#2
0
        public async Task <ActionResult> GetPendingMsgs([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var list = await LeaveMessageService.GetPagesAsync <DateTime, LeaveMessageDto>(page, size, m => m.Status == Status.Pending, l => l.PostDate, false);

            foreach (var m in list.Data)
            {
                m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
            }

            return(Ok(list));
        }
示例#3
0
        /// <summary>
        /// 获取留言
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="cid"></param>
        /// <returns></returns>
        public async Task <ActionResult> GetMsgs([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15, int?cid = null)
        {
            if (cid > 0)
            {
                var message = await LeaveMessageService.GetByIdAsync(cid.Value) ?? throw new NotFoundException("留言未找到");

                using var layer = LeaveMessageService.GetQueryNoTracking(e => e.GroupTag == message.GroupTag).ToPooledList();
                foreach (var m in layer)
                {
                    m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                    if (!CurrentUser.IsAdmin)
                    {
                        m.Email    = null;
                        m.IP       = null;
                        m.Location = null;
                    }
                }

                return(ResultData(new
                {
                    total = 1,
                    parentTotal = 1,
                    page,
                    size,
                    rows = layer.ToTree(e => e.Id, e => e.ParentId).Mapper <IList <LeaveMessageViewModel> >()
                }));
            }

            var parent = await LeaveMessageService.GetPagesAsync(page, size, m => m.ParentId == null && (m.Status == Status.Published || CurrentUser.IsAdmin), m => m.PostDate, false);

            if (!parent.Data.Any())
            {
                return(ResultData(null, false, "没有留言"));
            }
            var total = parent.TotalCount;
            var tags  = parent.Data.Select(c => c.GroupTag).ToArray();

            using var messages = LeaveMessageService.GetQueryNoTracking(c => tags.Contains(c.GroupTag)).ToPooledList();
            messages.ForEach(m =>
            {
                m.PostDate = m.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone));
                if (!CurrentUser.IsAdmin)
                {
                    m.Email    = null;
                    m.IP       = null;
                    m.Location = null;
                }
            });
            if (total > 0)
            {
                return(ResultData(new
                {
                    total,
                    parentTotal = total,
                    page,
                    size,
                    rows = messages.OrderByDescending(c => c.PostDate).ToTree(c => c.Id, c => c.ParentId).Mapper <IList <LeaveMessageViewModel> >()
                }));
            }

            return(ResultData(null, false, "没有留言"));
        }