Compare commits
3 Commits
b811be2226
...
dc3fb100b1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc3fb100b1 | ||
|
|
556571ae78 | ||
|
|
cedf5f0ca8 |
@ -1,11 +1,11 @@
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System.Security.Claims;
|
||||
using UserManagement.Application.Dtos.Auth;
|
||||
using UserManagement.Application.Interfaces;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
|
||||
namespace UserManagement.API.Controllers
|
||||
{
|
||||
@ -29,8 +29,8 @@ namespace UserManagement.API.Controllers
|
||||
public async Task<IActionResult> Login([FromBody] LoginDto login)
|
||||
{
|
||||
// Validate user
|
||||
var user = await _userService.GetByUsernameAsync(login.Username);
|
||||
if (user == null)
|
||||
var user = await _userService.GetByUsernameAsync(login.Username, includeRoles: true);
|
||||
if (user is null)
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
@ -49,9 +49,13 @@ namespace UserManagement.API.Controllers
|
||||
new Claim(ClaimTypes.Name, user.UserName),
|
||||
new Claim(ClaimTypes.Surname, user.LastName ?? ""),
|
||||
new Claim(ClaimTypes.GivenName, user.FirstName ?? ""),
|
||||
new Claim(ClaimTypes.Role, user?.Role?.Name.ToString() ?? "")
|
||||
};
|
||||
|
||||
foreach (var userRole in user.UserRoles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, userRole!.Name));
|
||||
}
|
||||
|
||||
// Create a ClaimsIdentity based on the created claims
|
||||
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
|
||||
@ -82,5 +86,10 @@ namespace UserManagement.API.Controllers
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// AUTH CHECK
|
||||
[HttpGet]
|
||||
[SwaggerOperation(Summary = "Authentication Check")]
|
||||
public IActionResult IsAuth() => Ok(User?.Identity?.IsAuthenticated ?? false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using UserManagement.Application.Dtos.Incomming;
|
||||
using UserManagement.Application.Interfaces;
|
||||
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
||||
|
||||
namespace UserManagement.API.Controllers
|
||||
{
|
||||
@ -114,7 +115,18 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> UpdateRole(int id, UpdatingRoleDto updatingRoleDto)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
}
|
||||
|
||||
var updated = await _roleService.UpdateRoleAsync(updatingRoleDto);
|
||||
|
||||
if (!updated)
|
||||
{
|
||||
return BadRequest("Update failed");
|
||||
}
|
||||
|
||||
return Ok(updated);
|
||||
}
|
||||
|
||||
@ -126,7 +138,18 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> DeleteRole([FromRoute] int id)
|
||||
{
|
||||
await _roleService.DeleteRoleAsync(id);
|
||||
if (id <= 0)
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
}
|
||||
|
||||
var deleted = await _roleService.DeleteRoleAsync(id);
|
||||
|
||||
if (!deleted)
|
||||
{
|
||||
return BadRequest("Deletion failed");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using UserManagement.Application.Dtos.Incomming;
|
||||
using UserManagement.Application.Interfaces;
|
||||
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
||||
|
||||
namespace UserManagement.API.Controllers
|
||||
{
|
||||
@ -24,6 +25,7 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[Authorize(Roles = "Admin")]
|
||||
public async Task<IActionResult> CreateUser([FromBody] CreatingUserDto creatingUserDto)
|
||||
{
|
||||
// Validate incomming model
|
||||
@ -61,9 +63,9 @@ namespace UserManagement.API.Controllers
|
||||
[HttpGet]
|
||||
[SwaggerOperation(Summary = "Get all Users")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetAllUsers()
|
||||
public async Task<IActionResult> GetAllUsers([FromQuery] bool includeRoles = true)
|
||||
{
|
||||
var users = await _userService.GetUsersAsync();
|
||||
var users = await _userService.GetUsersAsync(includeRoles);
|
||||
return Ok(users);
|
||||
}
|
||||
|
||||
@ -73,13 +75,13 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUserById(int id)
|
||||
public async Task<IActionResult> GetUserById(int id, [FromQuery] bool includeRoles = true)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
}
|
||||
var user = await _userService.GetByIdAsync(id);
|
||||
var user = await _userService.GetByIdAsync(id, includeRoles);
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound();
|
||||
@ -93,13 +95,13 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUserByUsername(string username)
|
||||
public async Task<IActionResult> GetUserByUsername(string username, [FromQuery] bool includeRoles = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
return BadRequest("Username connot be empty");
|
||||
}
|
||||
var user = await _userService.GetByUsernameAsync(username);
|
||||
var user = await _userService.GetByUsernameAsync(username, includeRoles);
|
||||
if (user == null)
|
||||
{
|
||||
return NotFound();
|
||||
@ -107,6 +109,22 @@ namespace UserManagement.API.Controllers
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
[HttpGet("role/{role}", Name = "GetUsersByRole")]
|
||||
[SwaggerOperation(Summary = "Get Users by Role")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUsersByRole(string role)
|
||||
{
|
||||
if (string.IsNullOrEmpty(role))
|
||||
{
|
||||
return BadRequest("Role cannot be empty");
|
||||
}
|
||||
var users = await _userService.GetByRoleAsync(role);
|
||||
return Ok(users);
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
[HttpPut("id/{id}", Name = "UpdateUser")]
|
||||
[SwaggerOperation(Summary = "Update User")]
|
||||
@ -114,7 +132,18 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> UpdateUser(int id, UpdatingUserDto updatingUserDto)
|
||||
{
|
||||
if (id <= 0)
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
}
|
||||
|
||||
var updated = await _userService.UpdateUserAsync(updatingUserDto);
|
||||
|
||||
if (!updated)
|
||||
{
|
||||
return BadRequest("Update failed");
|
||||
}
|
||||
|
||||
return Ok(updated);
|
||||
}
|
||||
|
||||
@ -126,7 +155,18 @@ namespace UserManagement.API.Controllers
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> DeleteUser([FromBody] int id)
|
||||
{
|
||||
await _userService.DeleteUserAsync(id);
|
||||
if (id <= 0)
|
||||
{
|
||||
return BadRequest("Invalid Id");
|
||||
}
|
||||
|
||||
var deleted = await _userService.DeleteUserAsync(id);
|
||||
|
||||
if (!deleted)
|
||||
{
|
||||
return BadRequest("Deletion failed");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,8 +12,8 @@ using UserManagement.Infrastructure;
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20240821105311_Initial")]
|
||||
partial class Initial
|
||||
[Migration("20240910064554_Init")]
|
||||
partial class Init
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@ -72,13 +72,6 @@ namespace UserManagement.API.Migrations
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD");
|
||||
|
||||
b.Property<int?>("ROLE")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ROLE_ID");
|
||||
|
||||
b.Property<string>("UserName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
@ -86,20 +79,53 @@ namespace UserManagement.API.Migrations
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRoles", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("USER_ID");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ROLE_ID");
|
||||
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRoles", b =>
|
||||
{
|
||||
b.HasOne("UserManagement.Domain.Entities.Role", "Role")
|
||||
.WithMany()
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("UserManagement.Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Initial : Migration
|
||||
public partial class Init : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
@ -34,24 +34,40 @@ namespace UserManagement.API.Migrations
|
||||
USER_NAME = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
FIRST_NAME = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
LAST_NAME = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
PASSWORD = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
ROLE_ID = table.Column<int>(type: "int", nullable: false),
|
||||
ROLE = table.Column<int>(type: "int", nullable: true)
|
||||
PASSWORD = table.Column<string>(type: "nvarchar(max)", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.ID);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "UserRoles",
|
||||
columns: table => new
|
||||
{
|
||||
USER_ID = table.Column<int>(type: "int", nullable: false),
|
||||
ROLE_ID = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_UserRoles", x => new { x.USER_ID, x.ROLE_ID });
|
||||
table.ForeignKey(
|
||||
name: "FK_Users_Roles_ROLE_ID",
|
||||
name: "FK_UserRoles_Roles_ROLE_ID",
|
||||
column: x => x.ROLE_ID,
|
||||
principalTable: "Roles",
|
||||
principalColumn: "ID",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_UserRoles_Users_USER_ID",
|
||||
column: x => x.USER_ID,
|
||||
principalTable: "Users",
|
||||
principalColumn: "ID",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Users_ROLE_ID",
|
||||
table: "Users",
|
||||
name: "IX_UserRoles_ROLE_ID",
|
||||
table: "UserRoles",
|
||||
column: "ROLE_ID");
|
||||
}
|
||||
|
||||
@ -59,10 +75,13 @@ namespace UserManagement.API.Migrations
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users");
|
||||
name: "UserRoles");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Roles");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users");
|
||||
}
|
||||
}
|
||||
}
|
||||
133
UserManagement.API/Migrations/20240910112722_Second.Designer.cs
generated
Normal file
133
UserManagement.API/Migrations/20240910112722_Second.Designer.cs
generated
Normal file
@ -0,0 +1,133 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using UserManagement.Infrastructure;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20240910112722_Second")]
|
||||
partial class Second
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<DateTime>("CreationDate")
|
||||
.HasColumnType("datetime")
|
||||
.HasColumnName("CREATION_DATE");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("ROLE");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Roles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("FIRST_NAME");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("LAST_NAME");
|
||||
|
||||
b.Property<string>("Password")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD");
|
||||
|
||||
b.Property<string>("UserName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("USER_NAME");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("USER_ID");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ROLE_ID");
|
||||
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.HasOne("UserManagement.Domain.Entities.Role", "Role")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("UserManagement.Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
22
UserManagement.API/Migrations/20240910112722_Second.cs
Normal file
22
UserManagement.API/Migrations/20240910112722_Second.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Second : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
139
UserManagement.API/Migrations/20240911113732_UniqueProperties.Designer.cs
generated
Normal file
139
UserManagement.API/Migrations/20240911113732_UniqueProperties.Designer.cs
generated
Normal file
@ -0,0 +1,139 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using UserManagement.Infrastructure;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20240911113732_UniqueProperties")]
|
||||
partial class UniqueProperties
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<DateTime>("CreationDate")
|
||||
.HasColumnType("datetime")
|
||||
.HasColumnName("CREATION_DATE");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("ROLE");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Roles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("FIRST_NAME");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("LAST_NAME");
|
||||
|
||||
b.Property<string>("Password")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD");
|
||||
|
||||
b.Property<string>("UserName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("USER_NAME");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserName")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("USER_ID");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ROLE_ID");
|
||||
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.HasOne("UserManagement.Domain.Entities.Role", "Role")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("UserManagement.Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class UniqueProperties : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AlterColumn<string>(
|
||||
name: "USER_NAME",
|
||||
table: "Users",
|
||||
type: "nvarchar(450)",
|
||||
nullable: false,
|
||||
oldClrType: typeof(string),
|
||||
oldType: "nvarchar(max)");
|
||||
|
||||
migrationBuilder.AlterColumn<string>(
|
||||
name: "ROLE",
|
||||
table: "Roles",
|
||||
type: "nvarchar(450)",
|
||||
nullable: false,
|
||||
oldClrType: typeof(string),
|
||||
oldType: "nvarchar(max)");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Users_USER_NAME",
|
||||
table: "Users",
|
||||
column: "USER_NAME",
|
||||
unique: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Roles_ROLE",
|
||||
table: "Roles",
|
||||
column: "ROLE",
|
||||
unique: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_Users_USER_NAME",
|
||||
table: "Users");
|
||||
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_Roles_ROLE",
|
||||
table: "Roles");
|
||||
|
||||
migrationBuilder.AlterColumn<string>(
|
||||
name: "USER_NAME",
|
||||
table: "Users",
|
||||
type: "nvarchar(max)",
|
||||
nullable: false,
|
||||
oldClrType: typeof(string),
|
||||
oldType: "nvarchar(450)");
|
||||
|
||||
migrationBuilder.AlterColumn<string>(
|
||||
name: "ROLE",
|
||||
table: "Roles",
|
||||
type: "nvarchar(max)",
|
||||
nullable: false,
|
||||
oldClrType: typeof(string),
|
||||
oldType: "nvarchar(450)");
|
||||
}
|
||||
}
|
||||
}
|
||||
144
UserManagement.API/Migrations/20240912141329_PasswordHash.Designer.cs
generated
Normal file
144
UserManagement.API/Migrations/20240912141329_PasswordHash.Designer.cs
generated
Normal file
@ -0,0 +1,144 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using UserManagement.Infrastructure;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20240912141329_PasswordHash")]
|
||||
partial class PasswordHash
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<DateTime>("CreationDate")
|
||||
.HasColumnType("datetime")
|
||||
.HasColumnName("CREATION_DATE");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("ROLE");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Roles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("FIRST_NAME");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("LAST_NAME");
|
||||
|
||||
b.Property<string>("Password")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD_HASH");
|
||||
|
||||
b.Property<string>("UserName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("USER_NAME");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserName")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("USER_ID");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ROLE_ID");
|
||||
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.HasOne("UserManagement.Domain.Entities.Role", "Role")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("UserManagement.Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
29
UserManagement.API/Migrations/20240912141329_PasswordHash.cs
Normal file
29
UserManagement.API/Migrations/20240912141329_PasswordHash.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class PasswordHash : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "PASSWORD_HASH",
|
||||
table: "Users",
|
||||
type: "nvarchar(max)",
|
||||
nullable: false,
|
||||
defaultValue: "");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "PASSWORD_HASH",
|
||||
table: "Users");
|
||||
}
|
||||
}
|
||||
}
|
||||
144
UserManagement.API/Migrations/20240912143537_RemoveObsoleteFromPassword.Designer.cs
generated
Normal file
144
UserManagement.API/Migrations/20240912143537_RemoveObsoleteFromPassword.Designer.cs
generated
Normal file
@ -0,0 +1,144 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using UserManagement.Infrastructure;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20240912143537_RemoveObsoleteFromPassword")]
|
||||
partial class RemoveObsoleteFromPassword
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<DateTime>("CreationDate")
|
||||
.HasColumnType("datetime")
|
||||
.HasColumnName("CREATION_DATE");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("ROLE");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Roles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("FIRST_NAME");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("LAST_NAME");
|
||||
|
||||
b.Property<string>("Password")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD_HASH");
|
||||
|
||||
b.Property<string>("UserName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("USER_NAME");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserName")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("USER_ID");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ROLE_ID");
|
||||
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.HasOne("UserManagement.Domain.Entities.Role", "Role")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("UserManagement.Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class RemoveObsoleteFromPassword : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
139
UserManagement.API/Migrations/20240913081538_RemovePasswordFromUser.Designer.cs
generated
Normal file
139
UserManagement.API/Migrations/20240913081538_RemovePasswordFromUser.Designer.cs
generated
Normal file
@ -0,0 +1,139 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using UserManagement.Infrastructure;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20240913081538_RemovePasswordFromUser")]
|
||||
partial class RemovePasswordFromUser
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<DateTime>("CreationDate")
|
||||
.HasColumnType("datetime")
|
||||
.HasColumnName("CREATION_DATE");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("ROLE");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Roles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ID");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("FIRST_NAME");
|
||||
|
||||
b.Property<string>("LastName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("LAST_NAME");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD_HASH");
|
||||
|
||||
b.Property<string>("UserName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("USER_NAME");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserName")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("USER_ID");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ROLE_ID");
|
||||
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.HasOne("UserManagement.Domain.Entities.Role", "Role")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("UserManagement.Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace UserManagement.API.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class RemovePasswordFromUser : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "PASSWORD",
|
||||
table: "Users");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "PASSWORD",
|
||||
table: "Users",
|
||||
type: "nvarchar(max)",
|
||||
nullable: false,
|
||||
defaultValue: "");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -37,11 +37,14 @@ namespace UserManagement.API.Migrations
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("ROLE");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Name")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Roles");
|
||||
});
|
||||
|
||||
@ -64,39 +67,68 @@ namespace UserManagement.API.Migrations
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("LAST_NAME");
|
||||
|
||||
b.Property<string>("Password")
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("PASSWORD");
|
||||
.HasColumnName("PASSWORD_HASH");
|
||||
|
||||
b.Property<int?>("ROLE")
|
||||
.HasColumnType("int");
|
||||
b.Property<string>("UserName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(450)")
|
||||
.HasColumnName("USER_NAME");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserName")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.Property<int>("UserId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("USER_ID");
|
||||
|
||||
b.Property<int>("RoleId")
|
||||
.HasColumnType("int")
|
||||
.HasColumnName("ROLE_ID");
|
||||
|
||||
b.Property<string>("UserName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)")
|
||||
.HasColumnName("USER_NAME");
|
||||
|
||||
b.HasKey("Id");
|
||||
b.HasKey("UserId", "RoleId");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("Users");
|
||||
b.ToTable("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.UserRole", b =>
|
||||
{
|
||||
b.HasOne("UserManagement.Domain.Entities.Role", "Role")
|
||||
.WithMany()
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("RoleId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("UserManagement.Domain.Entities.User", "User")
|
||||
.WithMany("UserRoles")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Role");
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.Role", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("UserManagement.Domain.Entities.User", b =>
|
||||
{
|
||||
b.Navigation("UserRoles");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=UserManagement;Trusted_Connection=True;TrustServerCertificate=True;"
|
||||
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ProjectUserManagement;Trusted_Connection=True;TrustServerCertificate=True;"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
namespace UserManagement.Application.Dtos.Incomming
|
||||
using UserManagement.Application.Dtos.Outgoing;
|
||||
|
||||
namespace UserManagement.Application.Dtos.Incomming
|
||||
{
|
||||
public class CreatingUserDto
|
||||
{
|
||||
@ -8,8 +10,8 @@
|
||||
|
||||
public string LastName { get; set; }
|
||||
|
||||
public string Password { get; init; }
|
||||
public string Password { get; set; }
|
||||
|
||||
public int RoleId { get; set; }
|
||||
public ICollection<int> RoleIds { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,8 +10,8 @@
|
||||
|
||||
public string LastName { get; set; }
|
||||
|
||||
public string Password { get; init; }
|
||||
public string Password { get; set; }
|
||||
|
||||
public int RoleId { get; set; }
|
||||
public ICollection<int> RoleIds { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,6 @@
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public required string Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,6 @@
|
||||
|
||||
public string LastName { get; set; }
|
||||
|
||||
public ReadingRoleDto? Role { get; set; }
|
||||
public ICollection<ReadingRoleDto> UserRoles { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
9
UserManagement.App/Dtos/Outgoing/ReadingUserRolesDto.cs
Normal file
9
UserManagement.App/Dtos/Outgoing/ReadingUserRolesDto.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace UserManagement.Application.Dtos.Outgoing
|
||||
{
|
||||
public class ReadingUserRolesDto
|
||||
{
|
||||
public int RoleId { get; set; }
|
||||
|
||||
public string RoleName { get; set; }
|
||||
}
|
||||
}
|
||||
@ -10,20 +10,20 @@ namespace UserManagement.Application.Interfaces
|
||||
Task<User?> AddUserAsync(CreatingUserDto creatingUserDto);
|
||||
|
||||
// READ ALL
|
||||
Task<IEnumerable<ReadingUserDto>> GetUsersAsync();
|
||||
Task<IEnumerable<ReadingUserDto>> GetUsersAsync(bool includeRoles = true);
|
||||
|
||||
// READ BY ID
|
||||
Task<ReadingUserDto> GetByIdAsync(int id);
|
||||
Task<ReadingUserDto> GetByIdAsync(int id, bool includeRoles = true);
|
||||
|
||||
// READ BY USERNAME
|
||||
Task<ReadingUserDto> GetByUsernameAsync(string username);
|
||||
Task<ReadingUserDto> GetByUsernameAsync(string username, bool includeRoles = true);
|
||||
|
||||
// READ BY ROLE
|
||||
Task<IEnumerable<ReadingUserDto>> GetByRoleAsync(string role);
|
||||
|
||||
// UPDATE
|
||||
Task<bool> UpdateUserAsync(UpdatingUserDto updatingUserDto);
|
||||
|
||||
// UPDATE USER ROLE
|
||||
Task UpdateUserRoleAsync(int userId, int roleId);
|
||||
|
||||
// DELETE
|
||||
Task<bool> DeleteUserAsync(int id);
|
||||
}
|
||||
|
||||
@ -11,13 +11,47 @@ namespace UserManagement.Application.MappingProfiles
|
||||
{
|
||||
// ROLE
|
||||
CreateMap<Role, CreatingRoleDto>().ReverseMap();
|
||||
|
||||
CreateMap<Role, ReadingRoleDto>().ReverseMap();
|
||||
|
||||
CreateMap<Role, UpdatingRoleDto>().ReverseMap();
|
||||
|
||||
// USER
|
||||
CreateMap<User, CreatingUserDto>().ReverseMap();
|
||||
CreateMap<User, ReadingUserDto>().ReverseMap();
|
||||
CreateMap<User, UpdatingUserDto>().ReverseMap();
|
||||
CreateMap<User, CreatingUserDto>()
|
||||
.ForMember(dest => dest.RoleIds, opt => opt.Ignore())
|
||||
.ReverseMap()
|
||||
.AfterMap((src, dest) =>
|
||||
{
|
||||
dest.UserRoles = src.RoleIds?.Select(roleId => new UserRole
|
||||
{
|
||||
RoleId = roleId,
|
||||
User = dest
|
||||
}).ToList();
|
||||
|
||||
dest.PasswordHash = BCrypt.Net.BCrypt.HashPassword(src.Password);
|
||||
});
|
||||
|
||||
CreateMap<User, ReadingUserDto>()
|
||||
.ForMember(dest => dest.UserRoles, opt => opt.MapFrom(src =>
|
||||
src.UserRoles!.Select(ur => new ReadingRoleDto
|
||||
{
|
||||
Id = ur.Role!.Id,
|
||||
Name = ur.Role.Name
|
||||
}).ToList()));
|
||||
|
||||
CreateMap<User, UpdatingUserDto>()
|
||||
.ForMember(dest => dest.RoleIds, opt => opt.Ignore())
|
||||
.ReverseMap()
|
||||
.AfterMap((src, dest) =>
|
||||
{
|
||||
dest.UserRoles = src.RoleIds?.Select(roleId => new UserRole
|
||||
{
|
||||
RoleId = roleId,
|
||||
UserId = dest.Id
|
||||
}).ToList();
|
||||
|
||||
dest.PasswordHash = BCrypt.Net.BCrypt.HashPassword(src.Password);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,9 +15,9 @@ namespace UserManagement.Application.Services
|
||||
// AUTHENTICATE
|
||||
public async Task<bool> ValidateAsync(string username, string password)
|
||||
{
|
||||
var user = await _userRepository.GetByUsernameAsync(username);
|
||||
var user = await _userRepository.GetByUsernameAsync(username, includeRoles: true);
|
||||
|
||||
return user?.Password == password;
|
||||
return BCrypt.Net.BCrypt.Verify(password, user!.PasswordHash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,70 +23,100 @@ namespace UserManagement.Application.Services
|
||||
// CREATE
|
||||
public async Task<User?> AddUserAsync(CreatingUserDto creatingUserDto)
|
||||
{
|
||||
// validating role
|
||||
var role = await _roleRepository.GetByIdAsync(creatingUserDto.RoleId);
|
||||
if (role == null)
|
||||
var user = _mapper.Map<User>(creatingUserDto);
|
||||
|
||||
if (!string.IsNullOrEmpty(creatingUserDto.Password))
|
||||
{
|
||||
throw new ArgumentException("Role not found");
|
||||
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(creatingUserDto.Password);
|
||||
}
|
||||
|
||||
user.UserRoles = new List<UserRole>();
|
||||
|
||||
foreach (var roleId in creatingUserDto.RoleIds)
|
||||
{
|
||||
var role = await _roleRepository.GetByIdAsync(roleId);
|
||||
if (role is not null)
|
||||
{
|
||||
user.UserRoles.Add(new UserRole { UserId = user.Id, RoleId = role.Id });
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Role with Id {roleId} not found");
|
||||
}
|
||||
}
|
||||
|
||||
// mapping dto to entity
|
||||
var user = _mapper.Map<User>(creatingUserDto);
|
||||
var created = await _userRepository.AddAsync(user);
|
||||
return created;
|
||||
}
|
||||
|
||||
// READ ALL
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetUsersAsync()
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetUsersAsync(bool includeRoles = true)
|
||||
{
|
||||
var users = await _userRepository.GetAllAsync();
|
||||
var users = await _userRepository.GetAllAsync(includeRoles);
|
||||
var readDto = _mapper.Map<IEnumerable<ReadingUserDto>>(users);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
public async Task<ReadingUserDto> GetByIdAsync(int id)
|
||||
public async Task<ReadingUserDto> GetByIdAsync(int id, bool includeRoles = true)
|
||||
{
|
||||
var user = await _userRepository.GetByIdAsync(id);
|
||||
var user = await _userRepository.GetByIdAsync(id, includeRoles);
|
||||
var readDto = _mapper.Map<ReadingUserDto>(user);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// READ BY USERNAME
|
||||
public async Task<ReadingUserDto> GetByUsernameAsync(string username)
|
||||
public async Task<ReadingUserDto> GetByUsernameAsync(string username, bool includeRoles = true)
|
||||
{
|
||||
var user = await _userRepository.GetByUsernameAsync(username);
|
||||
var user = await _userRepository.GetByUsernameAsync(username, includeRoles);
|
||||
var readDto = _mapper.Map<ReadingUserDto>(user);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
public async Task<IEnumerable<ReadingUserDto>> GetByRoleAsync(string role)
|
||||
{
|
||||
var users = await _userRepository.GetByRoleAsync(role);
|
||||
var readDto = _mapper.Map<IEnumerable<ReadingUserDto>>(users);
|
||||
return readDto;
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
public async Task<bool> UpdateUserAsync(UpdatingUserDto updatingUserDto)
|
||||
{
|
||||
var user = _mapper.Map<User>(updatingUserDto);
|
||||
var user = await _userRepository.GetByIdAsync(updatingUserDto.Id);
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_mapper.Map(updatingUserDto, user);
|
||||
|
||||
if (!string.IsNullOrEmpty(updatingUserDto.Password))
|
||||
{
|
||||
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(updatingUserDto.Password);
|
||||
}
|
||||
|
||||
user.UserRoles!.Clear();
|
||||
|
||||
foreach(var roleId in updatingUserDto.RoleIds)
|
||||
{
|
||||
var role = await _roleRepository.GetByIdAsync(roleId);
|
||||
if (role is not null)
|
||||
{
|
||||
user.UserRoles.Add(new UserRole { UserId = user.Id, RoleId = role.Id });
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentException($"Role with Id {roleId} not found");
|
||||
}
|
||||
}
|
||||
|
||||
bool isUpdated = await _userRepository.UpdateAsync(user);
|
||||
return isUpdated;
|
||||
}
|
||||
|
||||
// UPDATE USER ROLE -- die Rolle eines Users aktualisieren
|
||||
public async Task UpdateUserRoleAsync(int userId, int roleId)
|
||||
{
|
||||
var user = await _userRepository.GetByIdAsync(userId);
|
||||
if (user == null)
|
||||
{
|
||||
throw new ArgumentException("User not found");
|
||||
}
|
||||
|
||||
var role = await _roleRepository.GetByIdAsync(roleId);
|
||||
if (role == null)
|
||||
{
|
||||
throw new ArgumentException("Role not found");
|
||||
}
|
||||
|
||||
user.RoleId = roleId;
|
||||
await _userRepository.SaveAsync();
|
||||
}
|
||||
|
||||
// DELETE
|
||||
public async Task<bool> DeleteUserAsync(int id)
|
||||
{
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace UserManagement.Domain.Entities
|
||||
@ -12,7 +13,10 @@ namespace UserManagement.Domain.Entities
|
||||
|
||||
[Required]
|
||||
[Column("ROLE")]
|
||||
public string Name { get; set; }
|
||||
public required string Name { get; set; }
|
||||
|
||||
[Column("USERS")]
|
||||
public ICollection<UserRole>? UserRoles { get; set; } = new Collection<UserRole>();
|
||||
|
||||
[Required]
|
||||
[Column("CREATION_DATE", TypeName = "datetime")]
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace UserManagement.Domain.Entities
|
||||
@ -23,13 +24,9 @@ namespace UserManagement.Domain.Entities
|
||||
public string LastName { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column("PASSWORD")]
|
||||
public string Password { get; init; }
|
||||
[Column("PASSWORD_HASH")]
|
||||
public required string PasswordHash { get; set; }
|
||||
|
||||
[Column("ROLE_ID")]
|
||||
public int RoleId { get; set; }
|
||||
|
||||
[ForeignKey("ROLE")]
|
||||
public Role? Role { get; set; }
|
||||
public ICollection<UserRole>? UserRoles { get; set; } = new Collection<UserRole>();
|
||||
}
|
||||
}
|
||||
19
UserManagement.Domain/Entities/UserRole.cs
Normal file
19
UserManagement.Domain/Entities/UserRole.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace UserManagement.Domain.Entities
|
||||
{
|
||||
public class UserRole
|
||||
{
|
||||
[Column("USER_ID")]
|
||||
public int UserId { get; set; }
|
||||
|
||||
[ForeignKey("UserId")]
|
||||
public User? User { get; set; }
|
||||
|
||||
[Column("ROLE_ID")]
|
||||
public int RoleId { get; set; }
|
||||
|
||||
[ForeignKey("RoleId")]
|
||||
public Role? Role { get; set; }
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
|
||||
|
||||
@ -12,15 +12,32 @@ namespace UserManagement.Infrastructure
|
||||
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<Role> Roles { get; set; }
|
||||
public DbSet<UserRole> UserRoles { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.HasOne(u => u.Role)
|
||||
.WithMany()
|
||||
.HasForeignKey(u => u.RoleId);
|
||||
.HasIndex(u => u.UserName)
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<Role>()
|
||||
.HasIndex(r => r.Name)
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<UserRole>()
|
||||
.HasKey(ur => new { ur.UserId, ur.RoleId });
|
||||
|
||||
modelBuilder.Entity<UserRole>()
|
||||
.HasOne(ur => ur.User)
|
||||
.WithMany(u => u.UserRoles)
|
||||
.HasForeignKey(ur => ur.UserId);
|
||||
|
||||
modelBuilder.Entity<UserRole>()
|
||||
.HasOne(ur => ur.Role)
|
||||
.WithMany(r => r.UserRoles)
|
||||
.HasForeignKey(ur => ur.RoleId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,21 +8,21 @@ namespace UserManagement.Infrastructure.Interfaces
|
||||
Task<User?> AddAsync(User user);
|
||||
|
||||
// READ ALL
|
||||
Task<IEnumerable<User>> GetAllAsync();
|
||||
Task<IEnumerable<User>> GetAllAsync(bool includeRoles = true);
|
||||
|
||||
// READ BY ID
|
||||
Task<User?> GetByIdAsync(int id);
|
||||
Task<User?> GetByIdAsync(int id, bool includeRoles = true);
|
||||
|
||||
// READ BY USERNAME
|
||||
Task<User?> GetByUsernameAsync(string username);
|
||||
Task<User?> GetByUsernameAsync(string username, bool includeRoles = true);
|
||||
|
||||
// READ BY ROLE
|
||||
Task<IEnumerable<User>> GetByRoleAsync(string role);
|
||||
|
||||
// UPDATE
|
||||
Task<bool> UpdateAsync(User user);
|
||||
|
||||
// DELETE
|
||||
Task<bool> DeleteAsync(User user);
|
||||
|
||||
// SAVE
|
||||
Task<bool> SaveAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,21 +22,52 @@ namespace UserManagement.Infrastructure.Repositories
|
||||
}
|
||||
|
||||
// READ ALL
|
||||
public async Task<IEnumerable<User>> GetAllAsync()
|
||||
public async Task<IEnumerable<User>> GetAllAsync(bool includeRoles = true)
|
||||
{
|
||||
return await _context.Users.Include(u => u.Role).ToListAsync();
|
||||
var query = _context.Users.AsNoTracking();
|
||||
|
||||
if (includeRoles)
|
||||
query = query.Include(user => user.UserRoles)!.ThenInclude(ur => ur.Role);
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
// READ BY ID
|
||||
public async Task<User?> GetByIdAsync(int id)
|
||||
public async Task<User?> GetByIdAsync(int id, bool includeRoles = true)
|
||||
{
|
||||
return await _context.Users.Where(user => user.Id == id).Include(user => user.Role).FirstAsync();
|
||||
var query = _context.Users.AsNoTracking();
|
||||
|
||||
if (id > 0)
|
||||
query = query.Where(u => u.Id == id);
|
||||
|
||||
if (includeRoles)
|
||||
query = query.Include(user => user.UserRoles)!.ThenInclude(ur => ur.Role);
|
||||
|
||||
return await query.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
// READ BY USERNAME
|
||||
public async Task<User?> GetByUsernameAsync(string username)
|
||||
public async Task<User?> GetByUsernameAsync(string username, bool includeRoles = true)
|
||||
{
|
||||
return await _context.Users.Include(user => user.Role).FirstOrDefaultAsync(u => u.UserName == username);
|
||||
var query = _context.Users.AsNoTracking();
|
||||
|
||||
if (!string.IsNullOrEmpty(username))
|
||||
query = query.Where(u => u.UserName == username);
|
||||
|
||||
if (includeRoles)
|
||||
query = query.Include(user => user.UserRoles)!.ThenInclude(ur => ur.Role);
|
||||
|
||||
return await query.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
// READ BY ROLE
|
||||
public async Task<IEnumerable<User>> GetByRoleAsync(string role)
|
||||
{
|
||||
return await _context.Users
|
||||
.Include(u => u.UserRoles)!
|
||||
.ThenInclude(ur => ur.Role)
|
||||
.Where(ur => ur.UserRoles!.Any(r => r.Role!.Name == role))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
// UPDATE
|
||||
@ -54,12 +85,5 @@ namespace UserManagement.Infrastructure.Repositories
|
||||
var result = await _context.SaveChangesAsync();
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
// SAVE
|
||||
public async Task<bool> SaveAsync()
|
||||
{
|
||||
var saved = await _context.SaveChangesAsync();
|
||||
return saved > 0 ? true : false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user