diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs index f6ce377a04..fe297e1306 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs @@ -301,6 +301,48 @@ namespace Umbraco.Core.Persistence.Repositories return content; } + public void Delete(int id, Guid versionId) + { + var documentDto = Database.FirstOrDefault("WHERE nodeId = @Id AND versionId = @VersionId AND newest = @Newest", new { Id = id, VersionId = versionId, Newest = false }); + Mandate.That(documentDto != null); + + using(var transaction = Database.GetTransaction()) + { + DeleteVersion(id, versionId); + + transaction.Complete(); + } + } + + public void Delete(int id, DateTime versionDate) + { + var list = Database.Fetch("WHERE nodeId = @Id AND VersionDate < @VersionDate", new {Id = id, VersionDate = versionDate}); + Mandate.That(list.Any()); + + using (var transaction = Database.GetTransaction()) + { + foreach (var dto in list) + { + DeleteVersion(id, dto.VersionId); + } + + transaction.Complete(); + } + } + + /// + /// Private method to execute the delete statements for removing a single version for a Content item. + /// + /// Id of the to delete a version from + /// Guid id of the version to delete + private void DeleteVersion(int id, Guid versionId) + { + Database.Delete("WHERE nodeId = @Id AND versionId = @VersionId", new { Id = id, VersionId = versionId }); + Database.Delete("WHERE nodeId = @Id AND versionId = @VersionId", new { Id = id, VersionId = versionId }); + Database.Delete("WHERE nodeId = @Id AND VersionId = @VersionId", new { Id = id, VersionId = versionId }); + Database.Delete("WHERE nodeId = @Id AND versionId = @VersionId", new { Id = id, VersionId = versionId }); + } + #endregion private PropertyCollection GetPropertyCollection(int id, Guid versionId, IContentType contentType) diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IContentRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IContentRepository.cs index 31cec0322e..2b2a4c1f36 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IContentRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IContentRepository.cs @@ -6,7 +6,33 @@ namespace Umbraco.Core.Persistence.Repositories { public interface IContentRepository : IRepositoryQueryable { + /// + /// Gets a list of all versions for an . + /// + /// Id of the to retrieve versions from + /// An enumerable list of the same object with different versions IEnumerable GetAllVersions(int id); + + /// + /// Gets a specific version of an . + /// + /// Id of the to retrieve version from + /// Id of the version to retrieve + /// An item IContent GetByVersion(int id, Guid versionId); + + /// + /// Deletes a specific version from an object. + /// + /// Id of the object to delete a version from + /// Id of the version to delete + void Delete(int id, Guid versionId); + + /// + /// Deletes versions from an object prior to a specific date. + /// + /// Id of the object to delete versions from + /// Latest version date + void Delete(int id, DateTime versionDate); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/IContentService.cs b/src/Umbraco.Core/Services/IContentService.cs index 28b2c9fdb7..bd94eda210 100644 --- a/src/Umbraco.Core/Services/IContentService.cs +++ b/src/Umbraco.Core/Services/IContentService.cs @@ -150,6 +150,38 @@ namespace Umbraco.Core.Services /// Optional Id of the User deleting the Content void Delete(IContent content, int userId = -1); + /// + /// Permanently deletes versions from an object prior to a specific date. + /// + /// Id of the object to delete versions from + /// Latest version date + /// Optional Id of the User deleting versions of a Content object + void Delete(IContent content, DateTime versionDate, int userId = -1); + + /// + /// Permanently deletes a specific version from an object. + /// + /// Id of the object to delete a version from + /// Id of the version to delete + /// Optional Id of the User deleting versions of a Content object + void Delete(IContent content, Guid versionId, int userId = -1); + + /// + /// Permanently deletes versions from an object prior to a specific date. + /// + /// Id of the object to delete versions from + /// Latest version date + /// Optional Id of the User deleting versions of a Content object + void Delete(int id, DateTime versionDate, int userId = -1); + + /// + /// Permanently deletes a specific version from an object. + /// + /// Id of the object to delete a version from + /// Id of the version to delete + /// Optional Id of the User deleting versions of a Content object + void Delete(int id, Guid versionId, int userId = -1); + /// /// Deletes an object by moving it to the Recycle Bin /// diff --git a/src/Umbraco.Web/Services/ContentService.cs b/src/Umbraco.Web/Services/ContentService.cs index 8662c1cb1c..5301834f22 100644 --- a/src/Umbraco.Web/Services/ContentService.cs +++ b/src/Umbraco.Web/Services/ContentService.cs @@ -473,6 +473,52 @@ namespace Umbraco.Web.Services _unitOfWork.Commit(); } + /// + /// Permanently deletes versions from an object prior to a specific date. + /// + /// Id of the object to delete versions from + /// Latest version date + /// Optional Id of the User deleting versions of a Content object + public void Delete(IContent content, DateTime versionDate, int userId = -1) + { + Delete(content.Id, versionDate, userId); + } + + /// + /// Permanently deletes a specific version from an object. + /// + /// Id of the object to delete a version from + /// Id of the version to delete + /// Optional Id of the User deleting versions of a Content object + public void Delete(IContent content, Guid versionId, int userId = -1) + { + Delete(content.Id, versionId, userId); + } + + /// + /// Permanently deletes versions from an object prior to a specific date. + /// + /// Id of the object to delete versions from + /// Latest version date + /// Optional Id of the User deleting versions of a Content object + public void Delete(int id, DateTime versionDate, int userId = -1) + { + var repository = RepositoryResolver.ResolveByType(_unitOfWork); + repository.Delete(id, versionDate); + } + + /// + /// Permanently deletes a specific version from an object. + /// + /// Id of the object to delete a version from + /// Id of the version to delete + /// Optional Id of the User deleting versions of a Content object + public void Delete(int id, Guid versionId, int userId = -1) + { + var repository = RepositoryResolver.ResolveByType(_unitOfWork); + repository.Delete(id, versionId); + } + /// /// Deletes an object by moving it to the Recycle Bin ///