예제 #1
0
        /// <summary>
        /// 发布新帖子
        /// </summary>
        public async Task <Resp> PublishNewPostAsync(Models.NewPostInfo info)
        {
            (bool isValid, string msg) = info.IsValid();
            if (!isValid)
            {
                return(Resp.Fault(Resp.NONE, msg));
            }

            Content content = new Content(info.Content, info.Files);

            using var db    = new MyForContext();
            using var trans = db.Database.BeginTransaction();

            await content.UploadImagesAndSetInContent();

            DB.Tables.Post newPost = new DB.Tables.Post
            {
                Title      = info.Title,
                ThemeId    = Themes.Theme.GetIdByThemeName(info.Theme),
                Keyword    = info.Theme,
                CreateById = info.CreateById,
                State      = (int)Post.PostState.ToAudit
            };
            newPost.Content = content.ToString();

            db.Posts.Add(newPost);
            int suc = await db.SaveChangesAsync();

            if (suc == 1)
            {
                trans.Commit();
                return(Resp.Success(Resp.NONE));
            }
            return(Resp.Fault(Resp.NONE, "添加失败"));
        }
예제 #2
0
        public async Task <Resp> NewPostsAsync(Models.NewPostInfo info)
        {
            (bool isValid, string msg) = info.IsValid();
            if (!isValid)
            {
                return(Resp.Fault(Resp.NONE, msg));
            }

            File files = await File.SaveImageAsync(info.Img);

            DB.Tables.Post newPost = new DB.Tables.Post
            {
                CreateDate = DateTimeOffset.Now,
                Creator    = info.NickName,
                Content    = info.Content,
                ImageId    = files?.Id ?? File.DEFAULT_IMG_ID
            };
            using var db = new DB.DarkContext();
            db.Posts.Add(newPost);
            int suc = await db.SaveChangesAsync();

            if (suc == 1)
            {
                return(Resp.Success(Resp.NONE, "成功"));
            }
            return(Resp.Fault(Resp.NONE, "提交失败"));
        }
예제 #3
0
        public async Task <ActionResult> NewPostsAsync([FromForm] Models.NewPostInfo info)
        {
            Hub  hub = new Hub();
            Resp r   = await hub.NewPostsAsync(info);

            return(Pack(r));
        }
예제 #4
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, "发布失败");
        }