public async Task Add(Domain.Domains.Domain domain, CancellationToken cancellationToken)
        {
            var existing = _domainRepository.GetByName(domain.Name, cancellationToken);

            if (existing != null)
            {
                throw new DomainValidationException($"A domain named {domain.Name} already exists.");
            }

            await _domainRepository.Create(domain, cancellationToken);
        }
        /// <summary>
        /// 保存域
        /// </summary>
        /// <param name="entry"></param>
        /// <returns></returns>
        public static ResultKey SaveDomain(Domain entry)
        {
            if (entry == null)
            {
                return(ResultKey.Failure);
            }

            var dm = GetDomain(entry.Key);

            if (dm == null)
            {
                return(repository.Create(entry).Result);
            }
            return(repository.Update(entry));
        }
        public async Task <Result> Handle(ChangeBalanceCmd message)
        {
            var aggregate = await _repository
                            .FindOne(UserBalanceSpecifications.WithNonZeroId());

            aggregate ??= _repository.Create();

            return(await aggregate.ChangeBalance(message.Amount)
                   .Tap(async aggr =>
            {
                if (!aggregate.IsNew)
                {
                    _repository.Update(aggr);
                }

                await _domainUow.Commit();
            }));
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new domain.
        /// </summary>
        /// <param name="domain">New domain details.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Newly allocated domain identifier.</returns>
        public long Create(Domain domain, IUnitOfWork unitOfWork = null)
        {
            try
            {
                // Perform validation
                _domainValidator.ValidateCreate(domain);

                // Create domain
                return(_domainRepository.Create(domain, unitOfWork));
            }
            catch (ValidationErrorException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Create a new website from a template.
        /// </summary>
        /// <param name="templateTenantId">Identifies the template that will be used to create website.</param>
        /// <param name="web">New website details.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Newly allocated website identifier.</returns>
        public long Create(long templateTenantId, Web web, IUnitOfWork unitOfWork = null)
        {
            // If we don't have a unit of work in place, create one so that website creation tasks can all be rolled back in the case of failure
            IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

            try
            {
                // Check that website (and domain) do not already exist and can be created
                _webValidator.ValidateCreate(web);

                // Get template that determines initial website structure
                Template template = _templateRepository.Read(templateTenantId, true, unitOfWork ?? localUnitOfWork);

                // Get tenant identifier for new website
                DateTime now    = DateTime.UtcNow;
                Tenant   tenant = new Tenant {
                    Created = now, Updated = now
                };
                web.TenantId = _tenantRepository.Create(tenant, unitOfWork ?? localUnitOfWork);

                // Populate web from template
                web.CreateUserEnabled            = template.CreateUserEnabled;
                web.UserHasImage                 = template.UserHasImage;
                web.UserThumbnailImageWidth      = template.UserThumbnailImageWidth;
                web.UserThumbnailImageHeight     = template.UserThumbnailImageHeight;
                web.UserThumbnailImageResizeMode = template.UserThumbnailImageResizeMode;
                web.UserPreviewImageWidth        = template.UserPreviewImageWidth;
                web.UserPreviewImageHeight       = template.UserPreviewImageHeight;
                web.UserPreviewImageResizeMode   = template.UserPreviewImageResizeMode;
                web.UserImageMinWidth            = template.UserImageMinWidth;
                web.UserImageMinHeight           = template.UserImageMinHeight;

                // Create web
                _webRepository.Create(web, unitOfWork ?? localUnitOfWork);

                // Create domain
                web.Domains[0].TenantId = web.TenantId;
                long domainId = _domainRepository.Create(web.Domains[0], unitOfWork ?? localUnitOfWork);

                // Build website based on template
                Dictionary <ElementKeyValue, ElementKeyValue> templateElements = new Dictionary <ElementKeyValue, ElementKeyValue>();
                List <Page> navigationPages = new List <Page>();
                Page        page            = new Page {
                    TenantId = web.TenantId
                };
                BuildWebsite(web.TenantId, page, template.Page, templateElements, navigationPages, unitOfWork ?? localUnitOfWork);

                // Update navigation elements with navigation pages
                _elementService.AddNavigationPages(web.TenantId, templateElements.Select(kvp => kvp.Value).ToList(), navigationPages, unitOfWork ?? localUnitOfWork);

                // Commit work if local unit of work in place, then return newly allocated website identifier
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Commit();
                }
                return(web.TenantId);
            }
            catch (ValidationErrorException)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw;
            }
            catch (Exception ex)
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Rollback();
                }
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
            finally
            {
                if (localUnitOfWork != null)
                {
                    localUnitOfWork.Dispose();
                }
            }
        }
Beispiel #6
0
 protected override async Task HandleTransacted(AddDomainCommand command, CancellationToken cancellationToken = default(CancellationToken))
 {
     var domain = new Domain.Domains.Domain(command.DomainId, command.Name);
     await _domainRepository.Create(domain, cancellationToken);
 }