Ejemplo n.º 1
0
        public IHttpActionResult PutCustomerInfo(int id, CustomerInfo customerInfo)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customerInfo.ID)
            {
                return(BadRequest());
            }

            db.Entry(customerInfo).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerInfoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 2
0
        public Customer AddOrUpdate(Customer customer)
        {
            using (var context = new CustomerInfoContext())
            {
                var existingCustomer = context.Customers.Include("Contacts").FirstOrDefault(x => x.Id == customer.Id);
                if (existingCustomer == null)
                {
                    context.Customers.Add(customer);
                    context.SaveChanges();
                }
                else
                {
                    var existingContactIds = existingCustomer.Contacts.Select(x => x.Id).ToList();
                    foreach (var id in existingContactIds)
                    {
                        var contact = context.Contacts.FirstOrDefault(x => x.Id == id);
                        if (contact != null)
                        {
                            context.Contacts.Remove(contact);
                        }
                    }

                    existingCustomer.CompanyName      = customer.CompanyName;
                    existingCustomer.WebsiteUrl       = customer.WebsiteUrl;
                    existingCustomer.Industry         = customer.Industry;
                    existingCustomer.EnterpriseClient = customer.EnterpriseClient;
                    existingCustomer.PrimaryContactId = customer.PrimaryContactId;

                    foreach (var contact in customer.Contacts)
                    {
                        contact.CustomerId = existingCustomer.Id;
                        context.Contacts.Add(contact);
                    }

                    context.SaveChanges();
                }
            }

            return(customer);
        }
Ejemplo n.º 3
0
        public void Delete(int id)
        {
            using (var context = new CustomerInfoContext())
            {
                var existingCustomer = context.Customers.FirstOrDefault(x => x.Id == id);

                if (existingCustomer != null)
                {
                    context.Customers.Remove(existingCustomer);
                    context.SaveChanges();
                }
            }
        }