Пример #1
0
        public async Task <IActionResult> Create(AdminAuthenticateUserModel model)
        {
            try
            {
                await _adminUserService.CreateItem(model);

                response = new ApiResponse(HttpStatusCode.OK, "Admin user created successfully.", null);
                return(Ok(new { response }));
            }
            catch (Exception exception)
            {
                return(BadRequest("Admin user create failed. Error: " + exception.Message));
            }
        }
        public async Task <CustomerModel> Create(CustomerModel model)
        {
            model = await Validate(model);

            model.AdminMoniker   = model.AdminMoniker.ToUpper();
            model.TenantMonikers = new List <string>()
            {
                model.AdminMoniker
            };

            //  Create customer entity
            CustomerEntity customerEntity = new CustomerEntity(model);

            //  Create customer item in system database.
            customerEntity = await _customersManager.CreateItemAsync(customerEntity);

            //  Create customer admin database.
            DatabaseResponse databaseResponse = await _adminService.CreateDatabase();

            if (databaseResponse.StatusCode != HttpStatusCode.Created)
            {
                throw new SystemDatabaseNotCreatedException();
            }

            //  Clone 'LookupItems' container and clone items from system database.
            var containerResponse = await _adminService.CreateContainer("LookupItems", "/canonicalName");

            if (containerResponse.StatusCode != HttpStatusCode.Created)
            {
                throw new AdminContainerNotCreatedException("LookupItems");
            }

            var systemLookupItemModels = await _systemLookupItemService.GetItems();

            var systemLookupItems = SystemLookupItem.Construct(systemLookupItemModels);

            foreach (SystemLookupItem systemLookupItem in systemLookupItems)
            {
                if (systemLookupItem.CloneToAdminDatabase)
                {
                    var adminLookupItemModel = new AdminLookupItemModel(systemLookupItem);
                    await _adminLookupItemsService.CreateItem(adminLookupItemModel);
                }
            }

            //  Create 'Users' container and clone items from system database.
            containerResponse = await _adminService.CreateContainer("Users", "/username");

            if (containerResponse.StatusCode != HttpStatusCode.Created)
            {
                throw new AdminContainerNotCreatedException("Users");
            }

            var systemAuthenticateUsers = await _systemUserService.GetItems();

            foreach (SystemAuthenticateUser systemAuthenticateUser in systemAuthenticateUsers)
            {
                if (systemAuthenticateUser.CloneToAdminDatabase)
                {
                    AdminAuthenticateUserModel adminAuthenticateUserModel = new AdminAuthenticateUserModel(systemAuthenticateUser);
                    await _adminUsersService.CreateItem(adminAuthenticateUserModel);
                }
            }

            //  Create customer 'admin' user.
            foreach (AdminAuthenticateUserModel adminUserModel in model.Users)
            {
                adminUserModel.NameFirst = adminUserModel.NameFirst.Replace("{moniker}", model.AdminMoniker.ToUpper());
                adminUserModel.Username  = adminUserModel.Username.Replace("{moniker}", model.AdminMoniker.ToLower());
                adminUserModel.DisplayAs = adminUserModel.DisplayAs.Replace("{moniker}", model.AdminMoniker.ToUpper());
                adminUserModel.EmailAddresses.ForEach(x => x.Address = x.Address.Replace("{moniker}", model.AdminMoniker.ToLower()));
                //for (int i = 0; i < adminUserModel.Roles.Count; i++) adminUserModel.Roles[i] = adminUserModel.Roles[i].Replace("{moniker}", model.AdminMoniker.ToUpper());

                var randomWord = await Helpers.GetRandomWordFromWordsApi();

                var joPassword = JObject.Parse(randomWord)["word"];
                adminUserModel.Password       = _hashingService.EncryptString(joPassword.ToString());
                adminUserModel.EmailAddresses = await _adminEmailAddressService.Validate(adminUserModel.EmailAddresses);

                adminUserModel.PhoneNumbers = await _adminPhoneNumberService.Validate(adminUserModel.PhoneNumbers);

                await _adminUsersService.CreateItem(adminUserModel);
            }

            //  Create Tenants container.
            containerResponse = await _adminService.CreateContainer("Tenants", "/moniker");

            if (containerResponse.StatusCode != HttpStatusCode.Created)
            {
                throw new AdminContainerNotCreatedException("Tenants");
            }

            model = new CustomerModel(customerEntity);

            return(model);
        }