コード例 #1
0
        public GenericActionResult <RegistrationReturnModel <CompanyConsigneeBindingModel> > RegisterCompanyWithPrincipal(CreateUserBindingModel principalUser, CompanyConsigneeBindingModel companyConsignee)
        {
            bool companyAlreadyExists = _validationHelper.CheckForExistingCompany(companyConsignee.CompanyTRN);

            if (companyAlreadyExists)
            {
                return(new GenericActionResult <RegistrationReturnModel <CompanyConsigneeBindingModel> >()
                {
                    IsSuccess = false,
                    Errors = new List <string> {
                        "Company already registered, duplicate TRN"
                    }
                });
            }

            var createUserResult = _userManagement.CreateUser(principalUser);

            string userId = null;

            if (createUserResult.IsSuccess)
            {
                userId = createUserResult.Result.Id;

                AssignUserToDefaultRoles(userId);
            }
            else
            {
                return new GenericActionResult <RegistrationReturnModel <CompanyConsigneeBindingModel> >()
                       {
                           IsSuccess = false, Errors = createUserResult.Errors
                       }
            };

            //create and get company id to create a link between created user
            int?companyId = _dataAccess.CreateCompanyConsignee(companyConsignee);

            //if no company Id was returned, it means an error occured during creation of the user
            //in that case roll back the created user transactions
            if (!companyId.HasValue)
            {
                //TODO could use transactions here
                //reverse the created user by deleting it
                _userManagement.DeleteUser(createUserResult.Result.UserName);

                return(new GenericActionResult <RegistrationReturnModel <CompanyConsigneeBindingModel> >()
                {
                    IsSuccess = false,
                    Errors = new List <string> {
                        "Error completing registration,please try again later."
                    }
                });
            }

            //create a link record between the company and the user
            _dataAccess.CreateCompanyConsigneeRepresentative(userId, companyId.Value);

            //return new information on user & company
            return(new GenericActionResult <RegistrationReturnModel <CompanyConsigneeBindingModel> >()
            {
                IsSuccess = true,
                Errors = null,
                Result = new RegistrationReturnModel <CompanyConsigneeBindingModel>()
                {
                    UserModel = createUserResult.Result,
                    ConsigneeModel = companyConsignee
                }
            });
        }