CmsKit: ratings group by star count

pull/5216/head
EngincanV 5 years ago
parent 46eb96d2d6
commit 404fe5e8a0

@ -5,6 +5,7 @@
@using Volo.CmsKit.GlobalFeatures
@using Volo.CmsKit.Pages
@using Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Commenting
@using Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Rating
@using Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.ReactionSelection
@model IndexModel
@inject IStringLocalizer<AbpUiResource> Localizer
@ -57,3 +58,7 @@
}
</abp-card-footer>
</abp-card>
<hr>
@await Component.InvokeAsync(typeof(RatingViewComponent), new { entityType = "quote", entityId = "2" })
<hr>

@ -21,5 +21,11 @@ namespace Volo.CmsKit.Ratings
Guid userId,
CancellationToken cancellationToken = default
);
Task<List<RatingWithStarCountQueryResultItem>> GetGroupedStarCountsAsync(
[NotNull] string entityType,
[NotNull] string entityId,
CancellationToken cancellationToken = default
);
}
}

@ -0,0 +1,9 @@
namespace Volo.CmsKit.Ratings
{
public class RatingWithStarCountQueryResultItem
{
public short StarCount { get; set; }
public int Count { get; set; }
}
}

@ -38,5 +38,26 @@ namespace Volo.CmsKit.Ratings
return rating;
}
public async Task<List<RatingWithStarCountQueryResultItem>> GetGroupedStarCountsAsync(string entityType, string entityId, CancellationToken cancellationToken = default)
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));
var query = from rating in DbSet
where rating.EntityType == entityType && rating.EntityId == entityId
orderby rating.StarCount descending
group rating by rating.StarCount
into g
select new RatingWithStarCountQueryResultItem
{
StarCount = g.Key,
Count = g.Count()
};
var ratings = await query.ToListAsync(GetCancellationToken(cancellationToken));
return ratings;
}
}
}

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
@ -40,5 +41,27 @@ namespace Volo.CmsKit.MongoDB.Ratings
return rating;
}
public async Task<List<RatingWithStarCountQueryResultItem>> GetGroupedStarCountsAsync(string entityType,
string entityId, CancellationToken cancellationToken = default)
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));
var query = from rating in GetMongoQueryable()
where rating.EntityType == entityType && rating.EntityId == entityId
orderby rating.StarCount descending
group rating by rating.StarCount
into g
select new RatingWithStarCountQueryResultItem
{
StarCount = g.Key,
Count = g.Count()
};
var ratings = await query.ToListAsync(GetCancellationToken(cancellationToken));
return ratings;
}
}
}

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
@ -16,5 +17,7 @@ namespace Volo.CmsKit.Public.Ratings
Task DeleteAsync(Guid id);
Task<RatingDto> GetCurrentUserRatingAsync(string entityType, string entityId);
Task<List<RatingWithStarCountDto>> GetGroupedStarCountsAsync(string entityType, string entityId);
}
}

@ -0,0 +1,9 @@
namespace Volo.CmsKit.Public.Ratings
{
public class RatingWithStarCountDto
{
public short StarCount { get; set; }
public int Count { get; set; }
}
}

@ -22,6 +22,8 @@ namespace Volo.CmsKit.Public
.Ignore(x=> x.Author);
CreateMap<Rating, RatingDto>();
CreateMap<RatingWithStarCountQueryResultItem, RatingWithStarCountDto>();
}
}
}

@ -88,5 +88,12 @@ namespace Volo.CmsKit.Public.Ratings
return ObjectMapper.Map<Rating, RatingDto>(rating);
}
public virtual async Task<List<RatingWithStarCountDto>> GetGroupedStarCountsAsync(string entityType, string entityId)
{
var ratings = await RatingRepository.GetGroupedStarCountsAsync(entityType, entityId);
return ObjectMapper.Map<List<RatingWithStarCountQueryResultItem>, List<RatingWithStarCountDto>>(ratings);
}
}
}

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
@ -44,16 +45,23 @@ namespace Volo.CmsKit.Public.Ratings
[HttpDelete]
[Route("{id}")]
public Task DeleteAsync(Guid id)
public virtual Task DeleteAsync(Guid id)
{
return RatingPublicAppService.DeleteAsync(id);
}
[HttpGet]
[Route("{entityType}/{entityId}")]
public Task<RatingDto> GetCurrentUserRatingAsync(string entityType, string entityId)
public virtual Task<RatingDto> GetCurrentUserRatingAsync(string entityType, string entityId)
{
return RatingPublicAppService.GetCurrentUserRatingAsync(entityType, entityId);
}
[HttpGet]
[Route("{entityType}/{entityId}")]
public virtual Task<List<RatingWithStarCountDto>> GetGroupedStarCountsAsync(string entityType, string entityId)
{
return RatingPublicAppService.GetGroupedStarCountsAsync(entityType, entityId);
}
}
}

@ -31,7 +31,7 @@ namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Rating
public virtual async Task<IViewComponentResult> InvokeAsync(string entityType, string entityId)
{
var ratings = await RatingPublicAppService.GetListAsync(entityType, entityId);
var ratings = await RatingPublicAppService.GetGroupedStarCountsAsync(entityType, entityId);
RatingDto currentUserRating = null;
if (CurrentUser.IsAuthenticated)
@ -47,7 +47,7 @@ namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Rating
EntityId = entityId,
EntityType = entityType,
LoginUrl = loginUrl,
Ratings = ratings.Items,
Ratings = ratings,
CurrentRating = currentUserRating
};
@ -63,7 +63,7 @@ namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Rating
public string LoginUrl { get; set; }
public IReadOnlyList<RatingDto> Ratings { get; set; }
public List<RatingWithStarCountDto> Ratings { get; set; }
public RatingDto CurrentRating { get; set; }
}

Loading…
Cancel
Save