public Gift Add(Gift item) { if (item == null) { throw new ArgumentNullException("item"); } item.Id = _nextId++; gifts.Add(item); return item; }
public async Task<IHttpActionResult> Post(Gift gift) { if (!ModelState.IsValid) { var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception)); return BadRequest(ModelState); } try { Gift createdGift = repository.Add(gift); //TO DO : ajout en BD return Ok(createdGift); } catch (Exception ex) { throw ex; } }
public IHttpActionResult PutGift(int id, Gift gift) { //gift.Id = id; //if (!repository.Update(gift)) //{ // throw new HttpResponseException(HttpStatusCode.NotFound); //} if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != gift.Id) { return BadRequest(); } //db.Entry(gift).State = EntityState.Modified; try { if (!repository.Update(gift)) { throw new HttpResponseException(HttpStatusCode.NotFound); } //db.SaveChanges(); } catch (Exception) //DbUpdateConcurrencyException { return NotFound(); } return StatusCode(HttpStatusCode.OK); }
public bool Update(Gift item) { if (item == null) { throw new ArgumentNullException("item"); } int index = gifts.FindIndex(p => p.Id == item.Id); if (index == -1) { return false; } gifts.RemoveAt(index); gifts.Add(item); return true; }