Пример #1
0
        public IActionResult AddPhone(long id, CustomerPhoneNumber newPhone)
        {
            var customer = _db.Customers.FirstOrDefault(c => c.CustomerId == id);

            if (customer == null)
            {
                return(RedirectToAction("List"));
            }

            var addedPhone = new AddEditPhoneViewModel
            {
                AlterPhone = newPhone
            };

            addedPhone = FillEditPhone(customer, addedPhone);
            if (!ModelState.IsValid)
            {
                return(View(addedPhone));
            }

            if (!string.IsNullOrWhiteSpace(addedPhone.AlterPhone.PhoneNumber))
            {
                _db.Add(newPhone);
                _db.SaveChanges();
            }

            return(RedirectToAction("profile", "customer", new { id = id }));
        }
Пример #2
0
        public IActionResult AddPhone(long id)
        {
            var customer = _db.Customers.FirstOrDefault(c => c.CustomerId == id);

            if (customer == null)
            {
                return(RedirectToAction("List"));
            }

            var newPhone = new CustomerPhoneNumber {
                CustomerId = id
            };
            var adding = new AddEditPhoneViewModel {
                AlterPhone = newPhone
            };

            adding = FillEditPhone(customer, adding);

            if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                return(PartialView(adding));
            }

            return(View(adding));
        }
Пример #3
0
        public IActionResult EditPhone(long id, long ph)
        {
            var requested = _db.Customers.FirstOrDefault(c => c.CustomerId == id);

            if (ValidCustomerIdAndLong(requested, id, ph) != null)
            {
                return(ValidCustomerIdAndLong(requested, id, ph));
            }

            var editModel = new AddEditPhoneViewModel();

            //make sure there is an attatched phone to the customer
            if (_db.PhoneNumbers.Any(p => p.CustomerId == id))
            {
                //if there is find the one with the associated phone id
                var requestedPhone = _db.PhoneNumbers.FirstOrDefault(p => p.PhoneId == ph);
                if (requestedPhone == null)
                {
                    return(RedirectToAction("Profile", "Customer", new { id = id }));
                }
                else
                {
                    editModel            = FillEditPhone(requested, editModel);
                    editModel.AlterPhone = requestedPhone;
                }
            }

            if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                return(PartialView(editModel));
            }

            return(View(editModel));
        }
Пример #4
0
        //Phone View Model filler to fill out customer info
        private AddEditPhoneViewModel FillEditPhone(Customer givenCustomer, AddEditPhoneViewModel toFill)
        {
            toFill.CusId     = givenCustomer.CustomerId;
            toFill.Given     = givenCustomer.FirstName;
            toFill.Surname   = givenCustomer.LastName;
            toFill.Creation  = givenCustomer.DateCreated;
            toFill.Emails    = _db.Emails.Where(e => e.CustomerId == givenCustomer.CustomerId).ToList();
            toFill.Phones    = _db.PhoneNumbers.Where(p => p.CustomerId == givenCustomer.CustomerId).ToList();
            toFill.Addresses = _db.MailingAddresses.Where(m => m.CustomerId == givenCustomer.CustomerId).ToList();

            return(toFill);
        }
Пример #5
0
        public IActionResult EditPhone(long id, long ph, CustomerPhoneNumber editPhone)
        {
            var requested = _db.Customers.FirstOrDefault(c => c.CustomerId == id);

            if (ValidCustomerIdAndLong(requested, id, ph) != null)
            {
                return(ValidCustomerIdAndLong(requested, id, ph));
            }

            var edited = new AddEditPhoneViewModel
            {
                AlterPhone = editPhone
            };

            //make sure there is an attatched phone to the customer
            if (_db.PhoneNumbers.Any(p => p.CustomerId == id))
            {
                //if there is find the one with the associated phone id
                var requestedPhone = _db.PhoneNumbers.FirstOrDefault(p => p.PhoneId == ph);
                if (requestedPhone == null)
                {
                    return(RedirectToAction("Profile", "Customer", new { id = id }));
                }
                else
                {
                    var sendBack = FillEditPhone(requested, edited);
                    if (!ModelState.IsValid)
                    {
                        return(View(sendBack));
                    }

                    if (!string.IsNullOrWhiteSpace(edited.AlterPhone.PhoneNumber))
                    {
                        requestedPhone.PhoneNumber = edited.AlterPhone.PhoneNumber;
                        _db.SaveChanges();
                    }
                }
            }

            return(RedirectToAction("Profile", "Customer", new { id = id }));
        }