Example #1
0
        public ActionResult Get(int? id = null)
        {
            CustomerEditorModel model = null;

            if (id != null && id > 0)
            {
                var customer = _customerService.Find(id.Value);
                model = Mapper.Map<Customer, CustomerEditorModel>(customer);
            }
            else
            {
                model = new CustomerEditorModel();
            }

            return JsonNet(model).UsingClientConvention();
        }
Example #2
0
        public void Save(CustomerEditorModel model)
        {
            Customer customer = null;

            if (model.Id > 0)
            {
                customer = _customerService.Find(model.Id);
            }
            else
            {
                customer = new Customer();
            }

            customer.Email = model.Email;

            customer.FirstName = model.FirstName;
            customer.LastName = model.LastName;
            customer.Group = model.Group;
            customer.SavingPoints = model.SavingPoints;
            customer.Gender = model.Gender;

            customer.CustomFields.Clear();

            foreach (var field in model.CustomFields)
            {
                customer.CustomFields.Add(new CustomerCustomField(field.Name, field.Value));
            }

            if (model.Id > 0)
            {
                _customerService.Update(customer);
            }
            else
            {
                _customerService.Create(customer);
            }

            foreach (var address in customer.Addresses.ToList())
            {
                if (!model.Addresses.Any(it => it.Id == address.Id))
                {
                    customer.Addresses.Remove(address);
                }
            }

            foreach (var addressModel in model.Addresses)
            {
                var address = customer.Addresses.FirstOrDefault(it => it.Id == addressModel.Id);
                if (address == null)
                {
                    address = new Address();
                    customer.Addresses.Add(address);
                }

                UpdateAddress(address, addressModel);
            }

            _customerService.Update(customer);
        }