public IHttpActionResult PutBunny(int id, Bunny bunny)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != bunny.Id)
            {
                return BadRequest();
            }

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }
        public IHttpActionResult PostBunny(Bunny bunny)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Bunnies.Add(bunny);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = bunny.Id }, bunny);
        }
예제 #3
0
        public IHttpActionResult Create(BunnyModel bunny)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            else
            {
                var newBunny = new Bunny
                {
                    Name = bunny.Name,
                    Health = bunny.Health,
                    Color = bunny.Color,
                };

                this.data.Bunnies.Add(newBunny);
                this.data.Bunnies.SaveChanges();

                bunny.Id = newBunny.Id;
                return Ok(newBunny);
            }
        }