diff --git a/AnalysisData/AnalysisData.sln b/AnalysisData/AnalysisData.sln index 253a37f..732ee9d 100644 --- a/AnalysisData/AnalysisData.sln +++ b/AnalysisData/AnalysisData.sln @@ -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 @@ -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 diff --git a/AnalysisData/AnalysisData/AnalysisData.csproj b/AnalysisData/AnalysisData/AnalysisData.csproj index fe1cb9e..52fa80b 100644 --- a/AnalysisData/AnalysisData/AnalysisData.csproj +++ b/AnalysisData/AnalysisData/AnalysisData.csproj @@ -22,4 +22,8 @@ + + + + diff --git a/AnalysisData/AnalysisData/JwtService/Controllers/IdentifyControllers.cs b/AnalysisData/AnalysisData/JwtService/Controllers/IdentifyControllers.cs index f3b486f..6391116 100644 --- a/AnalysisData/AnalysisData/JwtService/Controllers/IdentifyControllers.cs +++ b/AnalysisData/AnalysisData/JwtService/Controllers/IdentifyControllers.cs @@ -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]")] diff --git a/AnalysisData/AnalysisData/JwtService/JwtService.cs b/AnalysisData/AnalysisData/JwtService/JwtService.cs index fc31a45..cc72470 100644 --- a/AnalysisData/AnalysisData/JwtService/JwtService.cs +++ b/AnalysisData/AnalysisData/JwtService/JwtService.cs @@ -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 { @@ -31,7 +31,7 @@ public async Task 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)); } diff --git a/AnalysisData/AnalysisData/Migrations/20240812200637_InitialCreate.Designer.cs b/AnalysisData/AnalysisData/Migrations/20240813052823_InitialCreate.Designer.cs similarity index 98% rename from AnalysisData/AnalysisData/Migrations/20240812200637_InitialCreate.Designer.cs rename to AnalysisData/AnalysisData/Migrations/20240813052823_InitialCreate.Designer.cs index 051ab95..e196933 100644 --- a/AnalysisData/AnalysisData/Migrations/20240812200637_InitialCreate.Designer.cs +++ b/AnalysisData/AnalysisData/Migrations/20240813052823_InitialCreate.Designer.cs @@ -11,7 +11,7 @@ namespace AnalysisData.Migrations { [DbContext(typeof(ApplicationDbContext))] - [Migration("20240812200637_InitialCreate")] + [Migration("20240813052823_InitialCreate")] partial class InitialCreate { /// diff --git a/AnalysisData/AnalysisData/Migrations/20240812200637_InitialCreate.cs b/AnalysisData/AnalysisData/Migrations/20240813052823_InitialCreate.cs similarity index 100% rename from AnalysisData/AnalysisData/Migrations/20240812200637_InitialCreate.cs rename to AnalysisData/AnalysisData/Migrations/20240813052823_InitialCreate.cs diff --git a/AnalysisData/AnalysisData/Program.cs b/AnalysisData/AnalysisData/Program.cs index e2c417b..1b3fab1 100644 --- a/AnalysisData/AnalysisData/Program.cs +++ b/AnalysisData/AnalysisData/Program.cs @@ -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); diff --git a/AnalysisData/AnalysisData/Repository/RoleRepository/Abstraction/IRoleRepository.cs b/AnalysisData/AnalysisData/Repository/RoleRepository/Abstraction/IRoleRepository.cs new file mode 100644 index 0000000..5f68dc0 --- /dev/null +++ b/AnalysisData/AnalysisData/Repository/RoleRepository/Abstraction/IRoleRepository.cs @@ -0,0 +1,10 @@ +using AnalysisData.UserManage.Model; + +namespace AnalysisData.Repository.RoleRepository.Abstraction; + +public interface IRoleRepository +{ + Task GetRole(int rolId); + bool AddRole(Role role); + bool DeleteRole(int roleId); +} \ No newline at end of file diff --git a/AnalysisData/AnalysisData/Repository/RoleRepository/RoleRepository.cs b/AnalysisData/AnalysisData/Repository/RoleRepository/RoleRepository.cs new file mode 100644 index 0000000..26d2cd5 --- /dev/null +++ b/AnalysisData/AnalysisData/Repository/RoleRepository/RoleRepository.cs @@ -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 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; + } +} \ No newline at end of file diff --git a/AnalysisData/AnalysisData/UserRepositories/Abstraction/IUserRepository.cs b/AnalysisData/AnalysisData/Repository/UserRepository/Abstraction/IUserRepository.cs similarity index 78% rename from AnalysisData/AnalysisData/UserRepositories/Abstraction/IUserRepository.cs rename to AnalysisData/AnalysisData/Repository/UserRepository/Abstraction/IUserRepository.cs index da816d5..26c113d 100644 --- a/AnalysisData/AnalysisData/UserRepositories/Abstraction/IUserRepository.cs +++ b/AnalysisData/AnalysisData/Repository/UserRepository/Abstraction/IUserRepository.cs @@ -1,6 +1,6 @@ using AnalysisData.UserManage.Model; -namespace AnalysisData.UserRepositories.Abstraction; +namespace AnalysisData.Repository.UserRepository.Abstraction; public interface IUserRepository { diff --git a/AnalysisData/AnalysisData/UserRepositories/UserRepository.cs b/AnalysisData/AnalysisData/Repository/UserRepository/UserRepository.cs similarity index 74% rename from AnalysisData/AnalysisData/UserRepositories/UserRepository.cs rename to AnalysisData/AnalysisData/Repository/UserRepository/UserRepository.cs index 3ce6b4f..3c307c9 100644 --- a/AnalysisData/AnalysisData/UserRepositories/UserRepository.cs +++ b/AnalysisData/AnalysisData/Repository/UserRepository/UserRepository.cs @@ -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 { @@ -30,13 +28,10 @@ public async Task> 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) diff --git a/AnalysisData/AnalysisData/Repository/UserRoleRepository/Abstraction/IUserRoleRepository.cs b/AnalysisData/AnalysisData/Repository/UserRoleRepository/Abstraction/IUserRoleRepository.cs new file mode 100644 index 0000000..f43d35a --- /dev/null +++ b/AnalysisData/AnalysisData/Repository/UserRoleRepository/Abstraction/IUserRoleRepository.cs @@ -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); +} \ No newline at end of file diff --git a/AnalysisData/AnalysisData/Repository/UserRoleRepository/UserRoleRepository.cs b/AnalysisData/AnalysisData/Repository/UserRoleRepository/UserRoleRepository.cs new file mode 100644 index 0000000..1669a4f --- /dev/null +++ b/AnalysisData/AnalysisData/Repository/UserRoleRepository/UserRoleRepository.cs @@ -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; + } +} \ No newline at end of file diff --git a/AnalysisData/AnalysisData/RoleRepository/IRoleRepository.cs b/AnalysisData/AnalysisData/RoleRepository/IRoleRepository.cs deleted file mode 100644 index f9d30f7..0000000 --- a/AnalysisData/AnalysisData/RoleRepository/IRoleRepository.cs +++ /dev/null @@ -1,8 +0,0 @@ -using AnalysisData.UserManage.Model; - -namespace AnalysisData.RoleRepository; - -public interface IRoleRepository -{ - Task GetRoles(int rolId); -} \ No newline at end of file diff --git a/AnalysisData/AnalysisData/RoleRepository/RoleRepository.cs b/AnalysisData/AnalysisData/RoleRepository/RoleRepository.cs deleted file mode 100644 index 653a64a..0000000 --- a/AnalysisData/AnalysisData/RoleRepository/RoleRepository.cs +++ /dev/null @@ -1,19 +0,0 @@ -using AnalysisData.Data; -using AnalysisData.UserManage.Model; -using Microsoft.EntityFrameworkCore; - -namespace AnalysisData.RoleRepository; - -public class RoleRepository : IRoleRepository -{ - private readonly ApplicationDbContext _context; - - public RoleRepository(ApplicationDbContext context) - { - _context = context; - } - public async Task GetRoles(int rolId) - { - return await _context.Roles.SingleOrDefaultAsync(x => x.Id == rolId); - } -} \ No newline at end of file diff --git a/AnalysisData/AnalysisData/UserManage/Model/Role.cs b/AnalysisData/AnalysisData/UserManage/Model/Role.cs index 7987eb3..f58bbca 100644 --- a/AnalysisData/AnalysisData/UserManage/Model/Role.cs +++ b/AnalysisData/AnalysisData/UserManage/Model/Role.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; -using AnalysisData.UserManage.Model; + +namespace AnalysisData.UserManage.Model; public class Role { diff --git a/AnalysisData/AnalysisData/UserManage/Model/UserRole.cs b/AnalysisData/AnalysisData/UserManage/Model/UserRole.cs index 709367c..fd36e96 100644 --- a/AnalysisData/AnalysisData/UserManage/Model/UserRole.cs +++ b/AnalysisData/AnalysisData/UserManage/Model/UserRole.cs @@ -1,6 +1,7 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using AnalysisData.UserManage.Model; + +namespace AnalysisData.UserManage.Model; public class UserRole { diff --git a/AnalysisData/AnalysisData/appsettings.json b/AnalysisData/AnalysisData/appsettings.json index e933368..8811fce 100644 --- a/AnalysisData/AnalysisData/appsettings.json +++ b/AnalysisData/AnalysisData/appsettings.json @@ -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": "*" diff --git a/AnalysisData/TestProject/TestProject.csproj b/AnalysisData/TestProject/TestProject.csproj new file mode 100644 index 0000000..085bddd --- /dev/null +++ b/AnalysisData/TestProject/TestProject.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + diff --git a/AnalysisData/TestProject/UnitTest1.cs b/AnalysisData/TestProject/UnitTest1.cs new file mode 100644 index 0000000..7e0552f --- /dev/null +++ b/AnalysisData/TestProject/UnitTest1.cs @@ -0,0 +1,9 @@ +namespace TestProject; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + } +} \ No newline at end of file