-
Notifications
You must be signed in to change notification settings - Fork 0
Api integration #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devTryer31
wants to merge
4
commits into
master
Choose a base branch
from
api_dev
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
34 changes: 34 additions & 0 deletions
34
Core/News.Application/Common/Behaviors/ValidationBehavior.cs
This file contains hidden or 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,34 @@ | ||
| using FluentValidation; | ||
| using MediatR; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace News.Application.Common.Behaviors | ||
| { | ||
| public class ValidationBehavior<TRequest, TResponce> | ||
| : IPipelineBehavior<TRequest, TResponce> where TRequest : IRequest<TResponce> | ||
| { | ||
| private readonly IEnumerable<IValidator<TRequest>> _validators; | ||
|
|
||
| public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators) => _validators = validators; | ||
|
|
||
| public Task<TResponce> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponce> next) | ||
| { | ||
| var context = new ValidationContext<TRequest>(request); | ||
| var fails = _validators | ||
| .Select(v => v.Validate(context)) | ||
| .SelectMany(res => res.Errors) | ||
| .Where(fail => fail is not null) | ||
| .ToList(); | ||
|
|
||
| if(fails.Any()) | ||
| throw new ValidationException(fails); | ||
|
|
||
| return next(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,14 +1,11 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using AutoMapper; | ||
| using AutoMapper; | ||
|
|
||
| namespace News.Application.Common.Mappings | ||
| { | ||
| [System.Obsolete("This method is obsolete. Use MapToAttribute", false)] | ||
| public interface IMapWith<T> | ||
| { | ||
| void CreateMapping(Profile profile) => profile.CreateMap(typeof(T), GetType());//TODO: check for error. | ||
| public void CreateMapping(Profile profile) | ||
| => profile.CreateMap(typeof(T), GetType()); | ||
| } | ||
| } |
This file contains hidden or 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,13 @@ | ||
| using System; | ||
|
|
||
| namespace News.Application.Common.Mappings | ||
| { | ||
| [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] | ||
| public class MapWithAttribute : Attribute | ||
| { | ||
| public Type MapSourceType { get; set; } | ||
|
|
||
| public MapWithAttribute() { } | ||
| public MapWithAttribute(Type mapWithType) => MapSourceType = mapWithType; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
12 changes: 4 additions & 8 deletions
12
Core/News.Application/News/Commands/Create/CreateNewsCommandHandler.cs
This file contains hidden or 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 hidden or 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
14 changes: 14 additions & 0 deletions
14
Core/News.Application/News/Commands/Create/CreateNewsCommandValidator.cs
This file contains hidden or 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,14 @@ | ||
| using FluentValidation; | ||
| using System; | ||
|
|
||
| namespace News.Application.News.Commands.Create | ||
| { | ||
| public class CreateNewsCommandValidator : AbstractValidator<CreateNewsCommandRequest> | ||
| { | ||
| public CreateNewsCommandValidator() | ||
| { | ||
| RuleFor(req => req.UserId).NotEqual(Guid.Empty); | ||
| RuleFor(req => req.Title).NotEmpty().MaximumLength(150); | ||
|
devTryer31 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
Core/News.Application/News/Commands/Delete/DeleteNewsCommandValidator.cs
This file contains hidden or 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,14 @@ | ||
| using FluentValidation; | ||
| using System; | ||
|
|
||
| namespace News.Application.News.Commands.Delete | ||
| { | ||
| public class DeleteNewsCommandValidator : AbstractValidator<DeleteNewsCommandRequest> | ||
| { | ||
| public DeleteNewsCommandValidator() | ||
| { | ||
| RuleFor(req => req.UserId).NotEqual(Guid.Empty); | ||
| RuleFor(req => req.Id).NotEqual(Guid.Empty); | ||
| } | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
Core/News.Application/News/Commands/Update/UpdateNewsCommandValidator.cs
This file contains hidden or 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,15 @@ | ||
| using FluentValidation; | ||
| using System; | ||
|
|
||
| namespace News.Application.News.Commands.Update | ||
| { | ||
| public class UpdateNewsCommandValidator : AbstractValidator<UpdateNewsCommandRequest> | ||
| { | ||
| public UpdateNewsCommandValidator() | ||
| { | ||
| RuleFor(req => req.UserId).NotEqual(Guid.Empty); | ||
| RuleFor(req => req.Id).NotEqual(Guid.Empty); | ||
| RuleFor(req => req.Title).MinimumLength(150).NotEmpty(); | ||
| } | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
Core/News.Application/News/Queries/GetDetails/GetNewsDetailsValidator.cs
This file contains hidden or 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,14 @@ | ||
| using FluentValidation; | ||
| using System; | ||
|
|
||
| namespace News.Application.News.Queries.GetDetails | ||
| { | ||
| public class GetNewsDetailsValidator : AbstractValidator<GetNewsDetailsRequest> | ||
| { | ||
| public GetNewsDetailsValidator() | ||
| { | ||
| RuleFor(req => req.UserId).NotEqual(Guid.Empty); | ||
| RuleFor(req => req.Id).NotEqual(Guid.Empty); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
Core/News.Application/News/Queries/GetNewsTitlesList/GetNewsTitilesListValidator.cs
This file contains hidden or 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,13 @@ | ||
| using FluentValidation; | ||
| using System; | ||
|
|
||
| namespace News.Application.News.Queries.GetNewsTitlesList | ||
| { | ||
| public class GetNewsTitilesListValidator : AbstractValidator<GetNewsTitilesListRequest> | ||
| { | ||
| public GetNewsTitilesListValidator() | ||
| { | ||
| RuleFor(req => req.UserId).NotEqual(Guid.Empty); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| using Microsoft.AspNetCore.Identity; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using System; | ||
| using System.Security.Claims; | ||
|
|
||
| namespace News.WebAPI.Controllers | ||
| { | ||
| [ApiController] | ||
| [Route("api/[controller]/[action]")] | ||
| public class BaseApiController : ControllerBase | ||
| { | ||
| private MediatR.IMediator _mediator; | ||
|
|
||
| protected MediatR.IMediator Mediator => | ||
| _mediator ??= HttpContext.RequestServices.GetService<MediatR.IMediator>(); | ||
|
|
||
| internal Guid UserId => | ||
| !User.Identity.IsAuthenticated ? | ||
| Guid.Empty : | ||
| Guid.Parse( | ||
| HttpContext.RequestServices.GetService<UserManager<IdentityUser>>().GetUserId(User) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be mistake in using UserManager because there may be another implementation of IdentityUser. |
||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.