Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
ProjectsStatusManagement
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hasan.bahjat
ProjectsStatusManagement
Commits
86ff4de9
Commit
86ff4de9
authored
Jul 26, 2024
by
hasan khaddour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor infr. layer
parent
699e07e7
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
18 additions
and
217 deletions
+18
-217
DependencyInjection.cs
PSManagement.Infrastructure/DI/DependencyInjection.cs
+3
-13
PSManagement.Infrastructure.csproj
...agement.Infrastructure/PSManagement.Infrastructure.csproj
+5
-5
AppDbContext.cs
PSManagement.Infrastructure/Persistence/AppDbContext.cs
+0
-13
BaseRepository.cs
...Persistence/Repositories/BaseRepository/BaseRepository.cs
+0
-70
SpecificationEvaluator.cs
...nce/Repositories/BaseRepository/SpecificationEvaluator.cs
+0
-57
UsersRepository.cs
...ersistence/Repositories/UserRepository/UsersRepository.cs
+0
-52
AuthenticationService.cs
...tructure/Services/Authentication/AuthenticationService.cs
+10
-7
No files found.
PSManagement.Infrastructure/DI/DependencyInjection.cs
View file @
86ff4de9
using
Microsoft.AspNetCore.Authentication.JwtBearer
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.DependencyInjection
;
using
PSManagement.Application.Common.Services
;
using
PSManagement.Application.Contracts.Authentication
;
using
PSManagement.Application.Contracts.Authorization
;
using
PSManagement.Domain.Identity.Repositories
;
using
PSManagement.Infrastructure.Authentication
;
using
PSManagement.Infrastructure.Persistence.Repositories.UserRepository
;
using
PSManagement.Infrastructure.Services
;
using
PSManagement.Infrastructure.Services.Authentication
;
using
PSManagement.Infrastructure.Tokens
;
...
...
@@ -19,8 +20,7 @@ namespace PSManagement.Infrastructure.DI
services
.
AddAuthentication
(
configuration
)
.
AddAuthorization
()
.
AddServices
()
.
AddPersistence
();
.
AddServices
();
return
services
;
...
...
@@ -32,16 +32,6 @@ namespace PSManagement.Infrastructure.DI
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
)
{
return
services
;
...
...
PSManagement.Infrastructure/PSManagement.Infrastructure.csproj
View file @
86ff4de9
...
...
@@ -5,12 +5,11 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Migrations\" />
<Folder Include="Persistence\EntitiesConfiguration\" />
</ItemGroup>
<ItemGroup>
<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.Tools" Version="5.0.17">
<PrivateAssets>all</PrivateAssets>
...
...
@@ -24,6 +23,7 @@
<ItemGroup>
<ProjectReference Include="..\PSManagement.Application\PSManagement.Application.csproj" />
<ProjectReference Include="..\PSManagement.Contracts\PSManagement.Contracts.csproj" />
<ProjectReference Include="..\PSManagement.Domain\PSManagement.Domain.csproj" />
</ItemGroup>
</Project>
PSManagement.Infrastructure/Persistence/AppDbContext.cs
deleted
100644 → 0
View file @
699e07e7
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
{
}
}
PSManagement.Infrastructure/Persistence/Repositories/BaseRepository/BaseRepository.cs
deleted
100644 → 0
View file @
699e07e7
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
);
}
}
}
PSManagement.Infrastructure/Persistence/Repositories/BaseRepository/SpecificationEvaluator.cs
deleted
100644 → 0
View file @
699e07e7
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
;
}
}
}
PSManagement.Infrastructure/Persistence/Repositories/UserRepository/UsersRepository.cs
deleted
100644 → 0
View file @
699e07e7
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
;
}
}
}
PSManagement.Infrastructure/Services/Authentication/AuthenticationService.cs
View file @
86ff4de9
using
FluentResults
;
using
PSManagement.Application.Contracts.Authentication
;
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
System
;
using
System.Threading.Tasks
;
...
...
@@ -21,8 +24,8 @@ namespace PSManagement.Infrastructure.Services.Authentication
public
async
Task
<
Result
<
AuthenticationResult
>>
Login
(
String
email
,
String
password
)
{
User
u
=
await
_userRepository
.
GetByEmail
(
email
);
if
(
u
is
null
||
u
.
Password
!=
password
)
{
return
Result
.
Fail
<
AuthenticationResult
>(
new
Error
(
"the password or email maybe wrong!."
));
if
(
u
is
null
||
u
.
Hashed
Password
!=
password
)
{
return
Result
.
Fail
<
AuthenticationResult
>(
new
InvalidLoginDataError
(
));
}
String
token
=
_jwtTokenGenerator
.
GenerateToken
(
u
.
Id
,
u
.
FirstName
,
u
.
LastName
,
u
.
Email
);
...
...
@@ -38,21 +41,21 @@ namespace PSManagement.Infrastructure.Services.Authentication
// check if the user exist
var
u
=
await
_userRepository
.
GetByEmail
(
email
);
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
{
Email
=
email
,
FirstName
=
firstName
,
LastName
=
lastName
,
Password
=
password
Hashed
Password
=
password
});
// generate token
String
token
=
_jwtTokenGenerator
.
GenerateToken
(
2322323
,
firstName
,
lastName
,
email
);
String
token
=
_jwtTokenGenerator
.
GenerateToken
(
user
.
Id
,
firstName
,
lastName
,
email
);
return
Result
.
Ok
<
AuthenticationResult
>(
new
AuthenticationResult
{
Id
=
233233
,
Id
=
user
.
Id
,
Email
=
email
,
FirstName
=
firstName
,
LastName
=
lastName
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment