コード例 #1
0
        public IHttpActionResult DeleteUser(int id)
        {
            User user = db.Users.Find(id);

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

            //Locate All posts by user
            var posts = db.Posts.Where(p => p.UserId == user.UserId);

            //Remove all posts by user
            db.Posts.RemoveRange(posts);

            //Save Changes
            db.SaveChanges();

            //remove Customer
            db.Users.Remove(user);

            db.SaveChanges();

            //Return model to USer
            var userModel = new UserModel
            {
                UserId = user.UserId,
                Username = user.Username,
                Password = user.Password,
                EmailAddress = user.EmailAddress,
                TwitterHandle = user.TwitterHandle,
                CreatedDate = user.CreatedDate,
                PhoneNumber = user.PhoneNumber

            };

            return Ok(userModel);
        }
コード例 #2
0
        public IHttpActionResult GetUser(int id)
        {
            User user = db.Users.Find(id);

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

            UserModel modelUser = new UserModel
            {
                UserId = user.UserId,
                CreatedDate = user.CreatedDate,
                Username = user.Username,
                Password = user.Password,
                EmailAddress = user.EmailAddress,
                PhoneNumber = user.PhoneNumber
            };

            return Ok(modelUser);
        }
コード例 #3
0
        public IHttpActionResult PutUser(int id, UserModel user)
        {
            //If not valid
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //If user is not found
            if (id != user.UserId)
            {
                return BadRequest();
            }

            //Update User In Database
            var dbUser = db.Users.Find(id);

            //Updates Entry State in DB
            db.Entry(user).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
コード例 #4
0
        public IHttpActionResult PostUser(UserModel user)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //Build new User
            var dbUser = new User();

            //Update User with new Model
            dbUser.Update(user);

            //Add user to Database
            db.Users.Add(dbUser);

            db.SaveChanges();

            user.UserId = dbUser.UserId;

            return CreatedAtRoute("DefaultApi", new { id = user.UserId }, user);
        }