Ejemplo n.º 1
0
        protected override async Task ActionAsync()
        {
            if (string.IsNullOrEmpty(Input.Data.SiteName))
            {
                throw new ArgumentException("SiteName is empty");
            }

            if (string.IsNullOrEmpty(Input.Data.ModuleId))
            {
                throw new ArgumentException("ModuleId is empty");
            }

            if (string.IsNullOrEmpty(Input.Data.CategoryId))
            {
                throw new ArgumentException("CategoryId is empty");
            }

            var checkAddSiteInput = new CheckAddSiteInput
            {
                CategoryId = Input.Data.CategoryId,
                ModuleId   = Input.Data.ModuleId,
                SiteId     = Input.Data.SiteId,
                SiteName   = Input.Data.SiteName
            };

            if (await CheckAddSiteCommand.IsSiteAlreadyExistAsync(_dataFactorySource, _dataFactoryDestination, checkAddSiteInput))
            {
                Result.ValidationResult.AddError("SITE_ADRESS_ALREADY_EXIST", "L'adresse demandé existe déjà");
            }

            var addSiteItemBusinessModel =
                (AddSiteBusinessModel)
                (await _dataFactorySource.ItemRepository.GetItemAsync(Input.Data.Site.SiteId, Input.Data.ModuleId)).Data;
            var template    = addSiteItemBusinessModel.Templates.First(t => t.CategoryId == Input.Data.CategoryId);
            var newSiteName = template.Title + " " + Input.Data.SiteName;

            var itemDataModelCurrentSite = await TransfertSiteCommand.GetSiteAsync(template.SiteId, _dataFactorySource);

            var currentSiteBusinessModel = (SiteBusinessModel)itemDataModelCurrentSite.Data;

            #region duplication du site

            var createFromSiteModel = new CreateFromSiteModel();
            createFromSiteModel.CategoryId = template.CategoryId;
            createFromSiteModel.SiteName   = newSiteName;
            createFromSiteModel.Title      = Input.Data.SiteName;
            createFromSiteModel.UserId     = Input.UserId;

            var moduleSite        = _businessModuleFactory.GetModuleCreate(itemDataModelCurrentSite.Module);
            var siteItemDataModel = await moduleSite.CreateFromAsync(_dataFactorySource, _dataFactoryDestination,
                                                                     itemDataModelCurrentSite, null, false, createFromSiteModel);

            #endregion

            await _dataFactoryDestination.SaveChangeAsync();

            await _cacheProvider.UpdateCacheAsync(siteItemDataModel.Id);

            #region Association Site/User

            await SaveSiteUserCommand.CreateNewSiteUserAsync(_siteUserService, _userService, siteItemDataModel.Id, Input.UserId);

            #endregion

            #region Generation Url nouveaux site

            var siteBusinessModel = (SiteBusinessModel)siteItemDataModel.Data;

            var input = new FindPathInput();
            input.DomainDatas = new Dictionary <string, string>();
            input.DomainDatas.Add("site", UrlHelper.NormalizeTextForUrl(newSiteName));

            if (Input.Data.Port.HasValue)
            {
                input.Port = Input.Data.Port.ToString();
            }
            //TODO règle temporaire pour le développement
            var isLocalhost = Input.Data.Site.DomainDatas.ContainsKey("domain") &&
                              Input.Data.Site.DomainDatas["domain"] == "localhost";
            if (isLocalhost)
            {
                input.DomainDatas.Add("domain", Input.Data.Site.DomainDatas["domain"]);
            }
            input.MasterDomainId = siteBusinessModel.MasterDomainId;

            input.IsSecure = null;


            input.Datas = await _routeProvider.GetRootMetadataAsync(siteItemDataModel.Id);

            var result = await _routeManager.FindDomainPathAsync(input);

            #endregion

            // On reconstitue l'Url
            var protocole = Input.Data.IsSecure ? _routeProvider.ProtocolSecure : _routeProvider.ProtocolDefault;
            var port      = string.Empty;

            if (Input.Data.Port.HasValue && Input.Data.Port.Value != 80 && Input.Data.Port.Value != 443)
            {
                port = string.Concat(":", Input.Data.Port.Value.ToString(CultureInfo.InvariantCulture));
            }
            var siteUrl = string.Concat(protocole, "://",
                                        UrlHelper.Concat(string.Concat(result.RequestDomain, port), result.Path));
            await SendEmailAsync(newSiteName, siteUrl, Input.UserId);

            Result.Data = UrlHelper.Concat(siteUrl, "/site/creation/confirmation?dm=false");
        }