public bool SavePackage(PackageDefinition definition)
        {
            if (definition == null)
            {
                throw new NullReferenceException("PackageDefinition cannot be null when saving");
            }

            if (definition.Name == null || string.IsNullOrEmpty(definition.Name) || definition.PackagePath == null)
            {
                return(false);
            }

            // Ensure it's valid
            ValidatePackage(definition);


            if (definition.Id == default)
            {
                // Create dto from definition
                var dto = new CreatedPackageSchemaDto()
                {
                    Name       = definition.Name,
                    Value      = _xmlParser.ToXml(definition).ToString(),
                    UpdateDate = DateTime.Now,
                    PackageId  = Guid.NewGuid()
                };

                // Set the ids, we have to save in database first to get the Id
                definition.PackageId = dto.PackageId;
                var result        = _umbracoDatabase.Insert(dto);
                var decimalResult = result.SafeCast <decimal>();
                definition.Id = decimal.ToInt32(decimalResult);
            }

            // Save snapshot locally, we do this to the updated packagePath
            ExportPackage(definition);
            // Create dto from definition
            var updatedDto = new CreatedPackageSchemaDto()
            {
                Name       = definition.Name,
                Value      = _xmlParser.ToXml(definition).ToString(),
                Id         = definition.Id,
                PackageId  = definition.PackageId,
                UpdateDate = DateTime.Now
            };

            _umbracoDatabase.Update(updatedDto);

            return(true);
        }
示例#2
0
    private void MigrateCreatedPackageFilesToDb()
    {
        // Load data from file
        IEnumerable <PackageDefinition> packages = _packagesRepository.GetAll().WhereNotNull();
        var createdPackageDtos = new List <CreatedPackageSchemaDto>();

        foreach (PackageDefinition package in packages)
        {
            // Create dto from xmlDocument
            var dto = new CreatedPackageSchemaDto
            {
                Name       = package.Name,
                Value      = _xmlParser.ToXml(package).ToString(),
                UpdateDate = DateTime.Now,
                PackageId  = Guid.NewGuid(),
            };
            createdPackageDtos.Add(dto);
        }

        _packagesRepository.DeleteLocalRepositoryFiles();
        if (createdPackageDtos.Any())
        {
            // Insert dto into CreatedPackage table
            Database.InsertBulk(createdPackageDtos);
        }
    }
    public bool SavePackage(PackageDefinition definition)
    {
        if (definition == null)
        {
            throw new ArgumentNullException(nameof(definition));
        }

        XDocument packagesXml = EnsureStorage(out var packagesFile);

        if (packagesXml?.Root == null)
        {
            return(false);
        }

        // ensure it's valid
        ValidatePackage(definition);

        if (definition.Id == default)
        {
            // need to gen an id and persist
            // Find max id
            var maxId = packagesXml.Root.Elements("package").Max(x => x.AttributeValue <int?>("id")) ?? 0;
            var newId = maxId + 1;
            definition.Id        = newId;
            definition.PackageId = definition.PackageId == default ? Guid.NewGuid() : definition.PackageId;
            XElement packageXml = _parser.ToXml(definition);
            packagesXml.Root.Add(packageXml);
        }
        else
        {
            // existing
            XElement?packageXml = packagesXml.Root.Elements("package")
                                  .FirstOrDefault(x => x.AttributeValue <int>("id") == definition.Id);
            if (packageXml == null)
            {
                return(false);
            }

            XElement updatedXml = _parser.ToXml(definition);
            packageXml.ReplaceWith(updatedXml);
        }

        packagesXml.Save(packagesFile);

        return(true);
    }