fix user storage (user search)

This commit is contained in:
mfnefd 2024-06-06 17:52:09 +04:00
parent ba55b692b4
commit 5345098bbd
2 changed files with 25 additions and 26 deletions

View File

@ -3,6 +3,7 @@ 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;
@ -21,7 +22,9 @@ namespace DatabaseImplement.Implements
}
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)
{
@ -42,7 +45,9 @@ namespace DatabaseImplement.Implements
var context = new Database();
return context.Users
.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();
}
@ -60,7 +65,9 @@ namespace DatabaseImplement.Implements
return [];
}
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)
.Select(r => r.GetBindingModel());
}
@ -68,7 +75,12 @@ namespace DatabaseImplement.Implements
public UserBindingModel? Insert(UserBindingModel model)
{
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.SaveChanges();
@ -80,15 +92,15 @@ namespace DatabaseImplement.Implements
{
var context = new Database();
var user = context.Users
.Include(u => u.Role)
.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;
}
user.Update(model);
user.Update(model, role);
context.SaveChanges();
return user.GetBindingModel();

View File

@ -43,17 +43,17 @@ namespace DatabaseImplement.Models
Role = Role?.GetBindingModel() ?? new()
};
public static User ToUserFromView(UserViewModel model) => 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 = 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,
FirstName = model.FirstName,
@ -61,10 +61,10 @@ namespace DatabaseImplement.Models
Email = model.Email,
PasswordHash = model.PasswordHash,
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)
{
@ -76,20 +76,7 @@ namespace DatabaseImplement.Models
SecondName = model.SecondName;
PasswordHash = model.PasswordHash;
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;
Role = role;
}
}
}