Ejemplo n.º 1
0
 public IHttpActionResult CreateCustomer([FromBody] Customerdto customerdto)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));//Model State
     }
     try
     {
         Entities entities = new Entities();
         Customer customer = new Customer
         {
             Name             = customerdto.Name,
             Gender           = customerdto.Gender,
             BirthDate        = customerdto.BirthDate,
             City             = customerdto.City,
             MembershipTypeId = customerdto.MembershipTypeId
         };
         entities.Customers.Add(customer);
         entities.SaveChanges();
         customerdto.Id = customer.Id;
         return(Created(new Uri(Request.RequestUri + "/" + customerdto.Id), customerdto));
     }
     catch (Exception ex)
     {
         ModelState.AddModelError("", ex.Message);
         return(StatusCode(HttpStatusCode.InternalServerError));
     }
 }
Ejemplo n.º 2
0
 public async Task <ActionResult> Put(int id, [FromBody] Customerdto customerdto)
 {
     try
     {
         if (customerdto == null || id <= 0)
         {
             return(BadRequest());
         }
         var customerdb = _db.Customers.Find(id);
         var customer   = Mapper.Map <Customerdto, Customer>(customerdto, customerdb);
         _db.SaveChanges();
         return(await Task.Run(() => new ObjectResult(customer)));
     }
     catch (Exception)
     {
         return(await Task.Run(() => StatusCode(500)));
     }
 }
Ejemplo n.º 3
0
 public async Task <ActionResult> Post([FromBody] Customerdto customerdto)
 {
     try
     {
         if (customerdto == null)
         {
             return(BadRequest());
         }
         var customer = Mapper.Map(customerdto, new Customer());
         _db.Customers.Add(customer);
         _db.SaveChanges();
         return(await Task.Run(() => new ObjectResult(customer)));
     }
     catch
     {
         return(await Task.Run(() => StatusCode(500)));
     }
 }
Ejemplo n.º 4
0
        public IHttpActionResult UpdateCustomer([FromUri] int id, [FromBody] Customerdto customerdto)
        {
            Entities entities     = new Entities();
            var      customerInDb = entities.Customers.FirstOrDefault(c => c.Id == id);

            if (customerInDb == null)
            {
                return(NotFound());
            }
            if (id != customerdto.Id)
            {
                return(BadRequest());
            }
            customerInDb.Name             = customerdto.Name;
            customerInDb.City             = customerdto.City;
            customerInDb.Gender           = customerdto.Gender;
            customerInDb.BirthDate        = customerdto.BirthDate;
            customerInDb.MembershipTypeId = customerdto.MembershipTypeId;

            entities.Entry(customerInDb).State = System.Data.Entity.EntityState.Modified;
            entities.SaveChanges();
            return(Ok());
        }