Commit 86ff4de9 authored by hasan khaddour's avatar hasan khaddour

refactor infr. layer

parent 699e07e7
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using PSManagement.Application.Common.Services; using PSManagement.Application.Common.Services;
using PSManagement.Application.Contracts.Authentication; using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Authorization; using PSManagement.Application.Contracts.Authorization;
using PSManagement.Domain.Identity.Repositories;
using PSManagement.Infrastructure.Authentication; using PSManagement.Infrastructure.Authentication;
using PSManagement.Infrastructure.Persistence.Repositories.UserRepository;
using PSManagement.Infrastructure.Services; using PSManagement.Infrastructure.Services;
using PSManagement.Infrastructure.Services.Authentication; using PSManagement.Infrastructure.Services.Authentication;
using PSManagement.Infrastructure.Tokens; using PSManagement.Infrastructure.Tokens;
...@@ -19,8 +20,7 @@ namespace PSManagement.Infrastructure.DI ...@@ -19,8 +20,7 @@ namespace PSManagement.Infrastructure.DI
services services
.AddAuthentication(configuration) .AddAuthentication(configuration)
.AddAuthorization() .AddAuthorization()
.AddServices() .AddServices();
.AddPersistence();
return services; return services;
...@@ -32,16 +32,6 @@ namespace PSManagement.Infrastructure.DI ...@@ -32,16 +32,6 @@ namespace PSManagement.Infrastructure.DI
return services; return services;
} }
private static IServiceCollection AddPersistence(this IServiceCollection services)
{
//services.AddDbContext<AppDbContext>(options => options.UseSqlite("Data Source = CleanArchitecture.sqlite"));
//services.AddScoped<IRemindersRepository, RemindersRepository>();
services.AddScoped<IUsersRepository, UsersRepository>();
return services;
}
private static IServiceCollection AddAuthorization(this IServiceCollection services) private static IServiceCollection AddAuthorization(this IServiceCollection services)
{ {
return services; return services;
......
...@@ -5,12 +5,11 @@ ...@@ -5,12 +5,11 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Migrations\" />
<Folder Include="Persistence\EntitiesConfiguration\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.17" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.17" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.17"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.17">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
...@@ -24,6 +23,7 @@ ...@@ -24,6 +23,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\PSManagement.Application\PSManagement.Application.csproj" /> <ProjectReference Include="..\PSManagement.Application\PSManagement.Application.csproj" />
<ProjectReference Include="..\PSManagement.Contracts\PSManagement.Contracts.csproj" /> <ProjectReference Include="..\PSManagement.Contracts\PSManagement.Contracts.csproj" />
<ProjectReference Include="..\PSManagement.Domain\PSManagement.Domain.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Common.Persistence
{
public class AppDbContext : DbContext
{
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using PSManagement.Infrastructure.Common.Persistence;
using PSManagement.SharedKernel.Entities;
using PSManagement.SharedKernel.Interfaces;
using PSManagement.SharedKernel.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Persistence.Repositories.BaseRepository
{
public class BaseRepository<T> : IRepository<T> where T :BaseEntity
{
protected AppDbContext _dbContext;
internal DbSet<T> dbSet;
public BaseRepository(AppDbContext context)
{
_dbContext = context;
dbSet = context.Set<T>();
}
public async Task<T> AddAsync(T entity)
{
EntityEntry<T> entry = await dbSet.AddAsync(entity);
await _dbContext.SaveChangesAsync();
return entry.Entity;
}
public async Task<IEnumerable<T>> ListAsync()
{
return await dbSet.ToListAsync();
}
public async Task<IEnumerable<T>> ListAsync(ISpecification<T> specification)
{
var q = ApplySpecification(specification);
return await q.ToListAsync<T>();
}
public Task DeleteAsync(T entity)
{
_dbContext.Set<T>().Remove(entity);
return _dbContext.SaveChangesAsync();
}
public async Task<T> GetByIdAsync(int id, ISpecification<T> specification=null)
{
var q = ApplySpecification(specification);
return await q.SingleOrDefaultAsync(e => e.Id == id);
}
public async Task<T> UpdateAsync(T entity)
{
_dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
return entity;
}
private IQueryable<T> ApplySpecification(ISpecification<T> specification)
{
return SpecificationEvaluator<T>.GetQuery(dbSet.AsQueryable(), specification);
}
}
}
using PSManagement.SharedKernel.Entities;
using PSManagement.SharedKernel.Interfaces;
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace PSManagement.Infrastructure.Persistence.Repositories.BaseRepository
{
public class SpecificationEvaluator<T> where T : BaseEntity
{
public static IQueryable<T> GetQuery(IQueryable<T> inputQuery, ISpecification<T> specification)
{
var query = inputQuery;
// modify the IQueryable using the specification's criteria expression
if (specification.Criteria != null)
{
query = query.Where(specification.Criteria);
}
// Includes all expression-based includes
query = specification.Includes.Aggregate(
query,
(current, include) => current.Include(include));
// Include any string-based include statements
query = specification.IncludeStrings.Aggregate(
query,
(current, include) => current.Include(include));
// Apply ordering if expressions are set
if (specification.OrderBy != null)
{
query = query.OrderBy(specification.OrderBy);
}
else if (specification.OrderByDescending != null)
{
query = query.OrderByDescending(specification.OrderByDescending);
}
//if (specification.GroupBy is not null)
//{
// query = query.GroupBy(specification.GroupBy).SelectMany(x => x);
//}
// Apply paging if enabled
if (specification.IsPagingEnabled)
{
query = query.Skip(specification.Skip)
.Take(specification.Take);
}
return query;
}
}
}
using PSManagement.Application.Contracts.Authentication;
using PSManagement.SharedKernel.Interfaces;
using PSManagement.SharedKernel.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.Infrastructure.Persistence.Repositories.UserRepository
{
public class UsersRepository : IUsersRepository
{
public static List<User> Users { get; set; } = new List<User>();
public async Task<User> AddAsync(User entity)
{
Users.Add(entity);
return entity;
}
public async Task DeleteAsync(User entity)
{
Users.Remove(entity);
}
public async Task<User> GetByEmail(string email)
{
return Users.Where(u => u.Email == email).FirstOrDefault();
}
public async Task<User> GetByIdAsync(int id, ISpecification<User> specification = null)
{
return Users.Where(u=>u.Id == id).FirstOrDefault();
}
public async Task<IEnumerable<User>> ListAsync()
{
return Users;
}
public async Task<IEnumerable<User>> ListAsync(ISpecification<User> spec)
{
return new List<User>();
}
public async Task<User> UpdateAsync(User entity)
{
return entity;
}
}
}
using FluentResults; using FluentResults;
using PSManagement.Application.Contracts.Authentication; using PSManagement.Application.Contracts.Authentication;
using PSManagement.Application.Contracts.Authorization; using PSManagement.Application.Contracts.Authorization;
using PSManagement.Domain.Identity.Aggregate;
using PSManagement.Domain.Identity.DomainErrors;
using PSManagement.Domain.Identity.Repositories;
//using PSManagement.SharedKernel.Utilities; //using PSManagement.SharedKernel.Utilities;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -21,8 +24,8 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -21,8 +24,8 @@ namespace PSManagement.Infrastructure.Services.Authentication
public async Task<Result<AuthenticationResult>> Login(String email, String password) { public async Task<Result<AuthenticationResult>> Login(String email, String password) {
User u = await _userRepository.GetByEmail(email); User u = await _userRepository.GetByEmail(email);
if (u is null || u.Password != password) { if (u is null || u.HashedPassword != password) {
return Result.Fail<AuthenticationResult>(new Error( "the password or email maybe wrong!.")); return Result.Fail<AuthenticationResult>(new InvalidLoginDataError());
} }
String token = _jwtTokenGenerator.GenerateToken(u.Id,u.FirstName,u.LastName,u.Email); String token = _jwtTokenGenerator.GenerateToken(u.Id,u.FirstName,u.LastName,u.Email);
...@@ -38,21 +41,21 @@ namespace PSManagement.Infrastructure.Services.Authentication ...@@ -38,21 +41,21 @@ namespace PSManagement.Infrastructure.Services.Authentication
// check if the user exist // check if the user exist
var u = await _userRepository.GetByEmail(email); var u = await _userRepository.GetByEmail(email);
if (u is not null) { if (u is not null) {
return Result.Fail<AuthenticationResult>(new Error("the user already exist ")); return Result.Fail<AuthenticationResult>(new AlreadyExistError());
} }
await _userRepository.AddAsync( var user = await _userRepository.AddAsync(
new User{ new User{
Email=email , Email=email ,
FirstName=firstName, FirstName=firstName,
LastName=lastName, LastName=lastName,
Password=password HashedPassword=password
}); });
// generate token // generate token
String token = _jwtTokenGenerator.GenerateToken(2322323,firstName,lastName,email); String token = _jwtTokenGenerator.GenerateToken(user.Id,firstName,lastName,email);
return Result.Ok<AuthenticationResult>( return Result.Ok<AuthenticationResult>(
new AuthenticationResult new AuthenticationResult
{ {
Id = 233233, Id = user.Id,
Email = email, Email = email,
FirstName = firstName, FirstName = firstName,
LastName = lastName, LastName = lastName,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment