コード例 #1
0
        //Post /api/customers
        //[HttpPost]
        public IHttpActionResult CreateCustomer(CustomerDTOs customerDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var customer = Mapper.Map <CustomerDTOs, Customer>(customerDTO);

            _context.Customers.Add(customer);
            _context.SaveChanges();

            customerDTO.Id = customer.Id;

            return(Created(new Uri(Request.RequestUri + "/" + customer.Id), customerDTO));
        }
コード例 #2
0
        public void UpdateCustomer(int id, CustomerDTOs customerDTO)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var customerInDb = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customerInDb == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            Mapper.Map(customerDTO, customerInDb);


            _context.SaveChanges();
        }