Browse Source

CmsKit: GetCurrentUserRatingAsync added

pull/5216/head
EngincanV 6 years ago
parent
commit
dd13fc2b17
  1. 7
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/IRatingRepository.cs
  2. 10
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Ratings/EfCoreRatingRepository.cs
  3. 13
      modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Ratings/MongoRatingRepository.cs
  4. 4
      modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/IRatingPublicAppService.cs
  5. 4
      modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/PublicApplicationAutoMapperProfile.cs
  6. 10
      modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Ratings/RatingPublicAppService.cs
  7. 32
      modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo.CmsKit.Public.HttpApi.csproj
  8. 59
      modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Ratings/RatingPublicController.cs
  9. 4
      modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicWidgetsController.cs
  10. 56
      modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/RatingViewComponent.cs

7
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Ratings/IRatingRepository.cs

@ -14,5 +14,12 @@ namespace Volo.CmsKit.Ratings
[NotNull] string entityId,
CancellationToken cancellationToken = default
);
Task<Rating> GetCurrentUserRatingAsync(
[NotNull] string entityType,
[NotNull] string entityId,
Guid userId,
CancellationToken cancellationToken = default
);
}
}

10
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Ratings/EfCoreRatingRepository.cs

@ -28,5 +28,15 @@ namespace Volo.CmsKit.Ratings
return ratings;
}
public async Task<Rating> GetCurrentUserRatingAsync(string entityType, string entityId, Guid userId, CancellationToken cancellationToken = default)
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));
var rating = await DbSet.FirstOrDefaultAsync(r => r.EntityType == entityType && r.EntityId == entityId && r.CreatorId == userId, GetCancellationToken(cancellationToken));
return rating;
}
}
}

13
modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Ratings/MongoRatingRepository.cs

@ -27,5 +27,18 @@ namespace Volo.CmsKit.MongoDB.Ratings
return ratings;
}
public async Task<Rating> GetCurrentUserRatingAsync(string entityType, string entityId, Guid userId,
CancellationToken cancellationToken = default)
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));
var rating = await GetMongoQueryable()
.FirstOrDefaultAsync(r => r.EntityType == entityType && r.EntityId == entityId && r.CreatorId == userId,
GetCancellationToken(cancellationToken));
return rating;
}
}
}

4
modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Ratings/IRatingPublicAppService.cs

@ -8,11 +8,13 @@ namespace Volo.CmsKit.Public.Ratings
public interface IRatingPublicAppService : IApplicationService
{
Task<ListResultDto<RatingDto>> GetListAsync(string entityType, string entityId);
Task<RatingDto> CreateAsync(string entityType, string entityId, CreateRatingInput input);
Task<RatingDto> UpdateAsync(Guid id, UpdateRatingInput input);
Task DeleteAsync(Guid id);
Task<RatingDto> GetCurrentUserRatingAsync(string entityType, string entityId);
}
}

4
modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/PublicApplicationAutoMapperProfile.cs

@ -2,6 +2,8 @@
using Volo.Abp.AutoMapper;
using Volo.CmsKit.Comments;
using Volo.CmsKit.Public.Comments;
using Volo.CmsKit.Public.Ratings;
using Volo.CmsKit.Ratings;
using Volo.CmsKit.Users;
namespace Volo.CmsKit.Public
@ -18,6 +20,8 @@ namespace Volo.CmsKit.Public
CreateMap<Comment, CommentWithDetailsDto>()
.Ignore(x=> x.Replies)
.Ignore(x=> x.Author);
CreateMap<Rating, RatingDto>();
}
}
}

10
modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Ratings/RatingPublicAppService.cs

@ -78,5 +78,15 @@ namespace Volo.CmsKit.Public.Ratings
await RatingRepository.DeleteAsync(id);
}
[Authorize]
public virtual async Task<RatingDto> GetCurrentUserRatingAsync(string entityType, string entityId)
{
var currentUserId = CurrentUser.GetId();
var rating = await RatingRepository.GetCurrentUserRatingAsync(entityType, entityId, currentUserId);
return ObjectMapper.Map<Rating, RatingDto>(rating);
}
}
}

32
modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo.CmsKit.Public.HttpApi.csproj

@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\..\common.props" />
<Import Project="..\..\..\..\configureawait.props" />
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.CmsKit.Common.HttpApi\Volo.CmsKit.Common.HttpApi.csproj" />
<ProjectReference Include="..\Volo.CmsKit.Public.Application.Contracts\Volo.CmsKit.Public.Application.Contracts.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\..\common.props" />
<Import Project="..\..\..\..\configureawait.props" />
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.CmsKit.Common.HttpApi\Volo.CmsKit.Common.HttpApi.csproj" />
<ProjectReference Include="..\Volo.CmsKit.Public.Application.Contracts\Volo.CmsKit.Public.Application.Contracts.csproj" />
</ItemGroup>
</Project>

59
modules/cms-kit/src/Volo.CmsKit.Public.HttpApi/Volo/CmsKit/Public/Ratings/RatingPublicController.cs

@ -0,0 +1,59 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.GlobalFeatures;
using Volo.CmsKit.GlobalFeatures;
namespace Volo.CmsKit.Public.Ratings
{
[RequiresGlobalFeature(typeof(RatingsFeature))]
[RemoteService(Name = CmsKitPublicRemoteServiceConsts.RemoteServiceName)]
[Area("cms-kit")]
[Route("api/cms-kit-public/ratings")]
public class RatingPublicController : CmsKitPublicControllerBase, IRatingPublicAppService
{
protected IRatingPublicAppService RatingPublicAppService { get; }
public RatingPublicController(IRatingPublicAppService ratingPublicAppService)
{
RatingPublicAppService = ratingPublicAppService;
}
[HttpGet]
[Route("{entityType}/{entityId}")]
public virtual Task<ListResultDto<RatingDto>> GetListAsync(string entityType, string entityId)
{
return RatingPublicAppService.GetListAsync(entityType, entityId);
}
[HttpPut]
[Route("{entityType}/{entityId}")]
public virtual Task<RatingDto> CreateAsync(string entityType, string entityId, CreateRatingInput input)
{
return RatingPublicAppService.CreateAsync(entityType, entityId, input);
}
[HttpPut]
[Route("{id}")]
public virtual Task<RatingDto> UpdateAsync(Guid id, UpdateRatingInput input)
{
return RatingPublicAppService.UpdateAsync(id, input);
}
[HttpDelete]
[Route("{id}")]
public Task DeleteAsync(Guid id)
{
return RatingPublicAppService.DeleteAsync(id);
}
[HttpGet]
[Route("{entityType}/{entityId}")]
public Task<RatingDto> GetCurrentUserRatingAsync(string entityType, string entityId)
{
return RatingPublicAppService.GetCurrentUserRatingAsync(entityType, entityId);
}
}
}

4
modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicWidgetsController.cs

@ -18,9 +18,9 @@ namespace Volo.CmsKit.Public.Web.Controllers
return Task.FromResult((IActionResult)ViewComponent(typeof(CommentingViewComponent), new {entityType, entityId}));
}
public Task<IActionResult> Rating()
public Task<IActionResult> Rating(string entityType, string entityId)
{
return Task.FromResult((IActionResult) ViewComponent(typeof(RatingViewComponent)));
return Task.FromResult((IActionResult) ViewComponent(typeof(RatingViewComponent), new {entityType, entityId}));
}
}
}

56
modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Rating/RatingViewComponent.cs

@ -1,6 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.UI;
using Volo.Abp.AspNetCore.Mvc.UI.Widgets;
using Volo.Abp.Users;
using Volo.CmsKit.Public.Ratings;
namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Rating
{
@ -12,9 +18,53 @@ namespace Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Rating
)]
public class RatingViewComponent : AbpViewComponent
{
public IViewComponentResult Invoke()
public IRatingPublicAppService RatingPublicAppService { get; }
public AbpMvcUiOptions AbpMvcUiOptions { get; }
public ICurrentUser CurrentUser { get; }
public RatingViewComponent(IRatingPublicAppService ratingPublicAppService, IOptions<AbpMvcUiOptions> options, ICurrentUser currentUser)
{
RatingPublicAppService = ratingPublicAppService;
AbpMvcUiOptions = options.Value;
CurrentUser = currentUser;
}
public virtual async Task<IViewComponentResult> InvokeAsync(string entityType, string entityId)
{
return View("~/Pages/CmsKit/Shared/Components/Rating/Default.cshtml");
var ratings = await RatingPublicAppService.GetListAsync(entityType, entityId);
RatingDto currentUserRating = null;
if (CurrentUser.IsAuthenticated)
{
currentUserRating = await RatingPublicAppService.GetCurrentUserRatingAsync(entityType, entityId);
}
var loginUrl =
$"{AbpMvcUiOptions.LoginUrl}?returnUrl={HttpContext.Request.Path.ToString()}&returnUrlHash=#cms-comment_{entityType}_{entityId}";
var viewModel = new RatingViewModel
{
EntityId = entityId,
EntityType = entityType,
LoginUrl = loginUrl,
Ratings = ratings.Items,
CurrentRating = currentUserRating
};
return View("~/Pages/CmsKit/Shared/Components/Rating/Default.cshtml", viewModel);
}
}
public class RatingViewModel
{
public string EntityType { get; set; }
public string EntityId { get; set; }
public string LoginUrl { get; set; }
public IReadOnlyList<RatingDto> Ratings { get; set; }
public RatingDto CurrentRating { get; set; }
}
}
Loading…
Cancel
Save