Пример #1
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();
        }
Пример #2
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);
        }
Пример #3
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, "发布失败");
        }