public List <Types.Post> filterPosts( int page, int?userId = null, string headline = null, DateTime?date = null) { using (var ctx = new Models.Model()) { List <Types.Post> result = new List <Types.Post>(); var posts = ctx.posts.AsQueryable(); try { if (userId != null) { posts = posts.Where(x => x.user.userId == userId); } if (headline != null) { posts = posts.Where(x => x.headline.Contains(headline)); } if (date != null) { posts = posts.Where(x => (DateTime.Compare((DateTime)date, x.time) <= 0)); } posts = posts.OrderByDescending(p => p.time).Skip((page - 1) * 10).Take(10); foreach (var p in posts.ToList()) { var p1 = new Types.Post(); p1.postId = p.postId; p1.headline = p.headline; p1.time = p.time; p1.description = p.description; p1.upvotes = p.upvotes; p1.user = new Types.User(); p1.user.userId = p.user.userId; p1.user.username = p.user.username; p1.user.firstName = p.user.firstName; p1.user.lastName = p.user.lastName; p1.user.password = p.user.password; result.Add(p1); } } catch (Exception ex) { Debug.WriteLine("he"); Debug.WriteLine(ex); Debug.WriteLine("he"); } return(result); } }
public Types.Post updatePost(Types.Post p) { using (var ctx = new Models.Model()) { Models.Post p1 = ctx.posts.FirstOrDefault(x => x.postId == p.postId); p1.headline = p.headline; p1.time = p.time; p1.description = p.description; p1.upvotes = p.upvotes; ctx.SaveChanges(); return(p); } }
public int addPost(Types.Post p) { using (var ctx = new Models.Model()) { Models.Post p1 = new Models.Post(); p1.headline = p.headline; p1.time = p.time; p1.description = p.description; p1.upvotes = p.upvotes; Models.User u = ctx.users.FirstOrDefault(x => x.userId == p.user.userId); p1.user = u; ctx.posts.Add(p1); ctx.SaveChanges(); return(p1.postId); } }
/********************************************************************************/ public Types.Post getPost(int postId) { using (var ctx = new Models.Model()) { Models.Post p = ctx.posts.FirstOrDefault(x => x.postId == postId); Types.Post p1 = new Types.Post(); p1.postId = p.postId; p1.headline = p.headline; p1.time = p.time; p1.description = p.description; p1.upvotes = p.upvotes; p1.user = new Types.User(); p1.user.userId = p.user.userId; p1.user.username = p.user.username; p1.user.firstName = p.user.firstName; p1.user.lastName = p.user.lastName; p1.user.password = p.user.password; return(p1); } }