-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from quizer-app/develop
Add GetQuizByName
- Loading branch information
Showing
23 changed files
with
668 additions
and
65 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
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
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
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
9 changes: 0 additions & 9 deletions
9
src/Quizer.Application/Quizes/Queries/GetQuiz/GetQuizQuery.cs
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
src/Quizer.Application/Quizes/Queries/GetQuiz/GetQuizQueryHandler.cs
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/Quizer.Application/Quizes/Queries/GetQuizById/GetQuizByIdQuery.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,8 @@ | ||
using ErrorOr; | ||
using MediatR; | ||
using Quizer.Domain.QuizAggregate; | ||
|
||
namespace Quizer.Application.Quizes.Queries.GetQuizById; | ||
|
||
public record GetQuizByIdQuery( | ||
Guid QuizId) : IRequest<ErrorOr<Quiz>>; |
24 changes: 24 additions & 0 deletions
24
src/Quizer.Application/Quizes/Queries/GetQuizById/GetQuizByIdQueryHandler.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,24 @@ | ||
using ErrorOr; | ||
using MediatR; | ||
using Quizer.Application.Common.Interfaces.Persistance; | ||
using Quizer.Domain.Common.Errors; | ||
using Quizer.Domain.QuizAggregate; | ||
|
||
namespace Quizer.Application.Quizes.Queries.GetQuizById; | ||
|
||
public class GetQuizByIdQueryHandler : IRequestHandler<GetQuizByIdQuery, ErrorOr<Quiz>> | ||
{ | ||
private readonly IQuizRepository _quizRepository; | ||
|
||
public GetQuizByIdQueryHandler(IQuizRepository quizRepository) | ||
{ | ||
_quizRepository = quizRepository; | ||
} | ||
|
||
public async Task<ErrorOr<Quiz>> Handle(GetQuizByIdQuery request, CancellationToken cancellationToken) | ||
{ | ||
var quiz = await _quizRepository.Get(QuizId.Create(request.QuizId)); | ||
if (quiz is null) return Errors.Quiz.NotFound; | ||
return quiz; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Quizer.Application/Quizes/Queries/GetQuizByName/GetQuizByNameQuery.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 @@ | ||
using ErrorOr; | ||
using MediatR; | ||
using Quizer.Domain.QuizAggregate; | ||
|
||
namespace Quizer.Application.Quizes.Queries.GetQuizByName; | ||
|
||
public record GetQuizByNameQuery( | ||
string UserName, | ||
string QuizName) : IRequest<ErrorOr<Quiz>>; |
26 changes: 26 additions & 0 deletions
26
src/Quizer.Application/Quizes/Queries/GetQuizByName/GetQuizByNameQueryHandler.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,26 @@ | ||
using ErrorOr; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Identity; | ||
using Quizer.Application.Common.Interfaces.Persistance; | ||
using Quizer.Domain.Common.Errors; | ||
using Quizer.Domain.QuizAggregate; | ||
|
||
namespace Quizer.Application.Quizes.Queries.GetQuizByName; | ||
|
||
public class GetQuizByNameQueryHandler : IRequestHandler<GetQuizByNameQuery, ErrorOr<Quiz>> | ||
{ | ||
private readonly IQuizRepository _quizRepository; | ||
|
||
public GetQuizByNameQueryHandler(IQuizRepository quizRepository) | ||
{ | ||
_quizRepository = quizRepository; | ||
} | ||
|
||
public async Task<ErrorOr<Quiz>> Handle(GetQuizByNameQuery request, CancellationToken cancellationToken) | ||
{ | ||
var quiz = await _quizRepository.Get(request.UserName, request.QuizName); | ||
if (quiz is null) return Errors.Quiz.NotFound; | ||
|
||
return quiz; | ||
} | ||
} |
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,17 @@ | ||
using Diacritics; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Quizer.Application.Services.Slugify; | ||
|
||
namespace Quizer.Application.Services; | ||
|
||
public static class DependencyInjection | ||
{ | ||
public static IServiceCollection AddServices(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<IDiacriticsMapper, DefaultDiacriticsMapper>(); | ||
services.AddSingleton<ISlugifyService, SlugifyService>(); | ||
|
||
return services; | ||
} | ||
} | ||
|
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,7 @@ | ||
namespace Quizer.Application.Services.Slugify | ||
{ | ||
public interface ISlugifyService | ||
{ | ||
string GenerateSlug(string str); | ||
} | ||
} |
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,71 @@ | ||
using System.Text.RegularExpressions; | ||
using System.Text; | ||
using Diacritics; | ||
|
||
namespace Quizer.Application.Services.Slugify | ||
{ | ||
public class SlugifyService : ISlugifyService | ||
{ | ||
protected readonly Config _config; | ||
protected readonly IDiacriticsMapper _diacriticsMapper; | ||
|
||
public SlugifyService(IDiacriticsMapper diacriticsMapper) | ||
{ | ||
_config = new Config(); | ||
_diacriticsMapper = diacriticsMapper; | ||
} | ||
|
||
public string GenerateSlug(string str) | ||
{ | ||
if (_config.ForceLowerCase) | ||
str = str.ToLower(); | ||
|
||
str = CleanWhiteSpace(str, _config.CollapseWhiteSpace); | ||
str = ApplyReplacements(str, _config.CharacterReplacements); | ||
str = _diacriticsMapper.RemoveDiacritics(str); | ||
str = DeleteCharacters(str, _config.DeniedCharactersRegex); | ||
|
||
return str; | ||
} | ||
|
||
protected string CleanWhiteSpace(string str, bool collapse) | ||
{ | ||
return Regex.Replace(str, collapse ? @"\s+" : @"\s", " "); | ||
} | ||
|
||
protected string ApplyReplacements(string str, Dictionary<string, string> replacements) | ||
{ | ||
StringBuilder sb = new StringBuilder(str); | ||
|
||
foreach (KeyValuePair<string, string> replacement in replacements) | ||
sb.Replace(replacement.Key, replacement.Value); | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
protected string DeleteCharacters(string str, string regex) | ||
{ | ||
return Regex.Replace(str, regex, ""); | ||
} | ||
|
||
public class Config | ||
{ | ||
public Dictionary<string, string> CharacterReplacements { get; set; } | ||
public bool ForceLowerCase { get; set; } | ||
public bool CollapseWhiteSpace { get; set; } | ||
public string DeniedCharactersRegex { get; set; } | ||
|
||
public Config() | ||
{ | ||
CharacterReplacements = new Dictionary<string, string> | ||
{ | ||
{ " ", "-" } | ||
}; | ||
|
||
ForceLowerCase = true; | ||
CollapseWhiteSpace = true; | ||
DeniedCharactersRegex = @"[^a-zA-Z0-9\-\._]"; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.