base logic #5
@ -13,6 +13,5 @@ namespace DataModels.Models
|
||||
string PasswordHash { get; }
|
||||
string Email { get; }
|
||||
DateTime Birthday { get; }
|
||||
Guid RoleId { 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.85: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,20 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
<Folder Include="Implements\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<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>
|
||||
|
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;
|
||||
}
|
||||
}
|
||||
}
|
76
DatabaseImplement/Models/User.cs
Normal file
76
DatabaseImplement/Models/User.cs
Normal file
@ -0,0 +1,76 @@
|
||||
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 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()
|
||||
{
|
||||
// TODO: get binding with a role by database contetxt
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public static User ToUserFromView(UserViewModel model) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
FirstName = model.FirstName,
|
||||
SecondName = model.SecondName,
|
||||
Email = model.Email,
|
||||
Birthday = model.Birthday,
|
||||
Role = Models.Role.ToRoleFromView(model.Role)
|
||||
};
|
||||
|
||||
public static User ToUserFromBinding(UserBindingModel model) => new()
|
||||
{
|
||||
Id = model.Id,
|
||||
FirstName = model.FirstName,
|
||||
SecondName = model.SecondName,
|
||||
Email = model.Email,
|
||||
PasswordHash = model.PasswordHash,
|
||||
Birthday = model.Birthday,
|
||||
Role = Models.Role.ToRoleFromBinding(model.Role)
|
||||
};
|
||||
|
||||
public void Update(UserBindingModel model)
|
||||
{
|
||||
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 = Models.Role.ToRoleFromBinding(model.Role);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user