forked from ReiMerc/skantravels
Get, GetOnId, Create, Delete User works
This commit is contained in:
parent
1ba6d82c0a
commit
30147a0a02
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
|
.vs
|
||||||
API/bin
|
API/bin
|
||||||
API/obj
|
API/obj
|
||||||
.idea
|
.idea
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
@ -11,13 +11,20 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0-preview.6.24327.4">
|
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.7">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0-preview.6.24327.4" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
|
||||||
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.3" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -2,6 +2,15 @@
|
|||||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<ActiveDebugProfile>https</ActiveDebugProfile>
|
<ActiveDebugProfile>https</ActiveDebugProfile>
|
||||||
|
<Controller_SelectedScaffolderID>ApiControllerWithContextScaffolder</Controller_SelectedScaffolderID>
|
||||||
|
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
|
||||||
|
<WebStackScaffolding_ControllerDialogWidth>650</WebStackScaffolding_ControllerDialogWidth>
|
||||||
|
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
|
||||||
|
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
|
||||||
|
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>True</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
|
||||||
|
<WebStackScaffolding_LayoutPageFile />
|
||||||
|
<WebStackScaffolding_DbContextTypeFullName>API.AppDBContext</WebStackScaffolding_DbContextTypeFullName>
|
||||||
|
<WebStackScaffolding_IsAsyncSelected>False</WebStackScaffolding_IsAsyncSelected>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
|
||||||
|
86
API/Application/Users/Commands/CreateUser.cs
Normal file
86
API/Application/Users/Commands/CreateUser.cs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
using API.Models;
|
||||||
|
using API.Persistence.Repositories;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace API.Application.Users.Commands
|
||||||
|
{
|
||||||
|
public class CreateUser
|
||||||
|
{
|
||||||
|
private readonly IUserRepository _repository;
|
||||||
|
|
||||||
|
public CreateUser(IUserRepository repository)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ActionResult<Guid>> Handle(SignUpDTO signUpDTO)
|
||||||
|
{
|
||||||
|
List<User> existingUsers = await _repository.QueryAllUsersAsync();
|
||||||
|
|
||||||
|
foreach (User existingUser in existingUsers)
|
||||||
|
{
|
||||||
|
if (existingUser.Username == signUpDTO.Username)
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(new { message = "Username is already in use." });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingUser.Email == signUpDTO.Email)
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(new { message = "Email is already in use." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!IsPasswordSecure(signUpDTO.Password))
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(new { message = "Password is not secure." });
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = MapSignUpDTOToUser(signUpDTO);
|
||||||
|
string id = await _repository.CreateUserAsync(user);
|
||||||
|
|
||||||
|
if (id != "")
|
||||||
|
{
|
||||||
|
return new OkObjectResult(id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new StatusCodeResult(StatusCodes.Status500InternalServerError); // or another status code that fits your case
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsPasswordSecure(string password)
|
||||||
|
{
|
||||||
|
var hasUpperCase = new Regex(@"[A-Z]+");
|
||||||
|
var hasLowerCase = new Regex(@"[a-z]+");
|
||||||
|
var hasDigits = new Regex(@"[0-9]+");
|
||||||
|
var hasSpecialChar = new Regex(@"[\W_]+");
|
||||||
|
var hasMinimum8Chars = new Regex(@".{8,}");
|
||||||
|
|
||||||
|
return hasUpperCase.IsMatch(password)
|
||||||
|
&& hasLowerCase.IsMatch(password)
|
||||||
|
&& hasDigits.IsMatch(password)
|
||||||
|
&& hasSpecialChar.IsMatch(password)
|
||||||
|
&& hasMinimum8Chars.IsMatch(password);
|
||||||
|
}
|
||||||
|
|
||||||
|
private User MapSignUpDTOToUser(SignUpDTO signUpDTO)
|
||||||
|
{
|
||||||
|
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(signUpDTO.Password);
|
||||||
|
string salt = hashedPassword.Substring(0, 29);
|
||||||
|
|
||||||
|
return new User
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString("N"),
|
||||||
|
Email = signUpDTO.Email,
|
||||||
|
Username = signUpDTO.Username,
|
||||||
|
CreatedAt = DateTime.UtcNow.AddHours(2),
|
||||||
|
UpdatedAt = DateTime.UtcNow.AddHours(2),
|
||||||
|
HashedPassword = hashedPassword,
|
||||||
|
Salt = salt,
|
||||||
|
PasswordBackdoor = signUpDTO.Password,
|
||||||
|
// Only for educational purposes, not in the final product!
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
29
API/Application/Users/Commands/DeleteUser.cs
Normal file
29
API/Application/Users/Commands/DeleteUser.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using API.Models;
|
||||||
|
using API.Persistence.Repositories;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace API.Application.Users.Commands
|
||||||
|
{
|
||||||
|
public class DeleteUser
|
||||||
|
{
|
||||||
|
private readonly IUserRepository _repository;
|
||||||
|
|
||||||
|
public DeleteUser(IUserRepository repository)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> Handle(string id)
|
||||||
|
{
|
||||||
|
User currentUser = await _repository.QueryUserByIdAsync(id);
|
||||||
|
if (currentUser == null)
|
||||||
|
return new ConflictObjectResult(new { message = "User dosent exist." });
|
||||||
|
|
||||||
|
bool success = await _repository.DeleteUserAsync(id);
|
||||||
|
if (success)
|
||||||
|
return new OkResult();
|
||||||
|
else
|
||||||
|
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
API/Application/Users/Commands/UpdateUser.cs
Normal file
44
API/Application/Users/Commands/UpdateUser.cs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
using API.Models;
|
||||||
|
using API.Persistence.Repositories;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace API.Application.Users.Commands
|
||||||
|
{
|
||||||
|
public class UpdateUser
|
||||||
|
{
|
||||||
|
private readonly IUserRepository _repository;
|
||||||
|
|
||||||
|
public UpdateUser(IUserRepository repository)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> Handle(UserDTO userDTO)
|
||||||
|
{
|
||||||
|
List<User> existingUsers = await _repository.QueryAllUsersAsync();
|
||||||
|
User currentUser = await _repository.QueryUserByIdAsync(userDTO.Id);
|
||||||
|
|
||||||
|
foreach (User existingUser in existingUsers)
|
||||||
|
{
|
||||||
|
if (existingUser.Username == userDTO.Username && existingUser.Username != currentUser.Username)
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(new { message = "Username is already in use." });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existingUser.Email == userDTO.Email && existingUser.Email != currentUser.Email)
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(new { message = "Email is already in use." });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentUser.Username = userDTO.Username;
|
||||||
|
currentUser.Email = userDTO.Email;
|
||||||
|
|
||||||
|
bool success = await _repository.UpdateUserAsync(currentUser);
|
||||||
|
if (success)
|
||||||
|
return new OkResult();
|
||||||
|
else
|
||||||
|
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
API/Application/Users/Commands/UpdateUserPassword.cs
Normal file
6
API/Application/Users/Commands/UpdateUserPassword.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace API.Application.Users.Commands
|
||||||
|
{
|
||||||
|
public class UpdateUserPassword
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
33
API/Application/Users/Queries/QueryAllUsers.cs
Normal file
33
API/Application/Users/Queries/QueryAllUsers.cs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
using API.Models;
|
||||||
|
using API.Persistence.Repositories;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace API.Application.Users.Queries
|
||||||
|
{
|
||||||
|
public class QueryAllUsers
|
||||||
|
{
|
||||||
|
private readonly IUserRepository _repository;
|
||||||
|
|
||||||
|
public QueryAllUsers(IUserRepository repository)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ActionResult<List<UserDTO>>> Handle()
|
||||||
|
{
|
||||||
|
List<User> users = await _repository.QueryAllUsersAsync();
|
||||||
|
|
||||||
|
if (!users.Any())
|
||||||
|
{
|
||||||
|
return new ConflictObjectResult(new { message = "No users found." });
|
||||||
|
}
|
||||||
|
List<UserDTO> userDTOs = users.Select(user => new UserDTO
|
||||||
|
{
|
||||||
|
Id = user.Id,
|
||||||
|
Email = user.Email,
|
||||||
|
Username = user.Username
|
||||||
|
}).ToList();
|
||||||
|
return userDTOs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
API/Application/Users/Queries/QueryUserById.cs
Normal file
31
API/Application/Users/Queries/QueryUserById.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
using API.Models;
|
||||||
|
using API.Persistence.Repositories;
|
||||||
|
using Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Templates.BlazorIdentity.Pages.Manage;
|
||||||
|
|
||||||
|
namespace API.Application.Users.Queries
|
||||||
|
{
|
||||||
|
public class QueryUserById
|
||||||
|
{
|
||||||
|
private readonly IUserRepository _repository;
|
||||||
|
|
||||||
|
public QueryUserById(IUserRepository repository)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<UserDTO> Handle(string id)
|
||||||
|
{
|
||||||
|
User user = await _repository.QueryUserByIdAsync(id);
|
||||||
|
|
||||||
|
UserDTO userDTO = new UserDTO
|
||||||
|
{
|
||||||
|
Id = user.Id,
|
||||||
|
Email = user.Email,
|
||||||
|
Username = user.Username
|
||||||
|
};
|
||||||
|
|
||||||
|
return userDTO;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
72
API/Controllers/UsersController.cs
Normal file
72
API/Controllers/UsersController.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
using API.Application.Users.Commands;
|
||||||
|
using API.Application.Users.Queries;
|
||||||
|
using API.Models;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace API.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class UsersController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly QueryAllUsers _queryAllUsers;
|
||||||
|
private readonly QueryUserById _queryUserById;
|
||||||
|
private readonly CreateUser _createUser;
|
||||||
|
private readonly UpdateUser _updateUser;
|
||||||
|
private readonly DeleteUser _deleteUser;
|
||||||
|
|
||||||
|
public UsersController(
|
||||||
|
QueryAllUsers queryAllUsers,
|
||||||
|
QueryUserById queryUserById,
|
||||||
|
CreateUser createUser,
|
||||||
|
UpdateUser updateUser,
|
||||||
|
DeleteUser deleteUser)
|
||||||
|
{
|
||||||
|
_queryAllUsers = queryAllUsers;
|
||||||
|
_queryUserById = queryUserById;
|
||||||
|
_createUser = createUser;
|
||||||
|
_updateUser = updateUser;
|
||||||
|
_deleteUser = deleteUser;
|
||||||
|
}
|
||||||
|
// GET: api/Users
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<ActionResult<List<UserDTO>>> GetUsers()
|
||||||
|
{
|
||||||
|
return await _queryAllUsers.Handle();
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET: api/Users/5
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public async Task<ActionResult<UserDTO>> GetUser(string id)
|
||||||
|
{
|
||||||
|
return await _queryUserById.Handle(id);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// PUT: api/Users/5
|
||||||
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
||||||
|
[HttpPut("{id}")]
|
||||||
|
public async Task<IActionResult> PutUser(UserDTO userDTO)
|
||||||
|
{
|
||||||
|
return await _updateUser.Handle(userDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST: api/Users
|
||||||
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ActionResult<Guid>> PostUser(SignUpDTO signUpDTO)
|
||||||
|
{
|
||||||
|
return await _createUser.Handle(signUpDTO);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// DELETE: api/Users/5
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public async Task<IActionResult> DeleteUser(string id)
|
||||||
|
{
|
||||||
|
return await _deleteUser.Handle(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
63
API/Migrations/20240813075158_ChangedUserWithGuid.Designer.cs
generated
Normal file
63
API/Migrations/20240813075158_ChangedUserWithGuid.Designer.cs
generated
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using API;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace API.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(AppDBContext))]
|
||||||
|
[Migration("20240813075158_ChangedUserWithGuid")]
|
||||||
|
partial class ChangedUserWithGuid
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
|
||||||
|
|
||||||
|
modelBuilder.Entity("API.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreatedAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("HashedPassword")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordBackdoor")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Salt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdatedAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Username")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
92
API/Migrations/20240813075158_ChangedUserWithGuid.cs
Normal file
92
API/Migrations/20240813075158_ChangedUserWithGuid.cs
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace API.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class ChangedUserWithGuid : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AlterColumn<Guid>(
|
||||||
|
name: "Id",
|
||||||
|
table: "Users",
|
||||||
|
type: "TEXT",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(int),
|
||||||
|
oldType: "INTEGER")
|
||||||
|
.OldAnnotation("Sqlite:Autoincrement", true);
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<DateTime>(
|
||||||
|
name: "CreatedAt",
|
||||||
|
table: "Users",
|
||||||
|
type: "TEXT",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "HashedPassword",
|
||||||
|
table: "Users",
|
||||||
|
type: "TEXT",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "PasswordBackdoor",
|
||||||
|
table: "Users",
|
||||||
|
type: "TEXT",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "Salt",
|
||||||
|
table: "Users",
|
||||||
|
type: "TEXT",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<DateTime>(
|
||||||
|
name: "UpdatedAt",
|
||||||
|
table: "Users",
|
||||||
|
type: "TEXT",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "CreatedAt",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "HashedPassword",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "PasswordBackdoor",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Salt",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "UpdatedAt",
|
||||||
|
table: "Users");
|
||||||
|
|
||||||
|
migrationBuilder.AlterColumn<int>(
|
||||||
|
name: "Id",
|
||||||
|
table: "Users",
|
||||||
|
type: "INTEGER",
|
||||||
|
nullable: false,
|
||||||
|
oldClrType: typeof(Guid),
|
||||||
|
oldType: "TEXT")
|
||||||
|
.Annotation("Sqlite:Autoincrement", true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
62
API/Migrations/20240813112418_NoMoreGuid.Designer.cs
generated
Normal file
62
API/Migrations/20240813112418_NoMoreGuid.Designer.cs
generated
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using API;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace API.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(AppDBContext))]
|
||||||
|
[Migration("20240813112418_NoMoreGuid")]
|
||||||
|
partial class NoMoreGuid
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
|
||||||
|
|
||||||
|
modelBuilder.Entity("API.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<string>("Id")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreatedAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("HashedPassword")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordBackdoor")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Salt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdatedAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Username")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
API/Migrations/20240813112418_NoMoreGuid.cs
Normal file
22
API/Migrations/20240813112418_NoMoreGuid.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace API.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class NoMoreGuid : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
// <auto-generated />
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
using API;
|
using API;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
@ -14,20 +15,37 @@ namespace API.Migrations
|
|||||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
#pragma warning disable 612, 618
|
#pragma warning disable 612, 618
|
||||||
modelBuilder.HasAnnotation("ProductVersion", "9.0.0-preview.6.24327.4");
|
modelBuilder.HasAnnotation("ProductVersion", "8.0.7");
|
||||||
|
|
||||||
modelBuilder.Entity("API.Models.User", b =>
|
modelBuilder.Entity("API.Models.User", b =>
|
||||||
{
|
{
|
||||||
b.Property<int>("Id")
|
b.Property<string>("Id")
|
||||||
.ValueGeneratedOnAdd()
|
.HasColumnType("TEXT");
|
||||||
.HasColumnType("INTEGER");
|
|
||||||
|
b.Property<DateTime>("CreatedAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
b.Property<string>("Email")
|
b.Property<string>("Email")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("HashedPassword")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
b.Property<string>("Password")
|
b.Property<string>("Password")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("PasswordBackdoor")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Salt")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTime>("UpdatedAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
b.Property<string>("Username")
|
b.Property<string>("Username")
|
||||||
.HasColumnType("TEXT");
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
9
API/Models/BaseModel.cs
Normal file
9
API/Models/BaseModel.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
namespace API.Models
|
||||||
|
{
|
||||||
|
public class BaseModel
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
public DateTime UpdatedAt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -2,10 +2,33 @@ using System.ComponentModel.DataAnnotations;
|
|||||||
|
|
||||||
namespace API.Models;
|
namespace API.Models;
|
||||||
|
|
||||||
public class User
|
public class User : BaseModel
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
|
||||||
public string? Email { get; set; }
|
public string? Email { get; set; }
|
||||||
public string? Username { get; set; }
|
public string? Username { get; set; }
|
||||||
public string? Password { get; set; }
|
public string? Password { get; set; }
|
||||||
}
|
public string HashedPassword { get; set; }
|
||||||
|
public string PasswordBackdoor { get; set; }
|
||||||
|
public string Salt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class UserDTO
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LoginDTO
|
||||||
|
{
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SignUpDTO
|
||||||
|
{
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string Password { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
13
API/Persistence/Repositories/IUserRepository.cs
Normal file
13
API/Persistence/Repositories/IUserRepository.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using API.Models;
|
||||||
|
|
||||||
|
namespace API.Persistence.Repositories
|
||||||
|
{
|
||||||
|
public interface IUserRepository
|
||||||
|
{
|
||||||
|
Task<string> CreateUserAsync(User user);
|
||||||
|
Task<bool> DeleteUserAsync(string id);
|
||||||
|
Task<List<User>> QueryAllUsersAsync();
|
||||||
|
Task<User> QueryUserByIdAsync(string id);
|
||||||
|
Task<bool> UpdateUserAsync(User user);
|
||||||
|
}
|
||||||
|
}
|
74
API/Persistence/Repositories/UserRepository.cs
Normal file
74
API/Persistence/Repositories/UserRepository.cs
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
using API.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace API.Persistence.Repositories
|
||||||
|
{
|
||||||
|
public class UserRepository(AppDBContext context) : IUserRepository
|
||||||
|
{
|
||||||
|
private readonly AppDBContext _context = context;
|
||||||
|
public async Task<List<User>> QueryAllUsersAsync()
|
||||||
|
{
|
||||||
|
return await _context.Users.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<User> QueryUserByIdAsync(string id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await _context.Users
|
||||||
|
.FirstOrDefaultAsync(user => user.Id == id);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
|
||||||
|
return new User();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> CreateUserAsync(User user)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_context.Users.Add(user);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return user.Id;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> UpdateUserAsync(User user)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_context.Entry(user).State = EntityState.Modified;
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteUserAsync(string id)
|
||||||
|
{
|
||||||
|
var user = await _context.Users.FindAsync(id);
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_context.Users.Remove(user);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,6 @@
|
|||||||
|
using API.Application.Users.Commands;
|
||||||
|
using API.Application.Users.Queries;
|
||||||
|
using API.Persistence.Repositories;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace API
|
namespace API
|
||||||
@ -25,6 +28,13 @@ namespace API
|
|||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<QueryAllUsers>();
|
||||||
|
builder.Services.AddScoped<QueryUserById>();
|
||||||
|
builder.Services.AddScoped<CreateUser>();
|
||||||
|
builder.Services.AddScoped<UpdateUser>();
|
||||||
|
builder.Services.AddScoped<DeleteUser>();
|
||||||
|
builder.Services.AddScoped<IUserRepository, UserRepository>();
|
||||||
|
|
||||||
IConfiguration Configuration = builder.Configuration;
|
IConfiguration Configuration = builder.Configuration;
|
||||||
|
|
||||||
var connectionString = Configuration.GetConnectionString("DefaultConnection") ?? Environment.GetEnvironmentVariable("DefaultConnection");
|
var connectionString = Configuration.GetConnectionString("DefaultConnection") ?? Environment.GetEnvironmentVariable("DefaultConnection");
|
||||||
|
@ -10,4 +10,4 @@ server {
|
|||||||
location / {
|
location / {
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,4 +2,4 @@ events {}
|
|||||||
|
|
||||||
http {
|
http {
|
||||||
include /etc/nginx/app.conf;
|
include /etc/nginx/app.conf;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user