-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bugs and allow upsert of feedback for other users by admins
- Loading branch information
1 parent
ba43a37
commit 3bddcbb
Showing
5 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...Gameboard.Api/Features/Feedback/Requests/UpdateFeedbackTemplate/UpdateFeedbackTemplate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using Gameboard.Api.Data; | ||
using Gameboard.Api.Services; | ||
using Gameboard.Api.Structure.MediatR; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Gameboard.Api.Features.Feedback; | ||
|
||
public record UpdateFeedbackTemplateCommand(UpdateFeedbackTemplateRequest Request) : IRequest<FeedbackTemplateView>; | ||
|
||
internal sealed class UpdateFeedbackTemplateHandler | ||
( | ||
IMapper mapper, | ||
IStore store, | ||
IValidatorService validator | ||
) : IRequestHandler<UpdateFeedbackTemplateCommand, FeedbackTemplateView> | ||
{ | ||
private readonly IMapper _mapper = mapper; | ||
private readonly IStore _store = store; | ||
private readonly IValidatorService _validator = validator; | ||
|
||
public async Task<FeedbackTemplateView> Handle(UpdateFeedbackTemplateCommand request, CancellationToken cancellationToken) | ||
{ | ||
await _validator | ||
.Auth(c => c.RequirePermissions(Users.PermissionKey.Games_CreateEditDelete)) | ||
.AddEntityExistsValidator<FeedbackTemplate>(request.Request.Id) | ||
.AddValidator(request.Request.Name.IsEmpty(), new MissingRequiredInput<string>(nameof(request.Request.Name))) | ||
.AddValidator(request.Request.Content.IsEmpty(), new MissingRequiredInput<string>(nameof(request.Request.Content))) | ||
.AddValidator(async ctx => | ||
{ | ||
if (await _store.WithNoTracking<FeedbackTemplate>().AnyAsync(t => t.Name == request.Request.Name && t.Id != request.Request.Id)) | ||
{ | ||
ctx.AddValidationException(new DuplicateFeedbackTemplateNameException(request.Request.Name)); | ||
} | ||
}) | ||
.Validate(cancellationToken); | ||
|
||
await _store | ||
.WithNoTracking<FeedbackTemplate>() | ||
.Where(t => t.Id == request.Request.Id) | ||
.ExecuteUpdateAsync | ||
( | ||
up => up | ||
.SetProperty(t => t.HelpText, request.Request.HelpText) | ||
.SetProperty(t => t.Content, request.Request.Content) | ||
.SetProperty(t => t.Name, request.Request.Name), | ||
cancellationToken | ||
); | ||
|
||
return await _mapper | ||
.ProjectTo<FeedbackTemplateView>(_store.WithNoTracking<FeedbackTemplate>().Where(t => t.Id == request.Request.Id)) | ||
.SingleAsync(cancellationToken); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ard.Api/Features/Feedback/Requests/UpdateFeedbackTemplate/UpdateFeedbackTemplateModels.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Gameboard.Api.Features.Feedback; | ||
|
||
public sealed class UpdateFeedbackTemplateRequest | ||
{ | ||
public required string Id { get; set; } | ||
public required string Content { get; set; } | ||
public string HelpText { get; set; } | ||
public required string Name { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters