base logic #5

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

View File

@ -3,6 +3,7 @@ using Contracts.SearchModels;
using Contracts.StorageContracts; using Contracts.StorageContracts;
using DatabaseImplement.Models; using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -21,7 +22,9 @@ namespace DatabaseImplement.Implements
} }
var context = new Database(); var context = new Database();
var user = context.Users.FirstOrDefault(u => u.Equals(model)); 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) if (user is null)
{ {
@ -42,7 +45,9 @@ namespace DatabaseImplement.Implements
var context = new Database(); var context = new Database();
return context.Users return context.Users
.Include(u => u.Role) .Include(u => u.Role)
.FirstOrDefault(u => u.Equals(model)) .FirstOrDefault(u =>
(model.Id.HasValue && u.Id == model.Id)
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)))
?.GetBindingModel(); ?.GetBindingModel();
} }
@ -60,7 +65,9 @@ namespace DatabaseImplement.Implements
return []; return [];
} }
return context.Users return context.Users
.Where(u => u.Equals(model)) .Where(u =>
(model.Id.HasValue && u.Id == model.Id)
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)))
.Include(u => u.Role) .Include(u => u.Role)
.Select(r => r.GetBindingModel()); .Select(r => r.GetBindingModel());
} }
@ -68,7 +75,12 @@ namespace DatabaseImplement.Implements
public UserBindingModel? Insert(UserBindingModel model) public UserBindingModel? Insert(UserBindingModel model)
{ {
var context = new Database(); var context = new Database();
var newUser = Models.User.ToUserFromBinding(model); 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.Users.Add(newUser);
context.SaveChanges(); context.SaveChanges();
@ -80,15 +92,15 @@ namespace DatabaseImplement.Implements
{ {
var context = new Database(); var context = new Database();
var user = context.Users var user = context.Users
.Include(u => u.Role)
.FirstOrDefault(u => u.Id == model.Id); .FirstOrDefault(u => u.Id == model.Id);
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
if (user is null) if (user is null || role is null)
{ {
return null; return null;
} }
user.Update(model); user.Update(model, role);
context.SaveChanges(); context.SaveChanges();
return user.GetBindingModel(); return user.GetBindingModel();

View File

@ -43,17 +43,17 @@ namespace DatabaseImplement.Models
Role = Role?.GetBindingModel() ?? new() Role = Role?.GetBindingModel() ?? new()
}; };
public static User ToUserFromView(UserViewModel model) => new() public static User ToUserFromView(UserViewModel model, Role role) => new()
{ {
Id = model.Id, Id = model.Id,
FirstName = model.FirstName, FirstName = model.FirstName,
SecondName = model.SecondName, SecondName = model.SecondName,
Email = model.Email, Email = model.Email,
Birthday = model.Birthday, Birthday = model.Birthday,
Role = Models.Role.ToRoleFromView(model.Role) Role = role
}; };
public static User ToUserFromBinding(UserBindingModel model) => new() public static User ToUserFromBinding(UserBindingModel model, Role role) => new()
{ {
Id = model.Id, Id = model.Id,
FirstName = model.FirstName, FirstName = model.FirstName,
@ -61,10 +61,10 @@ namespace DatabaseImplement.Models
Email = model.Email, Email = model.Email,
PasswordHash = model.PasswordHash, PasswordHash = model.PasswordHash,
Birthday = model.Birthday, Birthday = model.Birthday,
Role = Models.Role.ToRoleFromBinding(model.Role) Role = role
}; };
public void Update(UserBindingModel model) public void Update(UserBindingModel model, Role role)
{ {
if (model is null) if (model is null)
{ {
@ -76,20 +76,7 @@ namespace DatabaseImplement.Models
SecondName = model.SecondName; SecondName = model.SecondName;
PasswordHash = model.PasswordHash; PasswordHash = model.PasswordHash;
Birthday = model.Birthday; Birthday = model.Birthday;
Role = Models.Role.ToRoleFromBinding(model.Role); Role = 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;
} }
} }
} }