Esempio n. 1
0
        /// <summary>
        /// 按照发布顺序获取帖子列表
        /// </summary>
        /// <param name="pager"></param>
        /// <returns></returns>
        public async Task <Posts_Result> GetPostsAsync(Paginator pager)
        {
            using ATXDbContext db = new ATXDbContext();

            pager.TotalRows = await db.Posts.CountAsync(p => p.State == (int)ShareEnum.State.Enabled);

            List <Posts_Result.PostItem> list = await db.Posts.AsNoTracking()
                                                .Where(p => p.State == (int)ShareEnum.State.Enabled)
                                                .Include(p => p.File)
                                                .Skip(pager.GetSkip())
                                                .Take(pager.Rows)
                                                .OrderByDescending(p => p.CreateDate)
                                                .Select(o => new Posts_Result.PostItem
            {
                Id              = o.Id,
                Author          = o.Author,
                Description     = o.Description,
                SourceFilePath  = o.File.Path,
                ThumbnailPath   = o.File.Thumbnail,
                PublishDateTime = o.CreateDate.ToString("yyyy-MM-dd"),
                Likes           = o.Likes
            })
                                                .ToListAsync();

            Posts_Result result = new Posts_Result
            {
                List = list
            };

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 今日的访问记录增加一
        /// </summary>
        public static async Task TodayVisitRecordIncreaseAsync()
        {
            DateTimeOffset todayStart, todayEnd;

            (todayStart, todayEnd) = DateTimeOffset.Now.GetStartAndEnd();

            using ATXDbContext db = new ATXDbContext();

            DB.Tables.Record recordModel = await db.Records
                                           .Where(r => r.CreateDate >= todayStart && r.CreateDate <= todayEnd)
                                           .FirstOrDefaultAsync();

            if (recordModel is null)
            {
                recordModel = new DB.Tables.Record
                {
                    VisitCount = 1
                };
                _ = db.Records.Add(recordModel);
            }
            else
            {
                recordModel.VisitCount++;
            }

            _ = await db.SaveChangesAsync();
        }
Esempio n. 3
0
        public async Task <Posts_Result> GetPostsAsync(Paginator pager)
        {
            /*
             *  随机获取帖子
             *  分页获取
             *  index: 当前页码
             *  rows: 每页行数
             */

            using ATXDbContext db = new ATXDbContext();

            pager.TotalRows = await db.Posts.CountAsync(p => p.State == (int)ShareEnum.State.Enabled);

            List <Posts_Result.PostItem> list = await db.Posts
                                                .FromSqlRaw($"SELECT * FROM POSTS WHERE STATE = {(int)ShareEnum.State.Enabled} ORDER BY RAND() LIMIT {pager.Rows}")
                                                .Select(o => new Posts_Result.PostItem
            {
                Id              = o.Id,
                Author          = o.Author,
                Description     = o.Description,
                Type            = o.File.Type,
                SourceFilePath  = o.File.Path,
                ThumbnailPath   = o.File.Thumbnail,
                PublishDateTime = o.CreateDate.ToString("yyyy-MM-dd"),
                Likes           = o.Likes
            })
                                                .ToListAsync();

            //List<Posts_Result.PostItem> list = await db.Posts.AsNoTracking()
            //    .Where(p => p.State == (int)ShareEnum.State.Enabled)
            //    .OrderBy(p => Guid.NewGuid())
            //    .Take(pager.Rows)
            //    .Select(o => new Posts_Result.PostItem
            //    {
            //        Id = o.Id,
            //        Author = o.Author,
            //        Description = o.Description,
            //        Type = o.File.Type,
            //        SourceFilePath = o.File.Path,
            //        ThumbnailPath = o.File.Thumbnail,
            //        PublishDateTime = o.CreateDate.ToString("yyyy-MM-dd"),
            //        Likes = o.Likes
            //    })
            //    .ToListAsync();

            Posts_Result result = new Posts_Result
            {
                List = list
            };

            return(result);
        }
Esempio n. 4
0
        public async Task <bool> Like(int id)
        {
            using ATXDbContext db = new ATXDbContext();

            DB.Tables.Post model = await db.Posts.FirstOrDefaultAsync(p => p.Id == id);

            if (model is null)
            {
                return(false);
            }
            model.Likes++;
            int suc = await db.SaveChangesAsync();

            return(suc == 1);
        }
Esempio n. 5
0
        /// <summary>
        /// 发布新帖
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <(bool, string)> PulishNewPostAsync(Models.NewPostInfo model)
        {
            bool   suc = true;
            string msg = "";

            /*
             *  先保存文件
             *  会保存在数据库的 Files 表
             *  返回的 fileId 为保存的 ID
             */
            Files.File file = new Files.File();
            if (model.File is null)
            {
                return(false, "需要上传文件");
            }

            int fileId = await file.SaveFileToDBAsync(model.File);

            if (fileId == Files.File.NOT_FILES)
            {
                return(false, "文件上传失败");
            }

            using ATXDbContext db = new ATXDbContext();

            DB.Tables.Post newPost = new DB.Tables.Post
            {
                Author      = model.Author,
                Description = model.Description,
                FileId      = fileId
            };
            db.Posts.Add(newPost);

            //  添加今天的一条发布记录
            await Records.Records.TodayPulishRecordIncreaseAsync();

            if (await db.SaveChangesAsync() == 1)
            {
                return(suc, msg);
            }
            return(false, "发布失败");
        }