コード例 #1
0
        public IHttpActionResult PostCurrency(CurrencyViewModel newCurrency)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Currency currency = Mapper.Map<CurrencyViewModel, Currency>(newCurrency);

            db.Currencies.Add(currency);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = currency.Id }, new { data = currency });
        }
コード例 #2
0
        public IHttpActionResult PutCurrency(int id, CurrencyViewModel newCurrency)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            Currency currency = Mapper.Map<CurrencyViewModel, Currency>(newCurrency);
            db.Entry(currency).State = EntityState.Modified;

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

            return StatusCode(HttpStatusCode.NoContent);
        }