Ejemplo n.º 1
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 PackageDefinition?GetById(int id)
    {
        Sql <ISqlContext> query = new Sql <ISqlContext>(_umbracoDatabase !.SqlContext)
                                  .Select <CreatedPackageSchemaDto>()
                                  .From <CreatedPackageSchemaDto>()
                                  .Where <CreatedPackageSchemaDto>(x => x.Id == id);
        List <CreatedPackageSchemaDto> schemaDtos = _umbracoDatabase.Fetch <CreatedPackageSchemaDto>(query);

        if (schemaDtos.IsCollectionEmpty())
        {
            return(null);
        }

        CreatedPackageSchemaDto packageSchema = schemaDtos.First();
        var packageDefinition = _xmlParser.ToPackageDefinition(XElement.Parse(packageSchema.Value));

        if (packageDefinition is not null)
        {
            packageDefinition.Id        = packageSchema.Id;
            packageDefinition.Name      = packageSchema.Name;
            packageDefinition.PackageId = packageSchema.PackageId;
        }

        return(packageDefinition);
    }
        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);
        }