/// <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, "添加失败")); }
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, "提交失败")); }
/// <summary> /// 获取当前帖子的详情 /// </summary> /// <returns></returns> public async Task <Resp> GetDetail() { if (IsEmpty()) { return(Resp.Fault(Resp.NONE, NONE_POST)); } using var db = new MyForContext(); DB.Tables.Post post = await db.Posts.AsNoTracking() .Include(p => p.Theme) //.Include(p => p.Comments) .FirstOrDefaultAsync(p => p.Id == Id); if (post is null) { return(Resp.Fault(Resp.NONE, NONE_POST)); } Results.PostDetail detail = new Results.PostDetail { Title = post.Title, Theme = post.Theme.Name, Content = post.Content, Likes = post.Likes, State = new Share.KeyValue { Key = post.State, Value = ((PostState)post.State).GetDescription() }, Creator = Clients.Client.GetName(post.CreateById), CreateDate = post.CreateDate.ToStandardString(), //Comments = post.Comments.Select(c => new Results.Comment //{ // Id = c.Id, // NickName = Clients.Client.GetName(c.CreateById), // Date = c.CreateDate.ToStandardString(), // CommentContent = c.Comment //}).ToList() }; List <Results.Comment> comments = await db.PostComments.AsNoTracking() .Where(c => c.PostId == Id) .Take(10) .Select(c => new Results.Comment { Id = c.Id, NickName = Clients.Client.GetName(c.CreateById), Date = c.CreateDate.ToStandardString(), CommentContent = c.Comment }).ToListAsync(); detail.Comments = comments; return(Resp.Success(detail, "")); }
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); }
public static string GetName(int id) { string key = $"45abce59-96b8-4785-879f-f04d2663c037_{id}"; string name = Cache.Get <string>(key); if (name is null) { using var db = new MyForContext(); DB.Tables.Post post = db.Posts.AsNoTracking() .FirstOrDefault(t => t.Id == id); name = post?.Title ?? ""; _ = Cache.Set(key, name, 10); } return(name); }
/// <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, "发布失败"); }
/// <summary> /// 启用管理员 /// </summary> /// <param name="description">启用理由</param> /// <returns></returns> public async Task <Resp> EnabledAsync(string description) { using var db = new MyForContext(); DB.Tables.Post account = await db.Posts.FirstOrDefaultAsync(c => c.Id == Id); if (account is null) { return(Resp.Fault(Resp.NONE, NONE_POST)); } account.State = (int)PostState.Enabled; int suc = await db.SaveChangesAsync(); if (suc == 1) { return(Resp.Success()); } return(Resp.Fault(Resp.NONE, "启用失败")); }