Compare commits
26 Commits
cc2fa7bdf3
...
3ac33fa276
Author | SHA1 | Date | |
---|---|---|---|
3ac33fa276 | |||
53154657c8 | |||
2533ba90c0 | |||
5345098bbd | |||
ba55b692b4 | |||
a2b58598a6 | |||
0517a6cfb0 | |||
054ed4b461 | |||
4f472bce41 | |||
c902050055 | |||
78e87103ef | |||
681118ff91 | |||
57f90eaa82 | |||
9c7ada2260 | |||
23d3ba8885 | |||
72e40c79e6 | |||
be9c47aefa | |||
51d26c58d7 | |||
c3dd4384ff | |||
ea9a89ac74 | |||
d964ec4383 | |||
82ad684fe5 | |||
6dfb7d91ef | |||
78af8e86e9 | |||
5e0fb33ebe | |||
59857d80f3 |
@ -6,8 +6,17 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="BCrypt.Net.BCrypt" Static="True"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="BusinessLogic\" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Contracts\Contracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
96
BusinessLogic/BusinessLogic/RoleLogic.cs
Normal file
96
BusinessLogic/BusinessLogic/RoleLogic.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogicContracts;
|
||||
using Contracts.Converters;
|
||||
using Contracts.Exceptions;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.StorageContracts;
|
||||
using Contracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class RoleLogic : IRoleLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IRoleStorage _roleStorage;
|
||||
|
||||
public RoleLogic(ILogger<RoleLogic> logger, IRoleStorage roleStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_roleStorage = roleStorage;
|
||||
}
|
||||
|
||||
public RoleViewModel Create(RoleBindingModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
var role = _roleStorage.Insert(model);
|
||||
if (role is null)
|
||||
{
|
||||
throw new Exception("Insert operation failed.");
|
||||
}
|
||||
|
||||
return RoleConverter.ToView(role);
|
||||
}
|
||||
|
||||
public RoleViewModel Delete(RoleSearchModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
_logger.LogInformation("Delete role. Id: {0}", model.Id);
|
||||
var role = _roleStorage.Delete(model);
|
||||
if (role is null)
|
||||
{
|
||||
throw new Exception("Delete operation failed.");
|
||||
}
|
||||
|
||||
return RoleConverter.ToView(role);
|
||||
}
|
||||
|
||||
public RoleViewModel ReadElement(RoleSearchModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
_logger.LogInformation("ReadElement. Id: {0}", model.Id);
|
||||
var role = _roleStorage.GetElement(model);
|
||||
if (role is null)
|
||||
{
|
||||
throw new ElementNotFoundException();
|
||||
}
|
||||
_logger.LogInformation("ReadElement find. Id: {0}", role.Id);
|
||||
|
||||
return RoleConverter.ToView(role);
|
||||
}
|
||||
|
||||
public IEnumerable<RoleViewModel> ReadElements(RoleSearchModel? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
||||
var list = _roleStorage.GetList(model);
|
||||
if (list is null || list.Count() == 0)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return [];
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count: {Count}", list.Count());
|
||||
|
||||
return list.Select(RoleConverter.ToView);
|
||||
}
|
||||
|
||||
public RoleViewModel Update(RoleBindingModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
var role = _roleStorage.Update(model);
|
||||
if (role is null)
|
||||
{
|
||||
throw new Exception("Update operation failed.");
|
||||
}
|
||||
return RoleConverter.ToView(role);
|
||||
}
|
||||
}
|
||||
}
|
134
BusinessLogic/BusinessLogic/UserLogic.cs
Normal file
134
BusinessLogic/BusinessLogic/UserLogic.cs
Normal file
@ -0,0 +1,134 @@
|
||||
using BusinessLogic.Tools;
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogicContracts;
|
||||
using Contracts.Converters;
|
||||
using Contracts.Exceptions;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.StorageContracts;
|
||||
using Contracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class UserLogic : IUserLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IUserStorage _userStorage;
|
||||
|
||||
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_userStorage = userStorage;
|
||||
}
|
||||
|
||||
public UserViewModel Create(UserBindingModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
// Проверяем пароль
|
||||
_validatePassword(model.Password);
|
||||
// Хешируем пароль
|
||||
model.PasswordHash = PasswordHasher.Hash(model.Password);
|
||||
var user = _userStorage.Insert(model);
|
||||
if (user is null)
|
||||
{
|
||||
throw new Exception("Insert operation failed.");
|
||||
}
|
||||
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
public UserViewModel Delete(UserSearchModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
_logger.LogInformation("Delete user. Id: {0}", model.Id);
|
||||
var user = _userStorage.Delete(model);
|
||||
if (user is null)
|
||||
{
|
||||
throw new ElementNotFoundException();
|
||||
}
|
||||
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
public IEnumerable<UserViewModel> ReadElements(UserSearchModel? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
||||
var list = _userStorage.GetList(model);
|
||||
if (list is null || list.Count() == 0)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return [];
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count: {Count}", list.Count());
|
||||
|
||||
return list.Select(UserConverter.ToView);
|
||||
}
|
||||
|
||||
public UserViewModel ReadElement(UserSearchModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
_logger.LogInformation("ReadElement. Id: {0}", model.Id);
|
||||
var user = _userStorage.GetElement(model);
|
||||
if (user is null)
|
||||
{
|
||||
throw new ElementNotFoundException();
|
||||
}
|
||||
_logger.LogInformation("ReadElement find. Id: {0}", user.Id);
|
||||
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
public UserViewModel Update(UserBindingModel model)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(model);
|
||||
|
||||
if (model.Password is not null)
|
||||
{
|
||||
_validatePassword(model.Password);
|
||||
model.PasswordHash = PasswordHasher.Hash(model.Password);
|
||||
}
|
||||
var user = _userStorage.Update(model);
|
||||
if (user is null)
|
||||
{
|
||||
throw new Exception("Update operation failed.");
|
||||
}
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
public UserViewModel Login(string email, string password)
|
||||
{
|
||||
if (email is null)
|
||||
{
|
||||
throw new AccountException("Email is null");
|
||||
}
|
||||
var user = _userStorage.GetElement(new() { Email = email });
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
throw new ElementNotFoundException();
|
||||
}
|
||||
// Проверяем пароль
|
||||
_validatePassword(password);
|
||||
if (!PasswordHasher.Verify(password, user.PasswordHash))
|
||||
{
|
||||
throw new AccountException("The passwords don't match.");
|
||||
}
|
||||
return UserConverter.ToView(user);
|
||||
}
|
||||
|
||||
public void _validatePassword(string? password)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
throw new AccountException("The password is null.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
33
BusinessLogic/Tools/PasswordHasher.cs
Normal file
33
BusinessLogic/Tools/PasswordHasher.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.Tools
|
||||
{
|
||||
internal class PasswordHasher
|
||||
{
|
||||
/// <summary>
|
||||
/// Хеширует с использование SHA256
|
||||
/// </summary>
|
||||
/// <param name="password">Пароль</param>
|
||||
/// <returns>Хеш пароля</returns>
|
||||
public static string Hash(string password)
|
||||
{
|
||||
return BCrypt.Net.BCrypt.HashPassword(password);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проверяет на соответствие пароля и его хеша
|
||||
/// </summary>
|
||||
/// <param name="password">Пароль</param>
|
||||
/// <param name="passHash">Хеш пароля</param>
|
||||
/// <returns></returns>
|
||||
public static bool Verify(string password, string passHash)
|
||||
{
|
||||
return BCrypt.Net.BCrypt.Verify(password, passHash);
|
||||
}
|
||||
}
|
||||
}
|
14
Contracts/BindingModels/RoleBindingModel.cs
Normal file
14
Contracts/BindingModels/RoleBindingModel.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.BindingModels
|
||||
{
|
||||
public class RoleBindingModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
20
Contracts/BindingModels/UserBindingModel.cs
Normal file
20
Contracts/BindingModels/UserBindingModel.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.BindingModels
|
||||
{
|
||||
public class UserBindingModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string SecondName { get; set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string PasswordHash { get; set; } = string.Empty;
|
||||
public string? Password { get; set; }
|
||||
public DateTime Birthday { get; set; }
|
||||
public RoleBindingModel Role { get; set; } = null!;
|
||||
}
|
||||
}
|
24
Contracts/BusinessLogicContracts/IRoleLogic.cs
Normal file
24
Contracts/BusinessLogicContracts/IRoleLogic.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.BusinessLogicContracts
|
||||
{
|
||||
public interface IRoleLogic
|
||||
{
|
||||
RoleViewModel Create(RoleBindingModel model);
|
||||
|
||||
RoleViewModel Update(RoleBindingModel model);
|
||||
|
||||
RoleViewModel ReadElement(RoleSearchModel model);
|
||||
|
||||
IEnumerable<RoleViewModel> ReadElements(RoleSearchModel? model);
|
||||
|
||||
RoleViewModel Delete(RoleSearchModel model);
|
||||
}
|
||||
}
|
27
Contracts/BusinessLogicContracts/IUserLogic.cs
Normal file
27
Contracts/BusinessLogicContracts/IUserLogic.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.BusinessLogicContracts
|
||||
{
|
||||
public interface IUserLogic
|
||||
{
|
||||
UserViewModel Login(string email, string password);
|
||||
|
||||
UserViewModel Create(UserBindingModel model);
|
||||
|
||||
UserViewModel Update(UserBindingModel model);
|
||||
|
||||
UserViewModel ReadElement(UserSearchModel model);
|
||||
|
||||
IEnumerable<UserViewModel> ReadElements(UserSearchModel? model);
|
||||
|
||||
UserViewModel Delete(UserSearchModel model);
|
||||
}
|
||||
}
|
@ -6,12 +6,4 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="SearchModels\" />
|
||||
<Folder Include="BindingModels\" />
|
||||
<Folder Include="ViewModels\" />
|
||||
<Folder Include="StorageContracts\" />
|
||||
<Folder Include="BusinessLogicContracts\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
25
Contracts/Converters/RoleConverter.cs
Normal file
25
Contracts/Converters/RoleConverter.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.Converters
|
||||
{
|
||||
public static class RoleConverter
|
||||
{
|
||||
public static RoleViewModel ToView(RoleBindingModel model) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
};
|
||||
|
||||
public static RoleBindingModel ToBinding(RoleViewModel model) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
};
|
||||
}
|
||||
}
|
33
Contracts/Converters/UserConverter.cs
Normal file
33
Contracts/Converters/UserConverter.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.Converters
|
||||
{
|
||||
public static class UserConverter
|
||||
{
|
||||
public static UserViewModel ToView(UserBindingModel model) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
FirstName = model.FirstName,
|
||||
SecondName = model.SecondName,
|
||||
Email = model.Email,
|
||||
Birthday = model.Birthday,
|
||||
Role = RoleConverter.ToView(model.Role),
|
||||
};
|
||||
|
||||
public static UserBindingModel ToBinding(UserViewModel model) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
FirstName = model.FirstName,
|
||||
SecondName = model.SecondName,
|
||||
Email = model.Email,
|
||||
Birthday = model.Birthday,
|
||||
Role = RoleConverter.ToBinding(model.Role),
|
||||
};
|
||||
}
|
||||
}
|
23
Contracts/Exceptions/AccountException.cs
Normal file
23
Contracts/Exceptions/AccountException.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.Exceptions
|
||||
{
|
||||
public class AccountException : Exception
|
||||
{
|
||||
public AccountException()
|
||||
{
|
||||
}
|
||||
|
||||
public AccountException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public AccountException(string message, Exception inner) : base(message, inner)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
23
Contracts/Exceptions/ElementNotFoundException.cs
Normal file
23
Contracts/Exceptions/ElementNotFoundException.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.Exceptions
|
||||
{
|
||||
public class ElementNotFoundException : Exception
|
||||
{
|
||||
public ElementNotFoundException() : base("Element not founded.")
|
||||
{
|
||||
}
|
||||
|
||||
public ElementNotFoundException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public ElementNotFoundException(string message, Exception inner) : base(message, inner)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
13
Contracts/SearchModels/RoleSearchModel.cs
Normal file
13
Contracts/SearchModels/RoleSearchModel.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.SearchModels
|
||||
{
|
||||
public class RoleSearchModel
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
}
|
||||
}
|
14
Contracts/SearchModels/UserSearchModel.cs
Normal file
14
Contracts/SearchModels/UserSearchModel.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.SearchModels
|
||||
{
|
||||
public class UserSearchModel
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
public string? Email { get; set; }
|
||||
}
|
||||
}
|
24
Contracts/StorageContracts/IRoleStorage.cs
Normal file
24
Contracts/StorageContracts/IRoleStorage.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.StorageContracts
|
||||
{
|
||||
public interface IRoleStorage
|
||||
{
|
||||
RoleBindingModel? Insert(RoleBindingModel model);
|
||||
|
||||
IEnumerable<RoleBindingModel> GetList(RoleSearchModel? model);
|
||||
|
||||
RoleBindingModel? GetElement(RoleSearchModel model);
|
||||
|
||||
RoleBindingModel? Update(RoleBindingModel model);
|
||||
|
||||
RoleBindingModel? Delete(RoleSearchModel model);
|
||||
}
|
||||
}
|
23
Contracts/StorageContracts/IUserStorage.cs
Normal file
23
Contracts/StorageContracts/IUserStorage.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.SearchModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.StorageContracts
|
||||
{
|
||||
public interface IUserStorage
|
||||
{
|
||||
UserBindingModel? Insert(UserBindingModel model);
|
||||
|
||||
IEnumerable<UserBindingModel> GetList(UserSearchModel? model);
|
||||
|
||||
UserBindingModel? GetElement(UserSearchModel model);
|
||||
|
||||
UserBindingModel? Update(UserBindingModel model);
|
||||
|
||||
UserBindingModel? Delete(UserSearchModel model);
|
||||
}
|
||||
}
|
14
Contracts/ViewModels/RoleViewModel.cs
Normal file
14
Contracts/ViewModels/RoleViewModel.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.ViewModels
|
||||
{
|
||||
public class RoleViewModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
18
Contracts/ViewModels/UserViewModel.cs
Normal file
18
Contracts/ViewModels/UserViewModel.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Contracts.ViewModels
|
||||
{
|
||||
public class UserViewModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
public string SecondName { get; set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public DateTime Birthday { get; set; }
|
||||
public RoleViewModel Role { get; set; } = null!;
|
||||
}
|
||||
}
|
@ -6,8 +6,4 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
13
DataModels/IId.cs
Normal file
13
DataModels/IId.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DataModels
|
||||
{
|
||||
public interface IId
|
||||
{
|
||||
Guid Id { get; }
|
||||
}
|
||||
}
|
13
DataModels/Models/IRole.cs
Normal file
13
DataModels/Models/IRole.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DataModels.Models
|
||||
{
|
||||
public interface IRole : IId
|
||||
{
|
||||
string Name { get; }
|
||||
}
|
||||
}
|
17
DataModels/Models/IUser.cs
Normal file
17
DataModels/Models/IUser.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DataModels.Models
|
||||
{
|
||||
public interface IUser : IId
|
||||
{
|
||||
string FirstName { get; }
|
||||
string SecondName { get; }
|
||||
string PasswordHash { get; }
|
||||
string Email { get; }
|
||||
DateTime Birthday { get; }
|
||||
}
|
||||
}
|
25
DatabaseImplement/Database.cs
Normal file
25
DatabaseImplement/Database.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using DatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class Database : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseNpgsql("Server=192.168.191.42:32768;Database=gun_market;Username=postgres;Password=7355608;");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
public virtual DbSet<Role> Roles { get; set; } = null!;
|
||||
public virtual DbSet<User> Users { get; set; } = null!;
|
||||
}
|
||||
}
|
@ -7,8 +7,16 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
<Folder Include="Implements\" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Contracts\Contracts.csproj" />
|
||||
<ProjectReference Include="..\DataModels\DataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
88
DatabaseImplement/Implements/RoleStorage.cs
Normal file
88
DatabaseImplement/Implements/RoleStorage.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.StorageContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement.Implements
|
||||
{
|
||||
public class RoleStorage : IRoleStorage
|
||||
{
|
||||
public RoleBindingModel? Delete(RoleSearchModel model)
|
||||
{
|
||||
if (model.Id is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var context = new Database();
|
||||
var role = context.Roles.FirstOrDefault(r => r.Id == model.Id);
|
||||
if (role is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
context.Remove(role);
|
||||
context.SaveChanges();
|
||||
return role.GetBindingModel();
|
||||
}
|
||||
|
||||
public RoleBindingModel? GetElement(RoleSearchModel model)
|
||||
{
|
||||
if (model.Id is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var context = new Database();
|
||||
return context.Roles
|
||||
.FirstOrDefault(r => r.Id == model.Id)
|
||||
?.GetBindingModel();
|
||||
}
|
||||
|
||||
public IEnumerable<RoleBindingModel> GetList(RoleSearchModel? model)
|
||||
{
|
||||
var context = new Database();
|
||||
if (model is null)
|
||||
{
|
||||
return context.Roles.Select(r => r.GetBindingModel());
|
||||
}
|
||||
if (model.Id is null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
return context.Roles
|
||||
.Where(r => r.Id == model.Id)
|
||||
.Select(r => r.GetBindingModel());
|
||||
}
|
||||
|
||||
public RoleBindingModel? Insert(RoleBindingModel model)
|
||||
{
|
||||
var context = new Database();
|
||||
var newRole = Models.Role.ToRoleFromBinding(model);
|
||||
|
||||
context.Roles.Add(newRole);
|
||||
context.SaveChanges();
|
||||
|
||||
return newRole.GetBindingModel();
|
||||
}
|
||||
|
||||
public RoleBindingModel? Update(RoleBindingModel model)
|
||||
{
|
||||
var context = new Database();
|
||||
var role = context.Roles.FirstOrDefault(r => r.Id == model.Id);
|
||||
|
||||
if (role is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
role.Update(model);
|
||||
|
||||
context.SaveChanges();
|
||||
return role.GetBindingModel();
|
||||
}
|
||||
}
|
||||
}
|
109
DatabaseImplement/Implements/UserStorage.cs
Normal file
109
DatabaseImplement/Implements/UserStorage.cs
Normal file
@ -0,0 +1,109 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.StorageContracts;
|
||||
using DatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement.Implements
|
||||
{
|
||||
public class UserStorage : IUserStorage
|
||||
{
|
||||
public UserBindingModel? Delete(UserSearchModel model)
|
||||
{
|
||||
if (model.Id is null && model.Email is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var context = new Database();
|
||||
var user = context.Users.FirstOrDefault(u =>
|
||||
(model.Id.HasValue && u.Id == model.Id)
|
||||
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)));
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Remove(user);
|
||||
context.SaveChanges();
|
||||
|
||||
return user.GetBindingModel();
|
||||
}
|
||||
|
||||
public UserBindingModel? GetElement(UserSearchModel model)
|
||||
{
|
||||
if (model.Id is null && model.Email is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var context = new Database();
|
||||
return context.Users
|
||||
.Include(u => u.Role)
|
||||
.FirstOrDefault(u =>
|
||||
(model.Id.HasValue && u.Id == model.Id)
|
||||
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)))
|
||||
?.GetBindingModel();
|
||||
}
|
||||
|
||||
public IEnumerable<UserBindingModel> GetList(UserSearchModel? model)
|
||||
{
|
||||
var context = new Database();
|
||||
if (model is null)
|
||||
{
|
||||
return context.Users
|
||||
.Include(u => u.Role)
|
||||
.Select(r => r.GetBindingModel());
|
||||
}
|
||||
if (model.Id is null && model.Email is null)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
return context.Users
|
||||
.Where(u =>
|
||||
(model.Id.HasValue && u.Id == model.Id)
|
||||
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)))
|
||||
.Include(u => u.Role)
|
||||
.Select(r => r.GetBindingModel());
|
||||
}
|
||||
|
||||
public UserBindingModel? Insert(UserBindingModel model)
|
||||
{
|
||||
var context = new Database();
|
||||
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
|
||||
if (role is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var newUser = Models.User.ToUserFromBinding(model, role);
|
||||
|
||||
context.Users.Add(newUser);
|
||||
context.SaveChanges();
|
||||
|
||||
return newUser.GetBindingModel();
|
||||
}
|
||||
|
||||
public UserBindingModel? Update(UserBindingModel model)
|
||||
{
|
||||
var context = new Database();
|
||||
var user = context.Users
|
||||
.FirstOrDefault(u => u.Id == model.Id);
|
||||
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
|
||||
|
||||
if (user is null || role is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
user.Update(model, role);
|
||||
|
||||
context.SaveChanges();
|
||||
return user.GetBindingModel();
|
||||
}
|
||||
}
|
||||
}
|
89
DatabaseImplement/Migrations/20240604184007_registration_init.Designer.cs
generated
Normal file
89
DatabaseImplement/Migrations/20240604184007_registration_init.Designer.cs
generated
Normal file
@ -0,0 +1,89 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using DatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(Database))]
|
||||
[Migration("20240604184007_registration_init")]
|
||||
partial class registration_init
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.6")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("DatabaseImplement.Models.Role", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Roles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DatabaseImplement.Models.User", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime>("Birthday")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Guid?>("RoleId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("SecondName")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DatabaseImplement.Models.User", b =>
|
||||
{
|
||||
b.HasOne("DatabaseImplement.Models.Role", "Role")
|
||||
.WithMany()
|
||||
.HasForeignKey("RoleId");
|
||||
|
||||
b.Navigation("Role");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class registration_init : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Roles",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Name = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Roles", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Users",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
FirstName = table.Column<string>(type: "text", nullable: false),
|
||||
SecondName = table.Column<string>(type: "text", nullable: false),
|
||||
PasswordHash = table.Column<string>(type: "text", nullable: false),
|
||||
Email = table.Column<string>(type: "text", nullable: false),
|
||||
Birthday = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
||||
RoleId = table.Column<Guid>(type: "uuid", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Users_Roles_RoleId",
|
||||
column: x => x.RoleId,
|
||||
principalTable: "Roles",
|
||||
principalColumn: "Id");
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Users_RoleId",
|
||||
table: "Users",
|
||||
column: "RoleId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Roles");
|
||||
}
|
||||
}
|
||||
}
|
86
DatabaseImplement/Migrations/DatabaseModelSnapshot.cs
Normal file
86
DatabaseImplement/Migrations/DatabaseModelSnapshot.cs
Normal file
@ -0,0 +1,86 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using DatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(Database))]
|
||||
partial class DatabaseModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.6")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("DatabaseImplement.Models.Role", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Roles");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DatabaseImplement.Models.User", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTime>("Birthday")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("FirstName")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<string>("PasswordHash")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Guid?>("RoleId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("SecondName")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RoleId");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DatabaseImplement.Models.User", b =>
|
||||
{
|
||||
b.HasOne("DatabaseImplement.Models.Role", "Role")
|
||||
.WithMany()
|
||||
.HasForeignKey("RoleId");
|
||||
|
||||
b.Navigation("Role");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
47
DatabaseImplement/Models/Role.cs
Normal file
47
DatabaseImplement/Models/Role.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.ViewModels;
|
||||
using DataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement.Models
|
||||
{
|
||||
public class Role : IRole
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public RoleBindingModel GetBindingModel() => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
};
|
||||
|
||||
public static Role ToRoleFromView(RoleViewModel model) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
};
|
||||
|
||||
public static Role ToRoleFromBinding(RoleBindingModel model) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
};
|
||||
|
||||
public void Update(RoleBindingModel model)
|
||||
{
|
||||
if (model is null)
|
||||
{
|
||||
throw new ArgumentNullException("Update role: bindinng model is null");
|
||||
}
|
||||
Name = model.Name;
|
||||
}
|
||||
}
|
||||
}
|
82
DatabaseImplement/Models/User.cs
Normal file
82
DatabaseImplement/Models/User.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.ViewModels;
|
||||
using DataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement.Models
|
||||
{
|
||||
public class User : IUser
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string FirstName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string SecondName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string PasswordHash { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public DateTime Birthday { get; set; }
|
||||
|
||||
public Role? Role { get; set; }
|
||||
|
||||
public UserBindingModel GetBindingModel() => new()
|
||||
{
|
||||
Id = Id,
|
||||
FirstName = FirstName,
|
||||
SecondName = SecondName,
|
||||
Email = Email,
|
||||
PasswordHash = PasswordHash,
|
||||
Birthday = Birthday,
|
||||
Role = Role?.GetBindingModel() ?? new()
|
||||
};
|
||||
|
||||
public static User ToUserFromView(UserViewModel model, Role role) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
FirstName = model.FirstName,
|
||||
SecondName = model.SecondName,
|
||||
Email = model.Email,
|
||||
Birthday = model.Birthday,
|
||||
Role = role
|
||||
};
|
||||
|
||||
public static User ToUserFromBinding(UserBindingModel model, Role role) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
FirstName = model.FirstName,
|
||||
SecondName = model.SecondName,
|
||||
Email = model.Email,
|
||||
PasswordHash = model.PasswordHash,
|
||||
Birthday = model.Birthday,
|
||||
Role = role
|
||||
};
|
||||
|
||||
public void Update(UserBindingModel model, Role role)
|
||||
{
|
||||
if (model is null)
|
||||
{
|
||||
throw new ArgumentNullException("Update user: binding model is null");
|
||||
}
|
||||
|
||||
Email = model.Email;
|
||||
FirstName = model.FirstName;
|
||||
SecondName = model.SecondName;
|
||||
PasswordHash = model.PasswordHash;
|
||||
Birthday = model.Birthday;
|
||||
Role = role;
|
||||
}
|
||||
}
|
||||
}
|
120
RestAPI/Controllers/RoleController.cs
Normal file
120
RestAPI/Controllers/RoleController.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using BusinessLogic.BusinessLogic;
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogicContracts;
|
||||
using Contracts.Exceptions;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.ViewModels;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace RestAPI.Controllers
|
||||
{
|
||||
[Route("[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class RoleController
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IRoleLogic _roleLogic;
|
||||
|
||||
public RoleController(ILogger<RoleController> logger, IRoleLogic roleLogic)
|
||||
{
|
||||
_logger = logger;
|
||||
_roleLogic = roleLogic;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IResult Get([FromQuery] RoleSearchModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _roleLogic.ReadElement(model);
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogInformation(ex, "Role not found");
|
||||
return Results.NoContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error get role");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IResult GeFilteredtList([FromQuery] RoleSearchModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _roleLogic.ReadElements(model).ToList();
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error get list role");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IResult GetList()
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _roleLogic.ReadElements(null).ToList();
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error get list role");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IResult Create([FromBody] RoleBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _roleLogic.Create(model);
|
||||
return Results.Created(string.Empty, res);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error create role");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPatch]
|
||||
public IResult Update([FromBody] RoleBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _roleLogic.Update(model);
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error update role");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
public IResult Delete([FromQuery] RoleSearchModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _roleLogic.Delete(model); ;
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error delete role");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
130
RestAPI/Controllers/UserController.cs
Normal file
130
RestAPI/Controllers/UserController.cs
Normal file
@ -0,0 +1,130 @@
|
||||
using BusinessLogic.BusinessLogic;
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogicContracts;
|
||||
using Contracts.Exceptions;
|
||||
using Contracts.SearchModels;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace RestAPI.Controllers
|
||||
{
|
||||
[Route("[controller]/[action]")]
|
||||
[ApiController]
|
||||
public class UserController
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IUserLogic _userLogic;
|
||||
|
||||
public UserController(ILogger<UserController> logger, IUserLogic userLogic)
|
||||
{
|
||||
_userLogic = userLogic;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IResult Login(string email, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _userLogic.Login(email, password);
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogInformation(ex, "User not found");
|
||||
return Results.NoContent();
|
||||
}
|
||||
catch (AccountException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Wrong login data");
|
||||
return Results.BadRequest(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error get user");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IResult Registration([FromBody] UserBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _userLogic.Create(model);
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (AccountException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Wrong registration data");
|
||||
throw;
|
||||
return Results.BadRequest(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error create user");
|
||||
throw;
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IResult Get([FromQuery] UserSearchModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _userLogic.ReadElement(model);
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogInformation(ex, "User not found");
|
||||
return Results.NoContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error get user");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPatch]
|
||||
public IResult Update([FromBody] UserBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _userLogic.Update(model);
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (AccountException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Wrong update data");
|
||||
return Results.BadRequest(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error update user");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
public IResult Delete(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = _userLogic.Delete(new() { Id = id });
|
||||
return Results.Ok(res);
|
||||
}
|
||||
catch (ElementNotFoundException ex)
|
||||
{
|
||||
_logger.LogInformation(ex, "User not found");
|
||||
return Results.NoContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error delete user");
|
||||
return Results.Problem(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace RestAPI.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetWeatherForecast")]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +1,43 @@
|
||||
using BusinessLogic.BusinessLogic;
|
||||
using Contracts.BusinessLogicContracts;
|
||||
using Contracts.StorageContracts;
|
||||
using DatabaseImplement.Implements;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System;
|
||||
|
||||
const string VERSION = "v1";
|
||||
const string TITLE = "21GunsRestAPI";
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Logging.SetMinimumLevel(LogLevel.Trace);
|
||||
builder.Logging.AddLog4Net("log4net.config");
|
||||
|
||||
#region DI
|
||||
|
||||
builder.Services.AddTransient<IRoleLogic, RoleLogic>();
|
||||
builder.Services.AddTransient<IUserLogic, UserLogic>();
|
||||
|
||||
builder.Services.AddTransient<IRoleStorage, RoleStorage>();
|
||||
builder.Services.AddTransient<IUserStorage, UserStorage>();
|
||||
|
||||
#endregion DI
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc(VERSION, new OpenApiInfo { Title = TITLE, Version = VERSION });
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint($"/swagger/{VERSION}/swagger.json", $"{TITLE} {VERSION}"));
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
@ -7,7 +7,24 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="8.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BusinessLogic\BusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\Contracts\Contracts.csproj" />
|
||||
<ProjectReference Include="..\DatabaseImplement\DatabaseImplement.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Update="log4net.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,6 +1,6 @@
|
||||
@RestAPI_HostAddress = http://localhost:5168
|
||||
|
||||
GET {{RestAPI_HostAddress}}/weatherforecast/
|
||||
GET {{RestAPI_HostAddress}}/api/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
|
@ -1,13 +0,0 @@
|
||||
namespace RestAPI
|
||||
{
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
}
|
16
RestAPI/log4net.config
Normal file
16
RestAPI/log4net.config
Normal file
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<log4net>
|
||||
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
|
||||
<file value="c:/temp/RestAPI.log" />
|
||||
<appendToFile value="true" />
|
||||
<maximumFileSize value="100KB" />
|
||||
<maxSizeRollBackups value="2" />
|
||||
<layout type="log4net.Layout.PatternLayout">
|
||||
<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
|
||||
</layout>
|
||||
</appender>
|
||||
<root>
|
||||
<level value="TRACE" />
|
||||
<appender-ref ref="RollingFile" />
|
||||
</root>
|
||||
</log4net>
|
Loading…
Reference in New Issue
Block a user