Skip to content

Commit

Permalink
Added missing scope (#14711)
Browse files Browse the repository at this point in the history
  • Loading branch information
andr317c committed Aug 23, 2023
1 parent 6f10517 commit 98bcf15
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Expand Up @@ -168,7 +168,12 @@ public IEnumerable<PackageDefinition> GetAll()
.From<CreatedPackageSchemaDto>()
.Where<CreatedPackageSchemaDto>(x => x.PackageId == key);

List<CreatedPackageSchemaDto> schemaDtos = Database.Fetch<CreatedPackageSchemaDto>(query);
if (_scopeAccessor.AmbientScope is null)
{
return null;
}

List<CreatedPackageSchemaDto> schemaDtos = _scopeAccessor.AmbientScope.Database.Fetch<CreatedPackageSchemaDto>(query);

if (schemaDtos.IsCollectionEmpty())
{
Expand Down Expand Up @@ -215,7 +220,13 @@ public bool SavePackage(PackageDefinition? definition)
.SelectCount()
.From<CreatedPackageSchemaDto>()
.Where<CreatedPackageSchemaDto>(x => x.Name == definition.Name);
var exists = Database.ExecuteScalar<int>(query);

if (_scopeAccessor.AmbientScope is null)
{
return false;
}

var exists = _scopeAccessor.AmbientScope.Database.ExecuteScalar<int>(query);

if (exists > 0)
{
Expand Down
Expand Up @@ -161,7 +161,12 @@ public async Task<PagedModel<PackageDefinition>> GetCreatedPackagesAsync(int ski
}

/// <inheritdoc/>
public Task<PackageDefinition?> GetCreatedPackageByKeyAsync(Guid key) => Task.FromResult(_createdPackages.GetByKey(key));
public Task<PackageDefinition?> GetCreatedPackageByKeyAsync(Guid key)
{
using ICoreScope scope = _coreScopeProvider.CreateCoreScope(autoComplete: true);

return Task.FromResult(_createdPackages.GetByKey(key));
}

[Obsolete("Use CreateCreatedPackageAsync or UpdateCreatedPackageAsync instead. Scheduled for removal in Umbraco 15.")]
public bool SaveCreatedPackage(PackageDefinition definition)
Expand Down Expand Up @@ -197,13 +202,15 @@ public bool SaveCreatedPackage(PackageDefinition definition)
/// <inheritdoc/>
public async Task<Attempt<PackageDefinition, PackageOperationStatus>> UpdateCreatedPackageAsync(PackageDefinition package, Guid userKey)
{
using ICoreScope scope = _coreScopeProvider.CreateCoreScope();
if (_createdPackages.SavePackage(package) == false)
{
return Attempt.FailWithStatus(PackageOperationStatus.NotFound, package);
}

int currentUserId = _userService.GetAsync(userKey).Result?.Id ?? Constants.Security.SuperUserId;
_auditService.Add(AuditType.New, currentUserId, -1, "Package", $"Created package '{package.Name}' updated. Package key: {package.PackageId}");
scope.Complete();
return await Task.FromResult(Attempt.SucceedWithStatus(PackageOperationStatus.Success, package));
}

Expand Down

0 comments on commit 98bcf15

Please sign in to comment.