public void Update(IEnumerable <PostMetasInfo> infos, int postid) { using (MVCBlogContext Context = new MVCBlogContext()) { if (infos != null && infos.Count() > 0) { var relations = Context.PostMetaRelation.Where(x => x.PostId == postid).ToList(); foreach (PostMetasInfo info in infos) { if (info.Id != 0) { var exists = relations.FirstOrDefault(x => x.PostMetaId == info.Id); if (exists != null) { exists.IsDelete = false; } else { exists = new PostMetaRelation() { PostId = postid, PostMetaId = info.Id }; Context.PostMetaRelation.Add(exists); } Update(info); } else { var add = new PostMetasInfo() { Name = info.Name }; add = Context.PostMetasInfo.Add(add); Context.SaveChanges(); var relation = new PostMetaRelation() { PostId = postid, PostMetaId = add.Id }; Context.PostMetaRelation.Add(relation); } } var ids = infos.Select(s => s.Id); var removes = Context.PostMetaRelation.Where(x => x.PostId == postid && !ids.Contains(x.PostMetaId)).ToList(); foreach (var item in removes) { item.IsDelete = true; } Context.SaveChanges(); } } }
//private MVCBlogContext Context; //public PostMetaService(MVCBlogContext _context) //{ // this.Context = _context; //} public override void Delete(PostMetasInfo model) { using (MVCBlogContext Context = new MVCBlogContext()) { model.IsDelete = true; Context.SaveChanges(); } }
public override void Update(PostMetasInfo model) { using (MVCBlogContext Context = new MVCBlogContext()) { var entity = Context.PostMetasInfo.Find(model.Id); entity.Name = model.Name; Context.SaveChanges(); } }
public override void Delete(UserInfo model) { using (MVCBlogContext Context = new MVCBlogContext()) { var entity = Context.UserInfo.Find(model.Id); entity.IsDelete = true; Context.SaveChanges(); base.Delete(model); } }
public override void Insert(CommentInfo model, int userid = 0) { using (MVCBlogContext Context = new MVCBlogContext()) { model.CommentUser = Context.UserInfo.Find(model.CommentUser.Id); Context.CommentInfo.Add(model); Context.SaveChanges(); base.Insert(model, userid); } }
//public override IEnumerable<CommentInfo> Query(Expression<Func<CommentInfo, bool>> query = null) //{ // var ids = query != null ? Context.CommentInfo.Where(query).OrderByDescending(x => x.Id).Select(x => x.Id).ToList() : Context.CommentInfo.OrderByDescending(x => x.Id).Select(x => x.Id).ToList(); // if (ids != null && ids.Count > 0) // { // return GetByIds(ids); // } // else // { // return new List<CommentInfo>(); // } //} //public override Pagination<CommentInfo> Query(int index, int pagecount, Expression<Func<CommentInfo, bool>> query = null) //{ // var ids = query != null ? Context.CommentInfo.Where(query).OrderByDescending(x => x.Id).Select(x => x.Id).ToPagedList(index, pagecount) : Context.CommentInfo.OrderByDescending(x => x.Id).Select(x => x.Id).ToPagedList(index, pagecount); // if (ids != null && ids.Count() > 0) // { // Pagination<CommentInfo> pagination = new Pagination<CommentInfo>() // { // Items = GetByIds(ids), // TotalItemCount = ids.TotalItemCount, // PageCount = ids.PageCount, // PageNumber = ids.PageNumber, // PageSize = ids.PageSize // }; // return pagination; // } // else // { // return new Pagination<CommentInfo>() // { // Items = null, // TotalItemCount = 0, // PageCount = 0, // PageNumber = index, // PageSize = pagecount // }; // } //} public override void Update(CommentInfo model) { using (MVCBlogContext Context = new MVCBlogContext()) { var entity = Context.CommentInfo.Find(model.Id); entity.CommentContent = model.CommentContent; Context.SaveChanges(); base.Update(model); } }
//public override async Task<CategoryInfo> GetByIdAsync(int id) //{ // Func<CategoryInfo> getitem = () => // { // var list = RedisHelper.GetEntity<List<CategoryInfo>>(RedisKeyHelper.GetCategoryKey(id)); // if (list != null) // { // return list.Find(x => x.Id == id); // } // return Context.CategoryInfo.Find(id); // }; // return await Common.TaskExtensions.WithCurrentCulture<CategoryInfo>(getitem); //} public override void Insert(CategoryInfo model, int userid = 0) { using (MVCBlogContext Context = new MVCBlogContext()) { model.CreateUser = Context.UserInfo.Find(userid == 0 ? model.CreateUser.Id : userid); Context.CategoryInfo.Add(model); Context.SaveChanges(); base.Insert(model, userid); } }
public void Insert(IEnumerable <PostMetasInfo> infos, int postid) { using (MVCBlogContext Context = new MVCBlogContext()) { //throw new NotImplementedException(); if (infos != null) { foreach (PostMetasInfo info in infos) { if (info.Id != 0) { var relation = Context.PostMetaRelation.FirstOrDefault(x => x.PostId == postid && x.PostMetaId == info.Id); if (relation != null) { relation.IsDelete = false; } else { relation = new PostMetaRelation() { PostId = postid, PostMetaId = info.Id }; Context.PostMetaRelation.Add(relation); } } else { var entity = Context.PostMetasInfo.Add(info); Context.SaveChanges(); var relation = new PostMetaRelation() { PostId = postid, PostMetaId = entity.Id }; Context.PostMetaRelation.Add(relation); } } Context.SaveChanges(); } } }
//private MVCBlogContext Context; //public PostService(MVCBlogContext _context) //{ // this.Context = _context; //} public override void Delete(PostInfo model) { using (MVCBlogContext Context = new MVCBlogContext()) { if (model != null) { var entity = Context.PostInfo.Find(model.Id); entity.IsDelete = true; entity.PostStatus = PostStatus.除; Context.SaveChanges(); base.Delete(model); } } }
public override void Update(CategoryInfo model) { using (MVCBlogContext Context = new MVCBlogContext()) { var entity = Context.CategoryInfo.Find(model.Id); if (entity != null) { entity.CategoryName = model.CategoryName; entity.IsDelete = model.IsDelete; Context.SaveChanges(); base.Update(model); } } }
public override void Insert(UserInfo user, int userid = 0) { using (MVCBlogContext Context = new MVCBlogContext()) { user.Password = AesSecret.EncryptStringToAES(user.Password); user.CreateTime = DateTime.Now; user.EditedTime = DateTime.Now; user.UserStatus = UserStatus.正常; user.UserRole = UserRole.读者; user.IsDelete = false; user = Context.UserInfo.Add(user); Context.SaveChanges(); base.Insert(user, userid); } }
public UserInfo ValidateUser(string email, string password) { using (MVCBlogContext Context = new MVCBlogContext()) { string encryptPassword = AesSecret.EncryptStringToAES(password); var entity = Context.UserInfo.FirstOrDefault(x => x.Email == email && x.Password == encryptPassword); if (entity != null) { entity.LastLoginTime = DateTime.Now; Context.SaveChanges(); entity = Context.UserInfo.Find(entity.Id); return(entity); } return(null); } }
public override void Update(PostInfo model) { using (MVCBlogContext Context = new MVCBlogContext()) { var entity = Context.PostInfo.Find(model.Id); entity.CommentCount = model.CommentCount; entity.Title = model.Title; entity.Content = model.Content; entity.PostStatus = model.PostStatus; entity.PostCommentStatus = model.PostCommentStatus; entity.EditedTime = DateTime.Now; entity.PostCategoryInfo = Context.CategoryInfo.Find(entity.PostCategoryInfo.Id); Context.SaveChanges(); IPostMetaService metaservice = new PostMetaService(); metaservice.Update(model.PostMetasInfos, model.Id); base.Update(model); } }
public override async Task InsertAsync(PostInfo model, int userid) { using (MVCBlogContext Context = new MVCBlogContext()) { model.PostStatus = PostStatus.发布; model.PostType = PostType.文章; model.PostCommentStatus = PostCommentStatus.打开; model.CommentCount = 0; model.CreateTime = DateTime.Now; model.IsDelete = false; model.PostAuthor = Context.UserInfo.Find(userid); model.PostCategoryInfo = Context.CategoryInfo.Find(model.PostCategoryInfo.Id); var entity = Context.PostInfo.Add(model); Context.SaveChanges(); PostMetaService metaservice = new PostMetaService(); metaservice.Insert(model.PostMetasInfos, entity.Id); await base.InsertAsync(model, userid); } }
public override void Update(UserInfo model) { using (MVCBlogContext Context = new MVCBlogContext()) { var entity = Context.UserInfo.Find(model.Id); entity.EditedTime = DateTime.Now; entity.Name = model.Name; entity.Password = model.Password; entity.UserRole = model.UserRole; entity.UserStatus = model.UserStatus; entity.WeiBoAccessToken = model.WeiBoAccessToken; entity.WeiBoUid = model.WeiBoUid; entity.WeiBoAvator = model.WeiBoAvator; entity.QQAccessToken = model.QQAccessToken; entity.QQAvator = model.QQAvator; entity.QQUid = model.QQUid; Context.SaveChanges(); base.Update(model); } }