2024-04-29 21:08:50 +04:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using PolyclinicContracts.BindingModels;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
using PolyclinicContracts.SearchModels;
|
|
|
|
|
using PolyclinicContracts.StoragesContracts;
|
|
|
|
|
using PolyclinicContracts.ViewModels;
|
2024-04-29 21:08:50 +04:00
|
|
|
|
using PolyclinicDatabaseImplement.Models;
|
|
|
|
|
using SecuritySystemDatabaseImplement;
|
2024-04-28 20:10:45 +04:00
|
|
|
|
|
|
|
|
|
namespace PolyclinicDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class UserStorage : IUserStorage
|
|
|
|
|
{
|
2024-04-29 20:23:37 +04:00
|
|
|
|
public UserViewModel? Delete(UserBindingModel model)
|
|
|
|
|
{
|
2024-04-29 21:08:50 +04:00
|
|
|
|
using var context = new PolyclinicDatabase();
|
|
|
|
|
var element = context.Users.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Users.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? GetElement(UserSearchModel model)
|
|
|
|
|
{
|
2024-04-29 21:08:50 +04:00
|
|
|
|
return GetFilteredList(model).FirstOrDefault();
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<UserViewModel> GetFilteredList(UserSearchModel model)
|
|
|
|
|
{
|
2024-04-29 21:08:50 +04:00
|
|
|
|
var elements = GetFullList();
|
2024-05-29 02:22:44 +04:00
|
|
|
|
if (model.Id != null)
|
2024-04-29 21:08:50 +04:00
|
|
|
|
{
|
2024-05-29 02:22:44 +04:00
|
|
|
|
elements = elements.Where(x => x.Id == model.Id).ToList();
|
|
|
|
|
}
|
|
|
|
|
if (!model.Email.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
elements = elements.Where(x => x.Email == model.Email).ToList();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (!model.Password.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
elements = elements.Where(x => x.Password == model.Password).ToList();
|
|
|
|
|
|
|
|
|
|
}
|
2024-04-29 21:08:50 +04:00
|
|
|
|
return elements;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<UserViewModel> GetFullList()
|
|
|
|
|
{
|
2024-04-29 21:08:50 +04:00
|
|
|
|
using var context = new PolyclinicDatabase();
|
|
|
|
|
return context.Users.Select(x => x.GetViewModel).ToList();
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? Insert(UserBindingModel model)
|
|
|
|
|
{
|
2024-04-29 21:08:50 +04:00
|
|
|
|
var element = User.Create(model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new PolyclinicDatabase();
|
|
|
|
|
context.Users.Add(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? Update(UserBindingModel model)
|
|
|
|
|
{
|
2024-04-29 21:08:50 +04:00
|
|
|
|
using var context = new PolyclinicDatabase();
|
|
|
|
|
var element = context.Users.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
element.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
2024-04-28 20:10:45 +04:00
|
|
|
|
}
|
|
|
|
|
}
|