From b4755fc5c490109a3c3e44425afae22c93eb0ca0 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 10 Nov 2015 16:14:03 +0100 Subject: [PATCH] Gets more doc type folders working, can create and delete, now to get doc types to be moved. --- src/Umbraco.Core/Models/EntityContainer.cs | 8 ++- .../Repositories/ContentTypeBaseRepository.cs | 7 ++- .../Repositories/EntityContainerRepository.cs | 52 ++++++++++++++++--- .../Repositories/ExternalLoginRepository.cs | 2 +- .../Services/ContentTypeService.cs | 16 ++++-- .../src/views/documenttypes/create.html | 2 +- .../src/views/documenttypes/delete.html | 5 +- 7 files changed, 76 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.Core/Models/EntityContainer.cs b/src/Umbraco.Core/Models/EntityContainer.cs index 9c890fd594..5c4deef037 100644 --- a/src/Umbraco.Core/Models/EntityContainer.cs +++ b/src/Umbraco.Core/Models/EntityContainer.cs @@ -7,11 +7,17 @@ namespace Umbraco.Core.Models /// public sealed class EntityContainer : UmbracoEntity, IAggregateRoot { - public EntityContainer(int parentId, string name, int userId) + public EntityContainer() + { + } + + public EntityContainer(int id, int parentId, string name, int userId, string path) { + Id = id; ParentId = parentId; Name = name; CreatorId = userId; + Path = path; } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs index d25fab2cb7..126510ec0d 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ContentTypeBaseRepository.cs @@ -54,7 +54,12 @@ namespace Umbraco.Core.Persistence.Repositories public EntityContainer CreateFolder(int parentId, string name, int userId) { - var container = new EntityContainer(parentId, name, userId); + var container = new EntityContainer + { + ParentId = parentId, + Name = name, + CreatorId = userId + }; _containerRepository.AddOrUpdate(container); return container; } diff --git a/src/Umbraco.Core/Persistence/Repositories/EntityContainerRepository.cs b/src/Umbraco.Core/Persistence/Repositories/EntityContainerRepository.cs index e1dbc3ef8f..2ac407e7ba 100644 --- a/src/Umbraco.Core/Persistence/Repositories/EntityContainerRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/EntityContainerRepository.cs @@ -1,9 +1,12 @@ using System; using System.Collections.Generic; using System.Linq; +using Umbraco.Core.Cache; using Umbraco.Core.Logging; using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Models.Rdbms; +using Umbraco.Core.Persistence.Factories; using Umbraco.Core.Persistence.Querying; using Umbraco.Core.Persistence.SqlSyntax; using Umbraco.Core.Persistence.UnitOfWork; @@ -14,7 +17,7 @@ namespace Umbraco.Core.Persistence.Repositories /// An internal repository for managing entity containers such as doc type, media type, data type containers /// /// - /// All we're supporting here is creating and deleting + /// All we're supporting here is a single get, creating and deleting /// internal class EntityContainerRepository : PetaPocoRepositoryBase { @@ -29,9 +32,32 @@ namespace Umbraco.Core.Persistence.Repositories _entityObjectType = entityObjectType; } + /// + /// Do not cache anything + /// + protected override IRuntimeCacheProvider RuntimeCache + { + get { return new NullCacheProvider(); } + } + protected override EntityContainer PerformGet(int id) { - throw new NotImplementedException(); + var sql = GetBaseQuery(false); + sql.Where(GetBaseWhereClause(), new { Id = id, NodeObjectType = _containerObjectType }); + + var containerDto = Database.Fetch(sql).FirstOrDefault(); + if (containerDto == null) + return null; + + var entity = new EntityContainer(containerDto.NodeId, + containerDto.ParentId, containerDto.Text, containerDto.UserId ?? 0, + containerDto.Path); + + //on initial construction we don't want to have dirty properties tracked + // http://issues.umbraco.org/issue/U4-1946 + entity.ResetDirtyProperties(false); + + return entity; } protected override IEnumerable PerformGetAll(params int[] ids) @@ -46,12 +72,21 @@ namespace Umbraco.Core.Persistence.Repositories protected override Sql GetBaseQuery(bool isCount) { - throw new NotImplementedException(); + var sql = new Sql(); + if (isCount) + { + sql.Select("COUNT(*)").From(SqlSyntax); + } + else + { + sql.Select("*").From(SqlSyntax); + } + return sql; } protected override string GetBaseWhereClause() { - throw new NotImplementedException(); + return "umbracoNode.id = @Id and nodeObjectType = @NodeObjectType"; } protected override IEnumerable GetDeleteClauses() @@ -77,7 +112,8 @@ namespace Umbraco.Core.Persistence.Repositories var children = Database.Fetch( new Sql().Select("*") .From(SqlSyntax) - .Where(dto => dto.ParentId == entity.Id && (dto.NodeObjectType == _entityObjectType || dto.NodeObjectType == _containerObjectType))); + .Where("parentID=@parentID AND (nodeObjectType=@entityObjectType OR nodeObjectType=@containerObjectType)", + new {parentID = entity.ParentId, entityObjectType = _entityObjectType, containerObjectType = _containerObjectType})); foreach (var childDto in children) { @@ -136,11 +172,15 @@ namespace Umbraco.Core.Persistence.Repositories UserId = entity.CreatorId }; - Database.Save(nodeDto); + var id = Convert.ToInt32(Database.Insert(nodeDto)); + //update the path nodeDto.Path = nodeDto.Path + "," + nodeDto.NodeId; Database.Save(nodeDto); + entity.Id = id; + entity.Path = nodeDto.Path; + entity.ResetDirtyProperties(); } diff --git a/src/Umbraco.Core/Persistence/Repositories/ExternalLoginRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ExternalLoginRepository.cs index 97c6e4fcf5..276f4b0f89 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ExternalLoginRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ExternalLoginRepository.cs @@ -67,7 +67,7 @@ namespace Umbraco.Core.Persistence.Repositories //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 - ((TracksChangesEntityBase)entity).ResetDirtyProperties(false); + entity.ResetDirtyProperties(false); return entity; } diff --git a/src/Umbraco.Core/Services/ContentTypeService.cs b/src/Umbraco.Core/Services/ContentTypeService.cs index 2da2375d7f..38acfbe4ff 100644 --- a/src/Umbraco.Core/Services/ContentTypeService.cs +++ b/src/Umbraco.Core/Services/ContentTypeService.cs @@ -43,11 +43,13 @@ namespace Umbraco.Core.Services public Attempt CreateContentTypeFolder(int parentId, string name, int userId = 0) { - using (var repo = RepositoryFactory.CreateContentTypeRepository(UowProvider.GetUnitOfWork())) + var uow = UowProvider.GetUnitOfWork(); + using (var repo = RepositoryFactory.CreateContentTypeRepository(uow)) { try { var container = repo.CreateFolder(parentId, name, userId); + uow.Commit(); return Attempt.Succeed(container.Id); } catch (Exception ex) @@ -60,11 +62,13 @@ namespace Umbraco.Core.Services public Attempt CreateMediaTypeFolder(int parentId, string name, int userId = 0) { - using (var repo = RepositoryFactory.CreateMediaTypeRepository(UowProvider.GetUnitOfWork())) + var uow = UowProvider.GetUnitOfWork(); + using (var repo = RepositoryFactory.CreateMediaTypeRepository(uow)) { try { var container = repo.CreateFolder(parentId, name, userId); + uow.Commit(); return Attempt.Succeed(container.Id); } catch (System.Exception ex) @@ -77,18 +81,22 @@ namespace Umbraco.Core.Services public void DeleteContentTypeFolder(int folderId, int userId = 0) { - using (var repo = RepositoryFactory.CreateContentTypeRepository(UowProvider.GetUnitOfWork())) + var uow = UowProvider.GetUnitOfWork(); + using (var repo = RepositoryFactory.CreateContentTypeRepository(uow)) { repo.DeleteFolder(folderId); + uow.Commit(); //TODO: Audit trail ? } } public void DeleteMediaTypeFolder(int folderId, int userId = 0) { - using (var repo = RepositoryFactory.CreateMediaTypeRepository(UowProvider.GetUnitOfWork())) + var uow = UowProvider.GetUnitOfWork(); + using (var repo = RepositoryFactory.CreateMediaTypeRepository(uow)) { repo.DeleteFolder(folderId); + uow.Commit(); //TODO: Audit trail ? } } diff --git a/src/Umbraco.Web.UI.Client/src/views/documenttypes/create.html b/src/Umbraco.Web.UI.Client/src/views/documenttypes/create.html index a8e4086352..5f147a08ca 100644 --- a/src/Umbraco.Web.UI.Client/src/views/documenttypes/create.html +++ b/src/Umbraco.Web.UI.Client/src/views/documenttypes/create.html @@ -33,7 +33,7 @@
diff --git a/src/Umbraco.Web.UI.Client/src/views/documenttypes/delete.html b/src/Umbraco.Web.UI.Client/src/views/documenttypes/delete.html index 5c522dcbec..175e3a1fda 100644 --- a/src/Umbraco.Web.UI.Client/src/views/documenttypes/delete.html +++ b/src/Umbraco.Web.UI.Client/src/views/documenttypes/delete.html @@ -3,12 +3,13 @@

- Are you sure you want to delete {{currentNode.name}} ? + Are you sure you want to delete + {{currentNode.name}} ?

-

This action cannot be undone, click ok to delete.

+

Any item that exists in this folder will be moved to the parent folder