Exemple #1
0
        // POST api/WallPost
        public JsonResult PostPost(Post post)
        {
            post.PostedBy = WebSecurity.CurrentUserId;
            post.PostedDate = DateTime.UtcNow;
            // post.UserProfile.UserId = WebSecurity.CurrentUserId;
            ModelState.Remove("post.PostedBy");
            ModelState.Remove("post.PostedDate");
            //  ModelState.Remove("post.UserProfile.UserId");

               // if (ModelState.IsValid)
              //  {
                db.Posts.Add(post);
                db.SaveChanges();
                var usr = db.UserProfile.FirstOrDefault(x => x.UserId == post.PostedBy);
                var ret = new
                {
                    Message = post.Message,
                    PostedBy = post.PostedBy,
                    PostedByName = usr.UserName,
                    PostedByAvatar = imgFolder + (String.IsNullOrEmpty(usr.AvatarExt) ? defaultAvatar : post.PostedBy + "." + post.UserProfile.AvatarExt),
                    PostedDate = post.PostedDate,
                    PostId = post.PostId
                    //  UserId = usr.UserId
                };
              //  HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, ret);
              //  response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = post.PostId }));
              //  return response;
             //   }
            //else
            //{
            //    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            //}
                return Json(ret, JsonRequestBehavior.AllowGet);
        }
        // PUT api/WallPost/5
        public HttpResponseMessage PutPost(int id, Post post)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != post.PostId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(post).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }