Ejemplo n.º 1
0
        public IHttpActionResult PutGift(string 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);

            
        }
Ejemplo 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;
            }

            //return CreatedAtRoute("DefaultApi", new { id = product.ProductId }, product);
        }
Ejemplo n.º 3
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;
 }
Ejemplo n.º 4
0
        public Gift Add(Gift item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            item.Id = Tools.GetRandomKey();
            gifts.Add(item);
            return item;
        }