예제 #1
0
        // PUT api/Goods/5
        public async Task<IHttpActionResult> PutGood(int id, Good good)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

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

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GoodExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
예제 #2
0
        public async Task<IHttpActionResult> PostGood(Good good)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Goods.Add(good);
            await db.SaveChangesAsync();

            // New code:
            // Load author name
            //db.Entry(good).Reference(x => x.).Load();

            var dto = new GoodDTO()
            {
                Id = good.Id,
                Title = good.Title,
                Price = good.Price,
                Amount = good.Amount,
                UrlImage = good.UrlImage,
                PriceOld = good.PriceOld,
                IsHit = good.IsHit
            };

            return CreatedAtRoute("DefaultApi", new { id = good.Id }, good);
        }