diff --git a/modules/cms-kit/host/Volo.CmsKit.Web.Unified/CmsKitWebUnifiedModule.cs b/modules/cms-kit/host/Volo.CmsKit.Web.Unified/CmsKitWebUnifiedModule.cs index 08dadb3dd6..e5d9f23c05 100644 --- a/modules/cms-kit/host/Volo.CmsKit.Web.Unified/CmsKitWebUnifiedModule.cs +++ b/modules/cms-kit/host/Volo.CmsKit.Web.Unified/CmsKitWebUnifiedModule.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Linq; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Ratings/RatingConsts.cs b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Ratings/RatingConsts.cs index 892b3cef45..27fa9d7584 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Ratings/RatingConsts.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Ratings/RatingConsts.cs @@ -7,5 +7,9 @@ namespace Volo.CmsKit.Ratings public static int MaxEntityTypeLength { get; set; } = CmsEntityConsts.MaxEntityTypeLength; public static int MaxEntityIdLength { get; set; } = CmsEntityConsts.MaxEntityIdLength; + + public static int MaxRating { get; set; } = 5; + + public static int MinRating { get; set; } = 0; } } \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/IRatingRepository.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/IRatingRepository.cs index 29ce07df63..a9f71b0dfb 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/IRatingRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/IRatingRepository.cs @@ -9,12 +9,6 @@ namespace Volo.CmsKit.Ratings { public interface IRatingRepository : IBasicRepository { - Task> GetListAsync( - [NotNull] string entityType, - [NotNull] string entityId, - CancellationToken cancellationToken = default - ); - Task GetCurrentUserRatingAsync( [NotNull] string entityType, [NotNull] string entityId, diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/Rating.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/Rating.cs index 4567b5d275..75f0499db3 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/Rating.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/Rating.cs @@ -44,7 +44,7 @@ namespace Volo.CmsKit.Ratings public virtual void SetStarCount(short starCount) { - if(starCount <= 5 && starCount > 0) + if(starCount <= RatingConsts.MaxRating && starCount > RatingConsts.MinRating) { StarCount = starCount; } diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContextModelCreatingExtensions.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContextModelCreatingExtensions.cs index d73f60967a..adda59c3e2 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContextModelCreatingExtensions.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContextModelCreatingExtensions.cs @@ -85,7 +85,7 @@ namespace Volo.CmsKit.EntityFrameworkCore r.Property(x => x.EntityType).IsRequired().HasMaxLength(RatingConsts.MaxEntityTypeLength); r.Property(x => x.EntityId).IsRequired().HasMaxLength(RatingConsts.MaxEntityIdLength); - r.HasIndex(x => new {x.TenantId, x.EntityType, x.EntityId}); + r.HasIndex(x => new {x.TenantId, x.EntityType, x.EntityId, x.CreatorId}); }); } } diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Ratings/EfCoreRatingRepository.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Ratings/EfCoreRatingRepository.cs index 577f2e3a7d..810335b4d6 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Ratings/EfCoreRatingRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Ratings/EfCoreRatingRepository.cs @@ -18,18 +18,6 @@ namespace Volo.CmsKit.Ratings { } - public async Task> GetListAsync(string entityType, string entityId, - CancellationToken cancellationToken = default) - { - Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); - Check.NotNullOrWhiteSpace(entityId, nameof(entityId)); - - var query = DbSet.Where(r => r.EntityType == entityType && r.EntityId == entityId); - var ratings = await query.ToListAsync(GetCancellationToken(cancellationToken)); - - return ratings; - } - public async Task GetCurrentUserRatingAsync(string entityType, string entityId, Guid userId, CancellationToken cancellationToken = default) { diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Ratings/MongoRatingRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Ratings/MongoRatingRepository.cs index ecf389cde7..92f7f992da 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Ratings/MongoRatingRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Ratings/MongoRatingRepository.cs @@ -19,18 +19,6 @@ namespace Volo.CmsKit.MongoDB.Ratings { } - public async Task> GetListAsync(string entityType, string entityId, - CancellationToken cancellationToken = default) - { - Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); - Check.NotNullOrWhiteSpace(entityId, nameof(entityId)); - - var query = GetMongoQueryable().Where(r => r.EntityType == entityType && r.EntityId == entityId); - var ratings = await query.ToListAsync(GetCancellationToken(cancellationToken)); - - return ratings; - } - public async Task GetCurrentUserRatingAsync(string entityType, string entityId, Guid userId, CancellationToken cancellationToken = default) { diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/CreateRatingInput.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/CreateUpdateRatingInput.cs similarity index 63% rename from modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/CreateRatingInput.cs rename to modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/CreateUpdateRatingInput.cs index 88b53d5b0c..3018ed8b30 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/CreateRatingInput.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/CreateUpdateRatingInput.cs @@ -2,9 +2,9 @@ namespace Volo.CmsKit.Public.Ratings { - public class CreateRatingInput + public class CreateUpdateRatingInput { - [Required] + [Required, Range(1, 5)] public short StarCount { get; set; } } } \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/IRatingPublicAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/IRatingPublicAppService.cs index 9e4c8c1f11..aa0aba48b4 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/IRatingPublicAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/IRatingPublicAppService.cs @@ -8,13 +8,9 @@ namespace Volo.CmsKit.Public.Ratings { public interface IRatingPublicAppService : IApplicationService { - Task> GetListAsync(string entityType, string entityId); - - Task CreateAsync(string entityType, string entityId, CreateRatingInput input); - - Task UpdateAsync(Guid id, UpdateRatingInput input); + Task CreateAsync(string entityType, string entityId, CreateUpdateRatingInput input); - Task DeleteAsync(Guid id); + Task DeleteAsync(string entityType, string entityId); Task GetCurrentUserRatingAsync(string entityType, string entityId); diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/RatingWithStarCountDto.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/RatingWithStarCountDto.cs index e8f60ea49b..981ef2bd6e 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/RatingWithStarCountDto.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/RatingWithStarCountDto.cs @@ -5,5 +5,7 @@ public short StarCount { get; set; } public int Count { get; set; } + + public bool IsSelectedByCurrentUser { get; set; } } } \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/UpdateRatingInput.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/UpdateRatingInput.cs deleted file mode 100644 index e2f77122d4..0000000000 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/UpdateRatingInput.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Volo.CmsKit.Public.Ratings -{ - public class UpdateRatingInput - { - [Required] - public short StarCount { get; set; } - } -} \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/PublicApplicationAutoMapperProfile.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/PublicApplicationAutoMapperProfile.cs index 0a0a5403b1..3ac2d1949b 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/PublicApplicationAutoMapperProfile.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/PublicApplicationAutoMapperProfile.cs @@ -22,8 +22,6 @@ namespace Volo.CmsKit.Public .Ignore(x=> x.Author); CreateMap(); - - CreateMap(); } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Ratings/RatingPublicAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Ratings/RatingPublicAppService.cs index 7621924727..848dbeda28 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Ratings/RatingPublicAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Ratings/RatingPublicAppService.cs @@ -22,18 +22,22 @@ namespace Volo.CmsKit.Public.Ratings CmsUserLookupService = cmsUserLookupService; } - public virtual async Task> GetListAsync(string entityType, string entityId) + [Authorize] + public virtual async Task CreateAsync(string entityType, string entityId, + CreateUpdateRatingInput input) { - var ratings = await RatingRepository.GetListAsync(entityType, entityId); - var ratingDto = ObjectMapper.Map, List>(ratings); + var userId = CurrentUser.GetId(); + var user = await CmsUserLookupService.GetByIdAsync(userId); - return new ListResultDto(ratingDto); - } + var currentUserRating = await RatingRepository.GetCurrentUserRatingAsync(entityType, entityId, userId); - [Authorize] - public virtual async Task CreateAsync(string entityType, string entityId, CreateRatingInput input) - { - var user = await CmsUserLookupService.GetByIdAsync(CurrentUser.GetId()); + if (currentUserRating != null) + { + currentUserRating.SetStarCount(input.StarCount); + var updatedRating = await RatingRepository.UpdateAsync(currentUserRating); + + return ObjectMapper.Map(updatedRating); + } var rating = await RatingRepository.InsertAsync( new Rating( @@ -50,50 +54,51 @@ namespace Volo.CmsKit.Public.Ratings } [Authorize] - public virtual async Task UpdateAsync(Guid id, UpdateRatingInput input) - { - var rating = await RatingRepository.GetAsync(id); - - if (rating.CreatorId != CurrentUser.GetId()) - { - throw new AbpAuthorizationException(); - } - - rating.SetStarCount(input.StarCount); - - var updatedRating = await RatingRepository.UpdateAsync(rating); - - return ObjectMapper.Map(updatedRating); - } - - [Authorize] - public virtual async Task DeleteAsync(Guid id) + public virtual async Task DeleteAsync(string entityType, string entityId) { - var rating = await RatingRepository.GetAsync(id); + var rating = await RatingRepository.GetCurrentUserRatingAsync(entityType, entityId, CurrentUser.GetId()); if (rating.CreatorId != CurrentUser.GetId()) { throw new AbpAuthorizationException(); } - await RatingRepository.DeleteAsync(id); + await RatingRepository.DeleteAsync(rating.Id); } [Authorize] public virtual async Task GetCurrentUserRatingAsync(string entityType, string entityId) { var currentUserId = CurrentUser.GetId(); - + var rating = await RatingRepository.GetCurrentUserRatingAsync(entityType, entityId, currentUserId); return ObjectMapper.Map(rating); } - public virtual async Task> GetGroupedStarCountsAsync(string entityType, string entityId) + public virtual async Task> GetGroupedStarCountsAsync(string entityType, + string entityId) { var ratings = await RatingRepository.GetGroupedStarCountsAsync(entityType, entityId); - return ObjectMapper.Map, List>(ratings); + var userRatingOrNull = CurrentUser.IsAuthenticated + ? await RatingRepository.GetCurrentUserRatingAsync(entityType, entityId, CurrentUser.GetId()) + : null; + + var ratingWithStarCountDto = new List(); + + foreach (var rating in ratings) + { + ratingWithStarCountDto.Add( + new RatingWithStarCountDto + { + StarCount = rating.StarCount, + Count = rating.Count, + IsSelectedByCurrentUser = userRatingOrNull != null && userRatingOrNull.StarCount == rating.StarCount + }); + } + + return ratingWithStarCountDto; } } } \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Ratings/RatingPublicController.cs b/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Ratings/RatingPublicController.cs index 25cc40ea5a..e136c4564d 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Ratings/RatingPublicController.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Ratings/RatingPublicController.cs @@ -1,9 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Volo.Abp; -using Volo.Abp.Application.Dtos; using Volo.Abp.GlobalFeatures; using Volo.CmsKit.GlobalFeatures; @@ -22,32 +20,18 @@ namespace Volo.CmsKit.Public.Ratings RatingPublicAppService = ratingPublicAppService; } - [HttpGet] - [Route("{entityType}/{entityId}")] - public virtual Task> GetListAsync(string entityType, string entityId) - { - return RatingPublicAppService.GetListAsync(entityType, entityId); - } - [HttpPut] [Route("{entityType}/{entityId}")] - public virtual Task CreateAsync(string entityType, string entityId, CreateRatingInput input) + public virtual Task CreateAsync(string entityType, string entityId, CreateUpdateRatingInput input) { return RatingPublicAppService.CreateAsync(entityType, entityId, input); } - [HttpPut] - [Route("{id}")] - public virtual Task UpdateAsync(Guid id, UpdateRatingInput input) - { - return RatingPublicAppService.UpdateAsync(id, input); - } - [HttpDelete] - [Route("{id}")] - public virtual Task DeleteAsync(Guid id) + [Route("{entityType}/{entityId}")] + public virtual Task DeleteAsync(string entityType, string entityId) { - return RatingPublicAppService.DeleteAsync(id); + return RatingPublicAppService.DeleteAsync(entityType, entityId); } [HttpGet] diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/Default.cshtml b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/Default.cshtml index 51c47f93de..b5cfbbfaa9 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/Default.cshtml +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/Default.cshtml @@ -9,11 +9,11 @@ @if (CurrentUser.IsAuthenticated) {
- - @(Model.CurrentRating != null ? Model.CurrentRating.StarCount + " | " : 0 + "") + + @(Model.CurrentRating != null ? Model.CurrentRating + " | " : 0 + "") @if (Model.CurrentRating != null) { - + @L["Undo"] } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/RatingViewComponent.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/RatingViewComponent.cs index 7c458d033b..aabbd3c286 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/RatingViewComponent.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/RatingViewComponent.cs @@ -34,11 +34,11 @@ namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Rating { var ratings = await RatingPublicAppService.GetGroupedStarCountsAsync(entityType, entityId); var totalRating = ratings.Sum(x => x.Count); - - RatingDto currentUserRating = null; + + short? currentUserRating = null; if (CurrentUser.IsAuthenticated) { - currentUserRating = await RatingPublicAppService.GetCurrentUserRatingAsync(entityType, entityId); + currentUserRating = ratings.Find(x => x.IsSelectedByCurrentUser)?.StarCount; } var loginUrl = @@ -68,7 +68,7 @@ namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Rating public List Ratings { get; set; } - public RatingDto CurrentRating { get; set; } + public short? CurrentRating { get; set; } public int TotalRating { get; set; } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/default.js b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/default.js index 2835e57a3c..4416d7fb48 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/default.js +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/default.js @@ -1,81 +1,89 @@ (function () { var l = abp.localization.getResource("CmsKit"); - + $(document).ready(function () { abp.widgets.CmsRating = function ($widget) { var widgetManager = $widget.data("abp-widget-manager"); var $ratingArea = $widget.find(".cms-rating-area"); - + function getFilters() { return { entityType: $ratingArea.attr("data-entity-type"), entityId: $ratingArea.attr("data-entity-id") }; } - + function registerCreateOfNewRating() { - var authenticated = $(".my-rating-5").attr("data-authenticated"); + $widget.find(".my-rating-5").each(function () { + var authenticated = $(this).attr("data-authenticated"); - $(".my-rating-5").starRating({ - initialRating: 0, - disableAfterRate: true, - useFullStars: true, - readOnly: authenticated === "True", - onHover: function(currentIndex, currentRating, $el) { - $(".live-rating").text(currentIndex); - }, - onLeave: function(currentIndex, currentRating, $el) { - $(".live-rating").text(currentRating); - }, - callback: function(currentRating, $el) { - volo.cmsKit.public.ratings.ratingPublic.create( - $ratingArea.attr("data-entity-type"), - $ratingArea.attr("data-entity-id"), - { - starCount: parseInt(currentRating) + $(this).starRating({ + initialRating: 0, + disableAfterRate: true, + useFullStars: true, + readOnly: authenticated === "True", + onHover: function (currentIndex, currentRating, $el) { + $widget.find(".live-rating").text(currentIndex); + }, + onLeave: function (currentIndex, currentRating, $el) { + $widget.find(".live-rating").text(currentRating); + }, + callback: function (currentRating, $el) { + volo.cmsKit.public.ratings.ratingPublic.create( + $ratingArea.attr("data-entity-type"), + $ratingArea.attr("data-entity-id"), + { + starCount: parseInt(currentRating) + } + ).then(function () { + widgetManager.refresh($widget); + }) } - ).then(function () { - widgetManager.refresh($widget); - }) + }); } - }); + ); } - + function registerUndoLink() { - $(".rating-undo-link").on('click', '', function (e) { - e.preventDefault(); - - abp.message.confirm(l("RatingUndoMessage"), function (ok) { - if(ok) { - var id = $(".rating-undo-link").attr("data-id"); - volo.cmsKit.public.ratings.ratingPublic.delete( - id - ).then(function () { - widgetManager.refresh($widget); - }); - } - }) + $widget.find(".rating-undo-link").each(function () { + $(this).on('click', '', function (e) { + e.preventDefault(); + + abp.message.confirm(l("RatingUndoMessage"), function (ok) { + if (ok) { + volo.cmsKit.public.ratings.ratingPublic.delete( + $ratingArea.attr("data-entity-type"), + $ratingArea.attr("data-entity-id") + ).then(function () { + widgetManager.refresh($widget); + }); + } + }) + }); }); } - + function init() { registerCreateOfNewRating(); registerUndoLink(); } - + return { init: init, getFilters: getFilters } - }; - + } + ; + $('.abp-widget-wrapper[data-widget-name="CmsRating"]') .each(function () { var widgetManager = new abp.WidgetManager({ wrapper: $(this), }); - + widgetManager.init($(this)); }); - }); -})(jQuery); \ No newline at end of file + }) + ; +}) +(jQuery); \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Volo.CmsKit.Public.Web.csproj b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Volo.CmsKit.Public.Web.csproj index 3d52f544e0..6d2f8ff303 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Volo.CmsKit.Public.Web.csproj +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Volo.CmsKit.Public.Web.csproj @@ -27,10 +27,6 @@ - - true - PreserveNewest - diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Ratings/RatingPublicAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Ratings/RatingPublicAppService_Tests.cs index 1a21da8425..c8f2acd950 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Ratings/RatingPublicAppService_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Ratings/RatingPublicAppService_Tests.cs @@ -1,5 +1,6 @@ using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using NSubstitute; using Shouldly; @@ -34,8 +35,8 @@ namespace Volo.CmsKit.Ratings var newRating = await _ratingAppService.CreateAsync( _cmsKitTestData.EntityType1, - _cmsKitTestData.EntityId1, - new CreateRatingInput + _cmsKitTestData.EntityId2, + new CreateUpdateRatingInput { StarCount = 4 }); @@ -44,53 +45,86 @@ namespace Volo.CmsKit.Ratings { var ratings = context.Set().Where(x => x.EntityId == _cmsKitTestData.EntityId1 && x.EntityType == _cmsKitTestData.EntityType1).ToList(); - + ratings - .Any(c => c.Id == newRating.Id && c.CreatorId == newRating.CreatorId && c.StarCount == newRating.StarCount) + .Any(c => c.Id == newRating.Id && c.CreatorId == newRating.CreatorId && + c.StarCount == newRating.StarCount) .ShouldBeTrue(); }); } [Fact] - public async Task UpdateAsync() + public async Task CreateAsync_Should_Update_If_Rating_Is_Exist() { _currentUser.Id.Returns(_cmsKitTestData.User1Id); - var rating = await _ratingAppService.CreateAsync( + var entity = + await _ratingAppService.GetCurrentUserRatingAsync(_cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1); + + var updatedEntity = await _ratingAppService.CreateAsync( _cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1, - new CreateRatingInput + new CreateUpdateRatingInput { - StarCount = 4 + StarCount = 5 }); - await _ratingAppService.UpdateAsync(rating.Id, new UpdateRatingInput - { - StarCount = 5 - }); + entity.Id.ShouldBe(updatedEntity.Id); + entity.EntityId.ShouldBe(updatedEntity.EntityId); + entity.EntityType.ShouldBe(updatedEntity.EntityType); + entity.StarCount.ShouldBe(updatedEntity.StarCount); + } + + [Fact] + public async Task GetCurrentUserRatingAsync() + { + _currentUser.Id.Returns(_cmsKitTestData.User1Id); + + var rating = await _ratingAppService.GetCurrentUserRatingAsync( + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1 + ); UsingDbContext(context => { - var updatedRating = context.Set().Single(x => x.Id == rating.Id); - - updatedRating.StarCount.ShouldBe((short)5); + var ratings = context.Set().Where(x => + x.EntityId == _cmsKitTestData.EntityId1 && x.EntityType == _cmsKitTestData.EntityType1).ToList(); + + ratings + .Any(c => c.Id == rating.Id && c.EntityId == rating.EntityId && c.EntityType == rating.EntityType) + .ShouldBeTrue(); }); } + [Fact] + public async Task GetGroupedStarCountsAsync() + { + _currentUser.Id.Returns(_cmsKitTestData.User1Id); + + var ratings = await _ratingAppService.GetGroupedStarCountsAsync( + _cmsKitTestData.EntityType1, + _cmsKitTestData.EntityId1 + ); + + ratings.ShouldNotBeNull(); + ratings.Count.ShouldBeGreaterThan(0); + } + [Fact] public async Task DeleteAsync() { _currentUser.Id.Returns(_cmsKitTestData.User1Id); - + var rating = await _ratingAppService.CreateAsync( _cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1, - new CreateRatingInput + new CreateUpdateRatingInput { StarCount = 4 }); - await _ratingAppService.DeleteAsync(rating.Id); + await _ratingAppService.DeleteAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1); UsingDbContext(context => { diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/Ratings/RatingRepository_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/Ratings/RatingRepository_Tests.cs index b09b380f8e..19da73cf50 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/Ratings/RatingRepository_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/Ratings/RatingRepository_Tests.cs @@ -17,14 +17,6 @@ namespace Volo.CmsKit.Ratings _ratingRepository = GetRequiredService(); } - [Fact] - public async Task GetListAsync() - { - var list = await _ratingRepository.GetListAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1); - - list.Count.ShouldBeGreaterThan(0); - } - [Fact] public async Task GetCurrentUserRatingAsync() {