Ejemplo n.º 1
0
        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));
        }
Ejemplo n.º 2
0
        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));
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        public async Task <IHttpActionResult> GetuserPost(long id)
        {
            userPost userPost = await db.userPosts.FindAsync(id);

            if (userPost == null)
            {
                return(NotFound());
            }

            return(Ok(userPost));
        }
Ejemplo n.º 5
0
        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));
        }