示例#1
0
        public virtual IActionResult Create(AffiliateModel model, bool continueEditing)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageAffiliates))
            {
                return(AccessDeniedView());
            }

            if (ModelState.IsValid)
            {
                var address = model.Address.ToEntity <Address>();

                address.CreatedOnUtc = DateTime.UtcNow;

                //some validation
                if (address.CountryId == 0)
                {
                    address.CountryId = null;
                }
                if (address.StateProvinceId == 0)
                {
                    address.StateProvinceId = null;
                }

                _addressService.InsertAddress(address);

                var affiliate = model.ToEntity <Affiliate>();

                //validate friendly URL name
                var friendlyUrlName = _affiliateService.ValidateFriendlyUrlName(affiliate, model.FriendlyUrlName);
                affiliate.FriendlyUrlName = friendlyUrlName;
                affiliate.AddressId       = address.Id;

                _affiliateService.InsertAffiliate(affiliate);

                //activity log
                _customerActivityService.InsertActivity("AddNewAffiliate",
                                                        string.Format(_localizationService.GetResource("ActivityLog.AddNewAffiliate"), affiliate.Id), affiliate);

                _notificationService.SuccessNotification(_localizationService.GetResource("Admin.Affiliates.Added"));

                return(continueEditing ? RedirectToAction("Edit", new { id = affiliate.Id }) : RedirectToAction("List"));
            }

            //prepare model
            model = _affiliateModelFactory.PrepareAffiliateModel(model, null, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }