mirror of https://github.com/abpframework/abp.git
15 changed files with 256 additions and 33 deletions
@ -1,10 +1,18 @@ |
|||||
using System; |
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using JetBrains.Annotations; |
||||
using Volo.Abp.Domain.Repositories; |
using Volo.Abp.Domain.Repositories; |
||||
|
|
||||
namespace Volo.CmsKit.Ratings |
namespace Volo.CmsKit.Ratings |
||||
{ |
{ |
||||
public interface IRatingRepository : IBasicRepository<Rating, Guid> |
public interface IRatingRepository : IBasicRepository<Rating, Guid> |
||||
{ |
{ |
||||
|
Task<List<Rating>> GetListAsync( |
||||
|
[NotNull] string entityType, |
||||
|
[NotNull] string entityId, |
||||
|
CancellationToken cancellationToken = default |
||||
|
); |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using MongoDB.Driver; |
||||
|
using MongoDB.Driver.Linq; |
||||
|
using Volo.Abp; |
||||
|
using Volo.Abp.Domain.Repositories.MongoDB; |
||||
|
using Volo.Abp.MongoDB; |
||||
|
using Volo.CmsKit.Ratings; |
||||
|
|
||||
|
namespace Volo.CmsKit.MongoDB.Ratings |
||||
|
{ |
||||
|
public class MongoRatingRepository : MongoDbRepository<ICmsKitMongoDbContext, Rating, Guid>, IRatingRepository |
||||
|
{ |
||||
|
public MongoRatingRepository(IMongoDbContextProvider<ICmsKitMongoDbContext> dbContextProvider) : base(dbContextProvider) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public async Task<List<Rating>> 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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,15 +1,15 @@ |
|||||
<Project Sdk="Microsoft.NET.Sdk"> |
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
<Import Project="..\..\..\..\common.props" /> |
<Import Project="..\..\..\..\common.props" /> |
||||
<Import Project="..\..\..\..\configureawait.props" /> |
<Import Project="..\..\..\..\configureawait.props" /> |
||||
|
|
||||
<PropertyGroup> |
<PropertyGroup> |
||||
<TargetFramework>netstandard2.0</TargetFramework> |
<TargetFramework>netstandard2.0</TargetFramework> |
||||
<RootNamespace /> |
<RootNamespace /> |
||||
</PropertyGroup> |
</PropertyGroup> |
||||
|
|
||||
<ItemGroup> |
<ItemGroup> |
||||
<ProjectReference Include="..\Volo.CmsKit.Common.Application.Contracts\Volo.CmsKit.Common.Application.Contracts.csproj" /> |
<ProjectReference Include="..\Volo.CmsKit.Common.Application.Contracts\Volo.CmsKit.Common.Application.Contracts.csproj" /> |
||||
</ItemGroup> |
</ItemGroup> |
||||
|
|
||||
</Project> |
</Project> |
||||
|
|||||
@ -0,0 +1,10 @@ |
|||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
|
||||
|
namespace Volo.CmsKit.Public.Ratings |
||||
|
{ |
||||
|
public class CreateRatingInput |
||||
|
{ |
||||
|
[Required] |
||||
|
public short StarCount { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.Application.Dtos; |
||||
|
using Volo.Abp.Application.Services; |
||||
|
|
||||
|
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); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.CmsKit.Public.Ratings |
||||
|
{ |
||||
|
public class RatingDto |
||||
|
{ |
||||
|
public Guid Id { get; set; } |
||||
|
|
||||
|
public string EntityType { get; set; } |
||||
|
|
||||
|
public string EntityId { get; set; } |
||||
|
|
||||
|
public short StarCount { get; set; } |
||||
|
|
||||
|
public Guid CreatorId { get; set; } |
||||
|
|
||||
|
public DateTime CreationTime { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,10 @@ |
|||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
|
||||
|
namespace Volo.CmsKit.Public.Ratings |
||||
|
{ |
||||
|
public class UpdateRatingInput |
||||
|
{ |
||||
|
[Required] |
||||
|
public short StarCount { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -1,16 +1,16 @@ |
|||||
<Project Sdk="Microsoft.NET.Sdk"> |
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
<Import Project="..\..\..\..\common.props" /> |
<Import Project="..\..\..\..\common.props" /> |
||||
<Import Project="..\..\..\..\configureawait.props" /> |
<Import Project="..\..\..\..\configureawait.props" /> |
||||
|
|
||||
<PropertyGroup> |
<PropertyGroup> |
||||
<TargetFramework>netstandard2.0</TargetFramework> |
<TargetFramework>netstandard2.0</TargetFramework> |
||||
<RootNamespace /> |
<RootNamespace /> |
||||
</PropertyGroup> |
</PropertyGroup> |
||||
|
|
||||
<ItemGroup> |
<ItemGroup> |
||||
<ProjectReference Include="..\Volo.CmsKit.Common.Application\Volo.CmsKit.Common.Application.csproj" /> |
<ProjectReference Include="..\Volo.CmsKit.Common.Application\Volo.CmsKit.Common.Application.csproj" /> |
||||
<ProjectReference Include="..\Volo.CmsKit.Public.Application.Contracts\Volo.CmsKit.Public.Application.Contracts.csproj" /> |
<ProjectReference Include="..\Volo.CmsKit.Public.Application.Contracts\Volo.CmsKit.Public.Application.Contracts.csproj" /> |
||||
</ItemGroup> |
</ItemGroup> |
||||
|
|
||||
</Project> |
</Project> |
||||
|
|||||
@ -0,0 +1,82 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Authorization; |
||||
|
using Volo.Abp.Application.Dtos; |
||||
|
using Volo.Abp.Application.Services; |
||||
|
using Volo.Abp.Authorization; |
||||
|
using Volo.Abp.Users; |
||||
|
using Volo.CmsKit.Ratings; |
||||
|
using Volo.CmsKit.Users; |
||||
|
|
||||
|
namespace Volo.CmsKit.Public.Ratings |
||||
|
{ |
||||
|
public class RatingPublicAppService : ApplicationService, IRatingPublicAppService |
||||
|
{ |
||||
|
protected IRatingRepository RatingRepository { get; } |
||||
|
public ICmsUserLookupService CmsUserLookupService { get; } |
||||
|
|
||||
|
public RatingPublicAppService(IRatingRepository ratingRepository, ICmsUserLookupService cmsUserLookupService) |
||||
|
{ |
||||
|
RatingRepository = ratingRepository; |
||||
|
CmsUserLookupService = cmsUserLookupService; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task<ListResultDto<RatingDto>> GetListAsync(string entityType, string entityId) |
||||
|
{ |
||||
|
var ratings = await RatingRepository.GetListAsync(entityType, entityId); |
||||
|
var ratingDto = ObjectMapper.Map<List<Rating>, List<RatingDto>>(ratings); |
||||
|
|
||||
|
return new ListResultDto<RatingDto>(ratingDto); |
||||
|
} |
||||
|
|
||||
|
[Authorize] |
||||
|
public virtual async Task<RatingDto> CreateAsync(string entityType, string entityId, CreateRatingInput input) |
||||
|
{ |
||||
|
var user = await CmsUserLookupService.GetByIdAsync(CurrentUser.GetId()); |
||||
|
|
||||
|
var rating = await RatingRepository.InsertAsync( |
||||
|
new Rating( |
||||
|
GuidGenerator.Create(), |
||||
|
entityType, |
||||
|
entityId, |
||||
|
input.StarCount, |
||||
|
user.Id, |
||||
|
CurrentTenant.Id |
||||
|
) |
||||
|
); |
||||
|
|
||||
|
return ObjectMapper.Map<Rating, RatingDto>(rating); |
||||
|
} |
||||
|
|
||||
|
[Authorize] |
||||
|
public virtual async Task<RatingDto> 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<Rating, RatingDto>(updatedRating); |
||||
|
} |
||||
|
|
||||
|
[Authorize] |
||||
|
public virtual async Task DeleteAsync(Guid id) |
||||
|
{ |
||||
|
var rating = await RatingRepository.GetAsync(id); |
||||
|
|
||||
|
if (rating.CreatorId != CurrentUser.GetId()) |
||||
|
{ |
||||
|
throw new AbpAuthorizationException(); |
||||
|
} |
||||
|
|
||||
|
await RatingRepository.DeleteAsync(id); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue