public BookModel Add(BookModel book)
        {
            var addedBook = _context.Add(book);

            _context.SaveChanges();
            book.Id = addedBook.Entity.Id;
            return(book);
        }
Example #2
0
        public void Post([FromBody] Group group)
        {
            if (group.Id != 0)
            {
                Group grp = context.Groups.Find(group.Id);
                if (grp != null)
                {
                    grp.Copy(group);
                    context.SaveChanges();
                    return;
                }
            }

            context.Groups.Add(group);
            context.SaveChanges();
        }
Example #3
0
        public bool Post([FromBody] Post post)
        {
            if (!Authenticate())
            {
                return(false);
            }

            // Post validation
            if (!post.IsValid)
            {
                return(false); // If the local values are incoherents
            }
            if (RequestUserId != post.UserId)
            {
                return(false); // If the post's userId is incorrect
            }
            if (!context.Users.Any(u => u.Id == post.UserId))
            {
                return(false); // If the user doesn't exists
            }
            if (!context.Groups.Any(g => g.Id == post.GroupId))
            {
                return(false); // If the group doesn't exists
            }
            if (!context.User_Groups.Any(ug => ug.UserId == post.UserId && ug.GroupId == post.GroupId))
            {
                return(false); // If the user is not in group
            }
            if (post.Id != 0)
            {
                Post pt = context.Posts.Find(post.Id);
                if (pt != null)
                {
                    pt.Copy(post);
                    context.SaveChanges();
                    return(true);
                }
            }

            context.Posts.Add(post);
            context.SaveChanges();
            return(true);
        }
Example #4
0
        public bool Post([FromBody] User user)
        {
            if (!user.IsValid)
            {
                return(false);
            }

            if (user.Id != 0)
            {
                User usr = context.Users.Find(user.Id);
                if (usr != null)
                {
                    usr.Copy(user);
                    context.SaveChanges();
                    return(true);
                }
            }

            context.Users.Add(user);
            context.SaveChanges();
            return(true);
        }
Example #5
0
        public bool Post([FromBody] Like like)
        {
            // Check auth
            if (!Authenticate())
            {
                return(false);
            }

            // Check if like's userId is valid
            if (like.UserId != int.Parse(Request.Headers["userId"]))
            {
                return(false);
            }

            // Modify like
            if (like.Id != 0)
            {
                Like lk = context.Likes.Find(like.Id);
                if (lk != null)
                {
                    lk.Copy(like);
                    context.SaveChanges();
                    return(true);
                }
            }

            // If user already like this post
            if (context.Likes.Any(l => l.PostId == like.PostId && l.UserId == like.UserId))
            {
                return(false);
            }

            // Create new like
            context.Likes.Add(like);
            context.SaveChanges();
            return(true);
        }
Example #6
0
        public bool Post([FromBody] Comment comment)
        {
            if (!Authenticate())
            {
                return(false);
            }

            // Checking comment validity
            if (!comment.IsValid)
            {
                return(false);
            }
            if (!context.Users.Any(u => u.Id == comment.UserId))
            {
                return(false);
            }
            if (!context.Posts.Any(p => p.Id == comment.PostId))
            {
                return(false);
            }

            if (comment.Id != 0)
            {
                Comment cmt = context.Comments.Find(comment.Id);
                if (cmt != null)
                {
                    cmt.Copy(comment);
                    context.SaveChanges();
                    return(true);
                }
            }

            context.Comments.Add(comment);
            context.SaveChanges();
            return(true);
        }