PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicDatabaseImplement/Implements/UserStorage.cs

84 lines
2.6 KiB
C#

using Microsoft.IdentityModel.Tokens;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts;
using PolyclinicContracts.ViewModels;
using PolyclinicDatabaseImplement.Models;
using SecuritySystemDatabaseImplement;
namespace PolyclinicDatabaseImplement.Implements
{
public class UserStorage : IUserStorage
{
public UserViewModel? Delete(UserBindingModel model)
{
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;
}
public UserViewModel? GetElement(UserSearchModel model)
{
return GetFilteredList(model).FirstOrDefault();
}
public List<UserViewModel> GetFilteredList(UserSearchModel model)
{
var elements = GetFullList();
if (model.Id != null)
{
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();
}
return elements;
}
public List<UserViewModel> GetFullList()
{
using var context = new PolyclinicDatabase();
return context.Users.Select(x => x.GetViewModel).ToList();
}
public UserViewModel? Insert(UserBindingModel model)
{
var element = User.Create(model);
if (element == null)
{
return null;
}
using var context = new PolyclinicDatabase();
context.Users.Add(element);
context.SaveChanges();
return element.GetViewModel;
}
public UserViewModel? Update(UserBindingModel model)
{
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;
}
}
}