Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to work on Umbraco 10.x with Vendr 3.x #5

Open
wants to merge 1 commit into
base: v2/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build/NuGet/Vendr.Contrib.Reviews.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<dependency id="Umbraco.Cms.Web.Website" version="[9.0.0-rc002,9.9999999]" />
<dependency id="Vendr.Umbraco.Startup" version="[2.0.0,2.999999)" />
</group>
<group targetFramework="net6.0">
<dependency id="Umbraco.Cms.Web.Website" version="[10.0.0,10.9999999]" />
<dependency id="Vendr.Umbraco.Startup" version="[3.0.0,3.999999)" />
</group>
</dependencies>
</metadata>
<files>
Expand All @@ -32,13 +36,15 @@
<!-- libs -->
<file src="$ProjectDirectory$\bin\$Configuration$\net472\Vendr.Contrib.Reviews.dll" target="lib\net472\Vendr.Contrib.Reviews.dll" />
<file src="$ProjectDirectory$\bin\$Configuration$\net5.0\Vendr.Contrib.Reviews.dll" target="lib\net5.0\Vendr.Contrib.Reviews.dll" />
<file src="$ProjectDirectory$\bin\$Configuration$\net6.0\Vendr.Contrib.Reviews.dll" target="lib\net6.0\Vendr.Contrib.Reviews.dll" />

<!-- content -->
<file src="$ArtifactFilesDirectory$\App_Plugins\**\*" target="App_Plugins" />

<!-- UmbracoCms props and targets used to copy the content into the solution in .NET Core -->
<file src="build\Vendr.Contrib.Reviews.targets" target="buildTransitive\" />
<file src="build\Vendr.Contrib.Reviews.targets" target="build\net5.0\" />
<file src="build\Vendr.Contrib.Reviews.targets" target="build\net6.0\" />
<!-- Install script used to copy the content into the solution in .NET Framework -->
<file src="tools\install.ps1" target="tools\net472\" />

Expand Down
2 changes: 1 addition & 1 deletion build/_build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace></RootNamespace>
<NoWarn>CS0649;CS0169</NoWarn>
<NukeRootDirectory>..</NukeRootDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,38 @@ 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))
{
#if NETFRAMEWORK
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();
}
}
}
Expand Down
21 changes: 13 additions & 8 deletions src/Vendr.Contrib.Reviews/Migrations/V_1_0_0/CreateReviewTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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()
Expand All @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISqlContext> Sql() => _sqlSyntax.Sql();
protected ISqlSyntaxProvider SqlSyntax => _sqlSyntax.SqlSyntax;

Expand All @@ -45,7 +72,7 @@ public IEnumerable<Review> GetReviews(Guid[] ids)
.LeftJoin<CommentDto>().On<CommentDto, ReviewDto>((comment, review) => comment.ReviewId == review.Id)
.WhereIn<ReviewDto>(x => x.Id, ids);

var data = _uow.Database.FetchOneToMany<ReviewDto>(x => x.Comments, sql);
var data = GetDatabase().FetchOneToMany<ReviewDto>(x => x.Comments, sql);

