base logic #5

Merged
mfnefd merged 25 commits from registration into main 2024-06-10 12:27:55 +04:00
7 changed files with 255 additions and 9 deletions
Showing only changes of commit d964ec4383 - Show all commits

View File

@ -7,7 +7,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="StorageContracts\" />
<Folder Include="BusinessLogicContracts\" /> <Folder Include="BusinessLogicContracts\" />
</ItemGroup> </ItemGroup>

View 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);
}
}

View 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);
}
}

View File

@ -6,10 +6,6 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6"> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>

View File

@ -0,0 +1,92 @@
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 is null || 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 is null || 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)
{
if (model is null)
{
return null;
}
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();
}
}
}

View File

@ -0,0 +1,93 @@
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 UserStorage : IUserStorage
{
public UserBindingModel? Delete(UserSearchModel model)
{
if (model is null || (model.Id is null && model.Email is null))
{
return null;
}
var context = new Database();
var user = context.Users.FirstOrDefault(u => u.Equals(model));
if (user is null)
{
return null;
}
context.Remove(user);
context.SaveChanges();
return user.GetBindingModel();
}
public UserBindingModel? GetElement(UserSearchModel model)
{
if (model is null || (model.Id is null && model.Email is null))
{
return null;
}
var context = new Database();
return context.Users
.FirstOrDefault(u => u.Equals(model))
?.GetBindingModel();
}
public IEnumerable<UserBindingModel> GetList(UserSearchModel? model)
{
var context = new Database();
if (model is null)
{
return context.Users.Select(r => r.GetBindingModel());
}
if (model.Id is null && model.Email is null)
{
return [];
}
return context.Users
.Where(u => u.Equals(model))
.Select(r => r.GetBindingModel());
}
public UserBindingModel? Insert(UserBindingModel model)
{
if (model is null)
{
return null;
}
var context = new Database();
var newUser = Models.User.ToUserFromBinding(model);
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);
if (user is null)
{
return null;
}
user.Update(model);
context.SaveChanges();
return user.GetBindingModel();
}
}
}

View File

@ -1,4 +1,5 @@
using Contracts.BindingModels; using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels; using Contracts.ViewModels;
using DataModels.Models; using DataModels.Models;
using System; using System;
@ -31,11 +32,16 @@ namespace DatabaseImplement.Models
public Role? Role { get; set; } public Role? Role { get; set; }
public UserBindingModel GetBindingModel() public UserBindingModel GetBindingModel() => new()
{ {
// TODO: get binding with a role by database contetxt Id = Id,
throw new NotImplementedException(); FirstName = FirstName,
} SecondName = SecondName,
Email = Email,
PasswordHash = PasswordHash,
Birthday = Birthday,
Role = Role?.GetBindingModel() ?? new()
};
public static User ToUserFromView(UserViewModel model) => new() public static User ToUserFromView(UserViewModel model) => new()
{ {
@ -72,5 +78,18 @@ namespace DatabaseImplement.Models
Birthday = model.Birthday; Birthday = model.Birthday;
Role = Models.Role.ToRoleFromBinding(model.Role); Role = Models.Role.ToRoleFromBinding(model.Role);
} }
public bool Equals(UserSearchModel model)
{
if (model.Id is null)
{
return Email.Contains(model.Email!);
}
if (string.IsNullOrWhiteSpace(model.Email))
{
return Id == model.Id;
}
return false;
}
} }
} }