Реализовал хранилище бд для пользователя

This commit is contained in:
Никита Потапов 2024-04-29 21:08:50 +04:00
parent 7bb90df611
commit 56bba6809d

View File

@ -1,7 +1,10 @@
using PolyclinicContracts.BindingModels; using Microsoft.IdentityModel.Tokens;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.SearchModels; using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts; using PolyclinicContracts.StoragesContracts;
using PolyclinicContracts.ViewModels; using PolyclinicContracts.ViewModels;
using PolyclinicDatabaseImplement.Models;
using SecuritySystemDatabaseImplement;
namespace PolyclinicDatabaseImplement.Implements namespace PolyclinicDatabaseImplement.Implements
{ {
@ -9,32 +12,65 @@ namespace PolyclinicDatabaseImplement.Implements
{ {
public UserViewModel? Delete(UserBindingModel model) public UserViewModel? Delete(UserBindingModel model)
{ {
throw new NotImplementedException(); 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) public UserViewModel? GetElement(UserSearchModel model)
{ {
throw new NotImplementedException(); return GetFilteredList(model).FirstOrDefault();
} }
public List<UserViewModel> GetFilteredList(UserSearchModel model) public List<UserViewModel> GetFilteredList(UserSearchModel model)
{ {
throw new NotImplementedException(); var elements = GetFullList();
foreach (var prop in model.GetType().GetProperties())
{
if (model.GetType().GetProperty(prop.Name)?.GetValue(model, null) != null)
{
elements = elements.Where(x => x.GetType().GetProperty(prop.Name)?.GetValue(x, null) == model.GetType().GetProperty(prop.Name)?.GetValue(model, null)).ToList();
}
}
return elements;
} }
public List<UserViewModel> GetFullList() public List<UserViewModel> GetFullList()
{ {
throw new NotImplementedException(); using var context = new PolyclinicDatabase();
return context.Users.Select(x => x.GetViewModel).ToList();
} }
public UserViewModel? Insert(UserBindingModel model) public UserViewModel? Insert(UserBindingModel model)
{ {
throw new NotImplementedException(); 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) public UserViewModel? Update(UserBindingModel model)
{ {
throw new NotImplementedException(); 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;
} }
} }
} }