return data.Select(EntityFactory.BuildEntity).ToList();
}
Expand Down Expand Up @@ -110,8 +137,8 @@ public PagedResult<Review> SearchReviews(Guid storeId, string searchTerm = null,

sql.OrderByDescending<ReviewDto>(x => x.CreateDate);

var page = _uow.Database.Page<ReviewDto>(pageNumber, pageSize, sql);
var page = GetDatabase().Page<ReviewDto>(pageNumber, pageSize, sql);

return new PagedResult<Review>(page.TotalItems, page.CurrentPage, page.ItemsPerPage)
{
Items = page.Items.Select(EntityFactory.BuildEntity).ToList()
Expand All @@ -120,7 +147,7 @@ public PagedResult<Review> SearchReviews(Guid storeId, string searchTerm = null,

public decimal GetAverageRatingForProduct(Guid storeId, string productReference)
{
return _uow.Database.ExecuteScalar<decimal>($"SELECT AVG(rating) FROM {ReviewDto.TableName} WHERE storeId = @0 AND productReference = @1 AND status = @2", storeId, productReference, (int)ReviewStatus.Approved);
return GetDatabase().ExecuteScalar<decimal>($"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)
Expand All @@ -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<CommentDto>("WHERE reviewId = @0", id);
_uow.Database.Delete<ReviewDto>("WHERE id = @0", id);
GetDatabase().Delete<CommentDto>("WHERE reviewId = @0", id);
GetDatabase().Delete<ReviewDto>("WHERE id = @0", id);
}

public Review ChangeReviewStatus(Guid id, ReviewStatus status)
{
var review = _uow.Database.SingleById<ReviewDto>(id);
var review = GetDatabase().SingleById<ReviewDto>(id);

review.Status = (int)status;

_uow.Database.Update(review);
GetDatabase().Update(review);

return EntityFactory.BuildEntity(review);
}
Expand All @@ -157,7 +184,7 @@ public Comment SaveComment(Comment comment)

dto.Id = dto.Id == Guid.Empty ? Guid.NewGuid() : dto.Id;

var entry = _uow.Database.SingleOrDefaultById<CommentDto>(dto.Id);
var entry = GetDatabase().SingleOrDefaultById<CommentDto>(dto.Id);
if (entry == null)
{
dto.CreateDate = dto.CreateDate == DateTime.MinValue ? DateTime.UtcNow : dto.CreateDate;
Expand All @@ -167,7 +194,7 @@ public Comment SaveComment(Comment comment)
dto.CreateDate = entry.CreateDate;
}

_uow.Database.Save(dto);
GetDatabase().Save(dto);

return EntityFactory.BuildEntity(dto);
}
Expand All @@ -184,12 +211,12 @@ public IEnumerable<Comment> GetComments(Guid storeId, Guid[] reviewIds)
.Where<CommentDto>(x => x.StoreId == storeId)
.WhereIn<CommentDto>(x => x.ReviewId, reviewIds);

return _uow.Database.Fetch<CommentDto>(sql).Select(EntityFactory.BuildEntity).ToList();
return GetDatabase().Fetch<CommentDto>(sql).Select(EntityFactory.BuildEntity).ToList();
}

public void DeleteComment(Guid id)
{
_uow.Database.Delete<CommentDto>("WHERE id = @0", id);
GetDatabase().Delete<CommentDto>("WHERE id = @0", id);
}
}
}
27 changes: 22 additions & 5 deletions src/Vendr.Contrib.Reviews/Persistence/ReviewRepositoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
}
12 changes: 9 additions & 3 deletions src/Vendr.Contrib.Reviews/Vendr.Contrib.Reviews.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;net5.0</TargetFrameworks>
<TargetFrameworks>net472;net5.0;net6.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
Expand All @@ -11,8 +11,14 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="9.0.0" />
<PackageReference Include="Umbraco.Cms.Web.Website" Version="9.0.0" />
<PackageReference Include="Vendr.Umbraco.Startup" Version="2.1.0" />
<PackageReference Include="Umbraco.Cms.Web.Website" Version="9.0.0" />
<PackageReference Include="Vendr.Umbraco.Startup" Version="2.1.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="10.0.0" />
<PackageReference Include="Umbraco.Cms.Web.Website" Version="10.0.0" />
<PackageReference Include="Vendr.Umbraco.Startup" Version="3.0.0" />
</ItemGroup>

<!-- Workaround for this bug (replace the analyzer name with the one you need to exclude (filename only, no extension) -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -72,9 +72,7 @@ public ReviewTreeController(
protected override ActionResult<MenuItemCollection> GetMenuForNode(string id, [ModelBinder(typeof(HttpQueryStringModelBinder))] FormCollection queryStrings)
{
var menu = _menuItemCollectionFactory.Create();

menu.Items.Add<ActionDelete>(_localizedTextService).LaunchDialogView("/app_plugins/vendrreviews/backoffice/views/dialogs/delete.html", "Delete");

return menu;
}

Expand Down