Skip to content

Commit

Permalink
feat(User): Implement UserRoleRepository.
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Kabiri committed Aug 13, 2024
1 parent 6c645b5 commit 220a93e
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 58 deletions.
6 changes: 6 additions & 0 deletions AnalysisData/AnalysisData.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnalysisData", "AnalysisData\AnalysisData.csproj", "{1AC76299-2D1E-4438-B67B-AA6315B6B781}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject", "TestProject\TestProject.csproj", "{9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{1AC76299-2D1E-4438-B67B-AA6315B6B781}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AC76299-2D1E-4438-B67B-AA6315B6B781}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AC76299-2D1E-4438-B67B-AA6315B6B781}.Release|Any CPU.Build.0 = Release|Any CPU
{9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions AnalysisData/AnalysisData/AnalysisData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="Repository\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using AnalysisData.UserManage.Model;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.Data;
using Microsoft.AspNetCore.Mvc;

namespace Authentication.Controllers;
namespace AnalysisData.JwtService.Controllers;

[ApiController]
[Route("api/[controller]")]
Expand Down
8 changes: 4 additions & 4 deletions AnalysisData/AnalysisData/JwtService/JwtService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using AnalysisData.RoleRepository;
using AnalysisData.UserRepositories.Abstraction;
using AnalysisData.Repository.RoleRepository.Abstraction;
using AnalysisData.Repository.UserRepository.Abstraction;
using Microsoft.IdentityModel.Tokens;

namespace Authentication;
namespace AnalysisData.JwtService;

public class JwtService
{
Expand All @@ -31,7 +31,7 @@ public async Task<string> GenerateJwtToken(string userName)
};
foreach (var role in roles)
{
var result = await _roleRepository.GetRoles(role.Id);
var result = await _roleRepository.GetRole(role.Id);
claims.Add(new Claim(ClaimTypes.Role, result.RoleName));
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions AnalysisData/AnalysisData/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using AnalysisData.Data;
using AnalysisData.RoleRepository;
using AnalysisData.UserRepositories;
using AnalysisData.UserRepositories.Abstraction;
using Authentication;
using AnalysisData.JwtService;
using AnalysisData.Repository.RoleRepository;
using AnalysisData.Repository.RoleRepository.Abstraction;
using AnalysisData.Repository.UserRepository;
using AnalysisData.Repository.UserRepository.Abstraction;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using AnalysisData.UserManage.Model;

namespace AnalysisData.Repository.RoleRepository.Abstraction;

public interface IRoleRepository
{
Task<Role> GetRole(int rolId);
bool AddRole(Role role);
bool DeleteRole(int roleId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AnalysisData.Data;
using AnalysisData.Repository.RoleRepository.Abstraction;
using AnalysisData.UserManage.Model;
using Microsoft.EntityFrameworkCore;

namespace AnalysisData.Repository.RoleRepository;

public class RoleRepository : IRoleRepository
{
private readonly ApplicationDbContext _context;

public RoleRepository(ApplicationDbContext context)
{
_context = context;
}

public async Task<Role> GetRole(int rolId)
{
return await _context.Roles.SingleOrDefaultAsync(x => x.Id == rolId);
}

public bool AddRole(Role role)
{
_context.Roles.Add(role);
_context.SaveChanges();
return true;
}

public bool DeleteRole(int roleId)
{
var role = _context.Roles.FirstOrDefault(x => x.Id == roleId);
if (role == null) return false;
_context.Roles.Remove(role);
_context.SaveChanges();
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AnalysisData.UserManage.Model;

namespace AnalysisData.UserRepositories.Abstraction;
namespace AnalysisData.Repository.UserRepository.Abstraction;

public interface IUserRepository
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using AnalysisData.Data;
using AnalysisData.Repository.UserRepository.Abstraction;
using AnalysisData.UserManage.Model;
using AnalysisData.UserRepositories.Abstraction;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AnalysisData.UserRepositories
namespace AnalysisData.Repository.UserRepository
{
public class UserRepository : IUserRepository
{
Expand All @@ -30,13 +28,10 @@ public async Task<IReadOnlyList<User>> GetAllUser()
public bool DeleteUser(string userName)
{
var user = _context.Users.FirstOrDefault(x => x.Username == userName);
if (user != null)
{
_context.Users.Remove(user);
_context.SaveChanges();
return true;
}
return false;
if (user == null) return false;
_context.Users.Remove(user);
_context.SaveChanges();
return true;
}

public bool AddUser(User user)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using AnalysisData.UserManage.Model;

namespace AnalysisData.Repository.UserRoleRepository.Abstraction;

public interface IUserRoleRepository
{
bool Add(UserRole userRole);
bool DeleteUserInUserRole(int userId);
bool DeleteRoleInUserRole(int roleId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using AnalysisData.Data;
using AnalysisData.Repository.UserRoleRepository.Abstraction;
using AnalysisData.UserManage.Model;

namespace AnalysisData.Repository.UserRoleRepository;

public class UserRoleRepository : IUserRoleRepository
{
private readonly ApplicationDbContext _context;

public UserRoleRepository(ApplicationDbContext context)
{
_context = context;
}

public bool Add(UserRole userRole)
{
_context.UserRoles.Add(userRole);
_context.SaveChanges();
return true;
}

public bool DeleteUserInUserRole(int userId)
{
var recordOfTable = _context.UserRoles.Where(x => x.UserId == userId);
_context.UserRoles.RemoveRange(recordOfTable);
_context.SaveChanges();
return true;
}

public bool DeleteRoleInUserRole(int roleId)
{
var recordOfTable = _context.UserRoles.Where(x => x.RoleId == roleId);
_context.UserRoles.RemoveRange(recordOfTable);
_context.SaveChanges();
return true;
}
}
8 changes: 0 additions & 8 deletions AnalysisData/AnalysisData/RoleRepository/IRoleRepository.cs

This file was deleted.

19 changes: 0 additions & 19 deletions AnalysisData/AnalysisData/RoleRepository/RoleRepository.cs

This file was deleted.

3 changes: 2 additions & 1 deletion AnalysisData/AnalysisData/UserManage/Model/Role.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using AnalysisData.UserManage.Model;

namespace AnalysisData.UserManage.Model;

public class Role
{
Expand Down
3 changes: 2 additions & 1 deletion AnalysisData/AnalysisData/UserManage/Model/UserRole.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using AnalysisData.UserManage.Model;

namespace AnalysisData.UserManage.Model;

public class UserRole
{
Expand Down
2 changes: 1 addition & 1 deletion AnalysisData/AnalysisData/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=mohaymen;Username=postgres;Password=mahdijm;Timeout=300"
"DefaultConnection": "Host=localhost;Database=mohaymen;Username=postgres;Password=@Database20;Timeout=300"
},

"AllowedHosts": "*"
Expand Down
23 changes: 23 additions & 0 deletions AnalysisData/TestProject/TestProject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions AnalysisData/TestProject/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TestProject;

public class UnitTest1
{
[Fact]
public void Test1()
{
}
}

0 comments on commit 220a93e

Please sign in to comment.