From 8bfc6ca886ea499470a6daa01387f7dc659e5785 Mon Sep 17 00:00:00 2001 From: Niek van der Reest Date: Mon, 17 Oct 2022 17:19:08 +0200 Subject: [PATCH] Updated to work on Umbraco 10.x with Vendr 3.x --- build/NuGet/Vendr.Contrib.Reviews.nuspec | 6 ++ build/_build.csproj | 2 +- .../V_1_0_0/CreateReviewCommentTable.cs | 27 ++++---- .../Migrations/V_1_0_0/CreateReviewTable.cs | 21 ++++--- .../Implement/ReviewRepository.cs | 61 +++++++++++++------ .../Persistence/ReviewRepositoryFactory.cs | 27 ++++++-- .../Vendr.Contrib.Reviews.csproj | 12 +++- .../Web/Controllers/ReviewTreeController.cs | 16 +++-- 8 files changed, 115 insertions(+), 57 deletions(-) diff --git a/build/NuGet/Vendr.Contrib.Reviews.nuspec b/build/NuGet/Vendr.Contrib.Reviews.nuspec index 1e12d64..1cd8f89 100644 --- a/build/NuGet/Vendr.Contrib.Reviews.nuspec +++ b/build/NuGet/Vendr.Contrib.Reviews.nuspec @@ -23,6 +23,10 @@ + + + + @@ -32,6 +36,7 @@ + @@ -39,6 +44,7 @@ + diff --git a/build/_build.csproj b/build/_build.csproj index bccbef9..8a753c1 100644 --- a/build/_build.csproj +++ b/build/_build.csproj @@ -2,7 +2,7 @@ Exe - net5.0 + net6.0 CS0649;CS0169 .. diff --git a/src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewCommentTable.cs b/src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewCommentTable.cs index 6e59375..ed75af0 100644 --- a/src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewCommentTable.cs +++ b/src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewCommentTable.cs @@ -27,7 +27,11 @@ protected override void Migrate() { const string commentTableName = Constants.DatabaseSchema.Tables.Comment; const string reviewTableName = Constants.DatabaseSchema.Tables.Review; +#if NET6_0_OR_GREATER + const string storeTableName = Vendr.Persistence.Constants.DatabaseSchema.Tables.Store; +#else const string storeTableName = Vendr.Infrastructure.Constants.DatabaseSchema.Tables.Store; +#endif if (!TableExists(commentTableName)) { @@ -35,31 +39,26 @@ protected override void Migrate() var nvarcharMaxType = SqlSyntax is SqlCeSyntaxProvider ? "NTEXT" : "NVARCHAR(MAX)"; -#else +#elif NET5_0 + var nvarcharMaxType = DatabaseType is NPoco.DatabaseTypes.SqlServerCEDatabaseType + ? "NTEXT" + : "NVARCHAR(MAX)"; +#elif NET6_0_OR_GREATER var nvarcharMaxType = DatabaseType is NPoco.DatabaseTypes.SqlServerCEDatabaseType ? "NTEXT" + : DatabaseType is NPoco.DatabaseTypes.SQLiteDatabaseType + ? "TEXT" : "NVARCHAR(MAX)"; #endif // Create table Create.Table(commentTableName) .WithColumn("id").AsGuid().NotNullable().WithDefault(SystemMethods.NewGuid).PrimaryKey($"PK_{commentTableName}") - .WithColumn("storeId").AsGuid().NotNullable() - .WithColumn("reviewId").AsGuid().NotNullable() + .WithColumn("storeId").AsGuid().NotNullable().ForeignKey($"FK_{commentTableName}_{storeTableName}", storeTableName, "id") + .WithColumn("reviewId").AsGuid().NotNullable().ForeignKey($"FK_{commentTableName}_{reviewTableName}", reviewTableName, "id") .WithColumn("body").AsCustom(nvarcharMaxType).NotNullable() .WithColumn("createDate").AsDateTime().NotNullable() .Do(); - - // Foreign key constraints - Create.ForeignKey($"FK_{commentTableName}_{storeTableName}") - .FromTable(commentTableName).ForeignColumn("storeId") - .ToTable(storeTableName).PrimaryColumn("id") - .Do(); - - Create.ForeignKey($"FK_{commentTableName}_{reviewTableName}") - .FromTable(commentTableName).ForeignColumn("reviewId") - .ToTable(reviewTableName).PrimaryColumn("id") - .Do(); } } } diff --git a/src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewTable.cs b/src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewTable.cs index f6a8ea3..72196bb 100644 --- a/src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewTable.cs +++ b/src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewTable.cs @@ -25,7 +25,12 @@ protected override void Migrate() #endif { const string reviewTableName = Constants.DatabaseSchema.Tables.Review; +#if NET6_0_OR_GREATER + const string storeTableName = Vendr.Persistence.Constants.DatabaseSchema.Tables.Store; +#else const string storeTableName = Vendr.Infrastructure.Constants.DatabaseSchema.Tables.Store; +#endif + if (!TableExists(reviewTableName)) { @@ -34,16 +39,22 @@ protected override void Migrate() var nvarcharMaxType = SqlSyntax is SqlCeSyntaxProvider ? "NTEXT" : "NVARCHAR(MAX)"; -#else +#elif NET5_0 var nvarcharMaxType = DatabaseType is NPoco.DatabaseTypes.SqlServerCEDatabaseType ? "NTEXT" : "NVARCHAR(MAX)"; +#elif NET6_0_OR_GREATER + var nvarcharMaxType = DatabaseType is NPoco.DatabaseTypes.SqlServerCEDatabaseType + ? "NTEXT" + : DatabaseType is NPoco.DatabaseTypes.SQLiteDatabaseType + ? "TEXT" + : "NVARCHAR(MAX)"; #endif // Create table Create.Table(reviewTableName) .WithColumn("id").AsGuid().NotNullable().WithDefault(SystemMethods.NewGuid).PrimaryKey($"PK_{reviewTableName}") - .WithColumn("storeId").AsGuid().NotNullable() + .WithColumn("storeId").AsGuid().NotNullable().ForeignKey($"FK_{reviewTableName}_{storeTableName}", storeTableName, "id") .WithColumn("productReference").AsString(255).NotNullable() .WithColumn("customerReference").AsString(255).Nullable() .WithColumn("rating").AsDecimal(2, 1).NotNullable() @@ -57,12 +68,6 @@ protected override void Migrate() .WithColumn("createDate").AsDateTime().NotNullable() .WithColumn("updateDate").AsDateTime().NotNullable() .Do(); - - // Foreign key constraints - Create.ForeignKey($"FK_{reviewTableName}_{storeTableName}") - .FromTable(reviewTableName).ForeignColumn("storeId") - .ToTable(storeTableName).PrimaryColumn("id") - .Do(); } } } diff --git a/src/Vendr.Contrib.Reviews/Persistence/Repositories/Implement/ReviewRepository.cs b/src/Vendr.Contrib.Reviews/Persistence/Repositories/Implement/ReviewRepository.cs index 8f2a4b8..04a43d1 100644 --- a/src/Vendr.Contrib.Reviews/Persistence/Repositories/Implement/ReviewRepository.cs +++ b/src/Vendr.Contrib.Reviews/Persistence/Repositories/Implement/ReviewRepository.cs @@ -6,30 +6,57 @@ using Vendr.Contrib.Reviews.Models; using Vendr.Contrib.Reviews.Persistence.Dtos; using Vendr.Contrib.Reviews.Persistence.Factories; -using Vendr.Infrastructure; +using Vendr.Common; #if NETFRAMEWORK using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.SqlSyntax; -#else +#elif NET5_0_OR_GREATER using Umbraco.Cms.Infrastructure.Persistence; using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax; using Umbraco.Extensions; #endif +#if NET6_0_OR_GREATER +using Vendr.Persistence; +#else +using Vendr.Infrastructure; +#endif + namespace Vendr.Contrib.Reviews.Persistence.Repositories.Implement { internal class ReviewRepository : RepositoryBase, IReviewRepository { - private readonly IDatabaseUnitOfWork _uow; - private readonly ISqlContext _sqlSyntax; + private readonly ISqlContext _sqlSyntax; +#if NET6_0_OR_GREATER + private readonly IUnitOfWorkProvider _uowProvider; + private readonly INPocoDatabaseProvider _dbProvider; + public ReviewRepository(INPocoDatabaseProvider dbProvider, ISqlContext sqlSyntax) +#else + private readonly IDatabaseUnitOfWork _uow; public ReviewRepository(IDatabaseUnitOfWork uow, ISqlContext sqlSyntax) +#endif { +#if NET6_0_OR_GREATER + _dbProvider = dbProvider; +#else _uow = uow; +#endif _sqlSyntax = sqlSyntax; } + + private IDatabase GetDatabase() + { +#if NET6_0_OR_GREATER + return _dbProvider.GetDatabase(); +#else + return GetDatabase(); +#endif + } + + protected Sql Sql() => _sqlSyntax.Sql(); protected ISqlSyntaxProvider SqlSyntax => _sqlSyntax.SqlSyntax; @@ -45,7 +72,7 @@ public IEnumerable GetReviews(Guid[] ids) .LeftJoin().On((comment, review) => comment.ReviewId == review.Id) .WhereIn(x => x.Id, ids); - var data = _uow.Database.FetchOneToMany(x => x.Comments, sql); + var data = GetDatabase().FetchOneToMany(x => x.Comments, sql); return data.Select(EntityFactory.BuildEntity).ToList(); } @@ -110,8 +137,8 @@ public PagedResult SearchReviews(Guid storeId, string searchTerm = null, sql.OrderByDescending(x => x.CreateDate); - var page = _uow.Database.Page(pageNumber, pageSize, sql); - + var page = GetDatabase().Page(pageNumber, pageSize, sql); + return new PagedResult(page.TotalItems, page.CurrentPage, page.ItemsPerPage) { Items = page.Items.Select(EntityFactory.BuildEntity).ToList() @@ -120,7 +147,7 @@ public PagedResult SearchReviews(Guid storeId, string searchTerm = null, public decimal GetAverageRatingForProduct(Guid storeId, string productReference) { - return _uow.Database.ExecuteScalar($"SELECT AVG(rating) FROM {ReviewDto.TableName} WHERE storeId = @0 AND productReference = @1 AND status = @2", storeId, productReference, (int)ReviewStatus.Approved); + return GetDatabase().ExecuteScalar($"SELECT AVG(rating) FROM {ReviewDto.TableName} WHERE storeId = @0 AND productReference = @1 AND status = @2", storeId, productReference, (int)ReviewStatus.Approved); } public Review SaveReview(Review review) @@ -129,24 +156,24 @@ public Review SaveReview(Review review) dto.Id = dto.Id == Guid.Empty ? Guid.NewGuid() : dto.Id; - _uow.Database.Save(dto); + GetDatabase().Save(dto); return EntityFactory.BuildEntity(dto); } public void DeleteReview(Guid id) { - _uow.Database.Delete("WHERE reviewId = @0", id); - _uow.Database.Delete("WHERE id = @0", id); + GetDatabase().Delete("WHERE reviewId = @0", id); + GetDatabase().Delete("WHERE id = @0", id); } public Review ChangeReviewStatus(Guid id, ReviewStatus status) { - var review = _uow.Database.SingleById(id); + var review = GetDatabase().SingleById(id); review.Status = (int)status; - _uow.Database.Update(review); + GetDatabase().Update(review); return EntityFactory.BuildEntity(review); } @@ -157,7 +184,7 @@ public Comment SaveComment(Comment comment) dto.Id = dto.Id == Guid.Empty ? Guid.NewGuid() : dto.Id; - var entry = _uow.Database.SingleOrDefaultById(dto.Id); + var entry = GetDatabase().SingleOrDefaultById(dto.Id); if (entry == null) { dto.CreateDate = dto.CreateDate == DateTime.MinValue ? DateTime.UtcNow : dto.CreateDate; @@ -167,7 +194,7 @@ public Comment SaveComment(Comment comment) dto.CreateDate = entry.CreateDate; } - _uow.Database.Save(dto); + GetDatabase().Save(dto); return EntityFactory.BuildEntity(dto); } @@ -184,12 +211,12 @@ public IEnumerable GetComments(Guid storeId, Guid[] reviewIds) .Where(x => x.StoreId == storeId) .WhereIn(x => x.ReviewId, reviewIds); - return _uow.Database.Fetch(sql).Select(EntityFactory.BuildEntity).ToList(); + return GetDatabase().Fetch(sql).Select(EntityFactory.BuildEntity).ToList(); } public void DeleteComment(Guid id) { - _uow.Database.Delete("WHERE id = @0", id); + GetDatabase().Delete("WHERE id = @0", id); } } } \ No newline at end of file diff --git a/src/Vendr.Contrib.Reviews/Persistence/ReviewRepositoryFactory.cs b/src/Vendr.Contrib.Reviews/Persistence/ReviewRepositoryFactory.cs index 99daf39..b49d013 100644 --- a/src/Vendr.Contrib.Reviews/Persistence/ReviewRepositoryFactory.cs +++ b/src/Vendr.Contrib.Reviews/Persistence/ReviewRepositoryFactory.cs @@ -5,24 +5,41 @@ #if NETFRAMEWORK using Umbraco.Core.Scoping; -#else +#elif NET5_0 using Umbraco.Cms.Core.Scoping; +#elif NET6_0_OR_GREATER +using Umbraco.Cms.Infrastructure.Scoping; +using Vendr.Persistence; #endif namespace Vendr.Contrib.Reviews.Persistence { public class ReviewRepositoryFactory : IReviewRepositoryFactory { - private readonly IScopeAccessor _scopeAccessor; + //private readonly IScopeAccessor _scopeAccessor; + private readonly IScopeProvider _scopeProvider; - public ReviewRepositoryFactory(IScopeAccessor scopeAccessor) +#if NET6_0_OR_GREATER + private readonly INPocoDatabaseProvider _dbProvider; + public ReviewRepositoryFactory(IScopeProvider scopeProvider, INPocoDatabaseProvider dbProvider) +#else + public ReviewRepositoryFactory(IScopeProvider scopeProvider) +#endif { - _scopeAccessor = scopeAccessor; + _scopeProvider = scopeProvider; +#if NET6_0 + _dbProvider = dbProvider; +#endif } public IReviewRepository CreateReviewRepository(IUnitOfWork uow) { - return new ReviewRepository((IDatabaseUnitOfWork)uow, _scopeAccessor.AmbientScope.SqlContext); +#if NET6_0 + return new ReviewRepository(_dbProvider, _scopeProvider.SqlContext); +#else + return new ReviewRepository((IDatabaseUnitOfWork)uow, _scopeProvider.SqlContext); +#endif } + } } \ No newline at end of file diff --git a/src/Vendr.Contrib.Reviews/Vendr.Contrib.Reviews.csproj b/src/Vendr.Contrib.Reviews/Vendr.Contrib.Reviews.csproj index c1eaf99..4f108bf 100644 --- a/src/Vendr.Contrib.Reviews/Vendr.Contrib.Reviews.csproj +++ b/src/Vendr.Contrib.Reviews/Vendr.Contrib.Reviews.csproj @@ -1,7 +1,7 @@  - net472;net5.0 + net472;net5.0;net6.0 @@ -11,8 +11,14 @@ - - + + + + + + + + diff --git a/src/Vendr.Contrib.Reviews/Web/Controllers/ReviewTreeController.cs b/src/Vendr.Contrib.Reviews/Web/Controllers/ReviewTreeController.cs index 01b1233..a685951 100644 --- a/src/Vendr.Contrib.Reviews/Web/Controllers/ReviewTreeController.cs +++ b/src/Vendr.Contrib.Reviews/Web/Controllers/ReviewTreeController.cs @@ -45,13 +45,13 @@ protected override MenuItemCollection GetMenuForNode(string id, [ModelBinder(typ protected override TreeNodeCollection GetTreeNodes(string id, [ModelBinder(typeof(HttpQueryStringModelBinder))] FormDataCollection queryStrings) => null; } #else -[Tree( - Vendr.Umbraco.Constants.Sections.Commerce, - Constants.Trees.Reviews.Alias, - //TreeGroup = "commerce", - TreeTitle = "Reviews", - SortOrder = 10, - TreeUse = TreeUse.None)] + [Tree( + Vendr.Umbraco.Constants.Sections.Commerce, + Constants.Trees.Reviews.Alias, + //TreeGroup = "commerce", + TreeTitle = "Reviews", + SortOrder = 10, + TreeUse = TreeUse.None)] [PluginController(Constants.Internals.PluginControllerName)] public sealed class ReviewTreeController : TreeController { @@ -72,9 +72,7 @@ public ReviewTreeController( protected override ActionResult GetMenuForNode(string id, [ModelBinder(typeof(HttpQueryStringModelBinder))] FormCollection queryStrings) { var menu = _menuItemCollectionFactory.Create(); - menu.Items.Add(_localizedTextService).LaunchDialogView("/app_plugins/vendrreviews/backoffice/views/dialogs/delete.html", "Delete"); - return menu; }