Esempio n. 1
0
 public Gift Add(Gift item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     item.Id = _nextId++;
     gifts.Add(item);
     return item;
 }
Esempio n. 2
0
        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;
            }
        }
Esempio n. 3
0
        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);

            
        }
Esempio n. 4
0
 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;
 }