Storage contracts, implements and small fix
This commit is contained in:
parent
82ad684fe5
commit
d964ec4383
@ -7,7 +7,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="StorageContracts\" />
|
||||
<Folder Include="BusinessLogicContracts\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
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);
|
||||
}
|
||||
}
|
@ -6,10 +6,6 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Implements\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
92
DatabaseImplement/Implements/RoleStorage.cs
Normal file
92
DatabaseImplement/Implements/RoleStorage.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
93
DatabaseImplement/Implements/UserStorage.cs
Normal file
93
DatabaseImplement/Implements/UserStorage.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.ViewModels;
|
||||
using DataModels.Models;
|
||||
using System;
|
||||
@ -31,11 +32,16 @@ namespace DatabaseImplement.Models
|
||||
|
||||
public Role? Role { get; set; }
|
||||
|
||||
public UserBindingModel GetBindingModel()
|
||||
public UserBindingModel GetBindingModel() => new()
|
||||
{
|
||||
// TODO: get binding with a role by database contetxt
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
Id = Id,
|
||||
FirstName = FirstName,
|
||||
SecondName = SecondName,
|
||||
Email = Email,
|
||||
PasswordHash = PasswordHash,
|
||||
Birthday = Birthday,
|
||||
Role = Role?.GetBindingModel() ?? new()
|
||||
};
|
||||
|
||||
public static User ToUserFromView(UserViewModel model) => new()
|
||||
{
|
||||
@ -72,5 +78,18 @@ namespace DatabaseImplement.Models
|
||||
Birthday = model.Birthday;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user