public async Task <IHttpActionResult> PostuserPost(userPost userPost) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.userPosts.Add(userPost); try { await db.SaveChangesAsync(); } catch (DbUpdateException) { if (userPostExists(userPost.postId)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = userPost.postId }, userPost)); }
public async Task <IHttpActionResult> PutuserPost(long id, userPost userPost) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != userPost.postId) { return(BadRequest()); } db.Entry(userPost).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!userPostExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public static List <userPost> mapperPosts(User From, int id) { List <userPost> userPosts = new List <userPost>(); foreach (var item in From.UsersPosts.OrderByDescending(u => u.CreatedAt)) { if (item.Post.IsDeleted == false) { userPost userPost = new userPost(); // mapping userPost.PostContent = item.Post.PostContent; userPost.PostDate = GetPostCreateDate(item.CreatedAt); userPost.CanChange = (item.UserId == id); userPost.IsLike = item.Post.Likes.Any(u => u.UserId == id && u.IsDeleted == false); userPost.numOfLikes = item.Post.Likes.Where(u => u.IsDeleted == false).Count(); userPost.numOfComments = item.Post.Comments.Where(c => c.IsDeleted == false).Count(); userPost.Likes = GetPostLikes(item.Post.Likes.ToList()); userPost.Comments = GetPostComments(item.Post.Comments.Where(c => c.IsDeleted == false).ToList(), id); userPost.PostPhoto = item.Post.PostPhotos.Select(p => p.Url).FirstOrDefault(); userPost.PostId = item.PostId; userPosts.Add(userPost); } } return(userPosts); }
public async Task <IHttpActionResult> GetuserPost(long id) { userPost userPost = await db.userPosts.FindAsync(id); if (userPost == null) { return(NotFound()); } return(Ok(userPost)); }
public async Task <IHttpActionResult> DeleteuserPost(long id) { userPost userPost = await db.userPosts.FindAsync(id); if (userPost == null) { return(NotFound()); } db.userPosts.Remove(userPost); await db.SaveChangesAsync(); return(Ok(userPost)); }