Esempio n. 1
0
        public async Task<IHttpActionResult> Edit(int id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id.ToString() != customer.CustomerID)
            {
                return BadRequest();
            }

            try
            {
                var model = CustomerRepository.FindOneBy(x => x.CustomerID == id.ToString());

                if (model != null)
                {
                    model.CompanyName = customer.CompanyName;
                    model.ContactName = customer.ContactName;
                    model.ContactTitle = customer.ContactTitle;
                    model.Address = customer.Address;
                    model.City = customer.City;
                    model.Region = customer.Region;
                    model.PostalCode = customer.PostalCode;
                    model.Country = customer.Country;
                    model.Phone = customer.Phone;
                    model.Fax = customer.Fax;
                }

                await CustomerRepository.UpdateAsync(customer);
            }
            catch (DbUpdateConcurrencyException)
            {
                var customerToEdit = CustomerRepository.FindOneBy(x => x.CustomerID == id.ToString());

                if (customerToEdit != null)
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Esempio n. 2
0
        public async Task<IHttpActionResult> Create(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var model = new Customer()
            {
                CompanyName = customer.CompanyName,
                ContactName = customer.ContactName,
                ContactTitle = customer.ContactTitle,
                Address = customer.Address,
                City = customer.City,
                Region = customer.Region,
                PostalCode = customer.PostalCode,
                Country = customer.Country,
                Phone = customer.Phone,
                Fax = customer.Fax
            };

            await CustomerRepository.SaveAsync(model);

            return CreatedAtRoute("DefaultApi", new { id = customer.CustomerID }, customer);
        }