Example #1
0
        public HttpResponseMessage EditPost(int id, [FromBody] PostsModel postModel)
        {
            try
            {
                using (var blogdb = new blogdbEntities())
                {
                    posts newPost = (from p in blogdb.posts
                                     where p.id == id && p.id_author == loginUserId
                                     select p).SingleOrDefault();
                    if (newPost != null)
                    {
                        newPost.content = postModel.content;

                        blogdb.Entry(newPost).State = EntityState.Modified;
                        blogdb.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, "The Post was updated successfully."));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                           "The Post with Id: " + id.ToString() + " not found to update"));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }
        }
Example #2
0
        public HttpResponseMessage CreatePost([FromBody] PostsModel postModel)
        {
            try
            {
                using (var blogdb = new blogdbEntities())
                {
                    int lastPostId = (!blogdb.posts.Any()) ? 0 : blogdb.posts.OrderByDescending(p => p.id).First().id;
                    int nextPostId = lastPostId + 1;

                    posts newPost = new posts
                    {
                        id        = nextPostId,
                        id_author = loginUserId,
                        content   = postModel.content
                    };

                    blogdb.Entry(newPost).State = EntityState.Added;
                    blogdb.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK, "The Post was created successfully."));
                }
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }
        }
Example #3
0
        public HttpResponseMessage DeletePost(int id)
        {
            try
            {
                using (var blogdb = new blogdbEntities())
                {
                    posts postToDelete = (from p in blogdb.posts
                                          where p.id == id && p.id_author == loginUserId
                                          select p).SingleOrDefault();
                    if (postToDelete != null)
                    {
                        blogdb.Entry(postToDelete).State = EntityState.Deleted;
                        blogdb.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK,
                                                      "The Post was deleted successfully."));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                           "The Post with Id " + id.ToString() + " not found to delete"));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }
        }
Example #4
0
        public HttpResponseMessage PutEditUser(int id, [FromBody] UsersModel userModel)
        {
            try
            {
                using (var blogdb = new blogdbEntities())
                {
                    users newUser = (from u in blogdb.users
                                     where u.id == id
                                     select u).SingleOrDefault();
                    if (newUser != null)
                    {
                        newUser.userName = userModel.userName;
                        newUser.password = userModel.password;
                        newUser.email    = userModel.email;

                        blogdb.Entry(newUser).State = System.Data.Entity.EntityState.Modified;
                        blogdb.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, "User data was updated successfully."));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                           "User with Id " + id.ToString() + " not found to update"));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }
        }
Example #5
0
        public HttpResponseMessage CreateUser([FromBody] UsersModel userModel)
        {
            try
            {
                using (var blogdb = new blogdbEntities())
                {
                    int   lastUserId = blogdb.users.OrderByDescending(u => u.id).First().id;
                    int   nextUserId = lastUserId + 1;
                    users newUser    = new users
                    {
                        id       = nextUserId,
                        userName = userModel.userName,
                        password = userModel.password,
                        email    = userModel.email
                    };

                    blogdb.Entry(newUser).State = System.Data.Entity.EntityState.Added;
                    blogdb.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK, "User was created successfully."));
                }
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }
        }
Example #6
0
        public HttpResponseMessage DeleteUser(int id)
        {
            try
            {
                using (var blogdb = new blogdbEntities())
                {
                    users newUser = (from u in blogdb.users
                                     where u.id == id
                                     select u).SingleOrDefault();
                    if (newUser != null)
                    {
                        blogdb.Entry(newUser).State = System.Data.Entity.EntityState.Deleted;
                        blogdb.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK,
                                                      "The User data was deleted successfully."));
                    }
                    else
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                           "User with Id " + id.ToString() + " not found to delete"));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, e.Message));
            }
        }