CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/UserStorage.cs

84 lines
2.7 KiB
C#
Raw Normal View History

2024-04-29 01:55:20 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Implements
{
2024-05-19 16:35:35 +04:00
public class UserStorage : IClientStorage
2024-04-29 01:55:20 +04:00
{
2024-05-19 16:35:35 +04:00
public ClientViewModel? Delete(UserBindingModel model)
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
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-05-19 16:35:35 +04:00
public ClientViewModel? GetElement(ClientSearchModel model)
2024-04-29 01:55:20 +04:00
{
if (string.IsNullOrEmpty(model.Login) && !model.ID.HasValue)
{
return null;
}
using var context = new Database();
return context.Users.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Login) && x.Login == model.Login ||
(model.ID.HasValue && x.ID == model.ID)))?.GetViewModel;
}
2024-05-19 16:35:35 +04:00
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
2024-04-29 01:55:20 +04:00
{
if (string.IsNullOrEmpty(model.Login))
{
return new();
}
using var context = new Database();
return context.Users.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList();
}
2024-05-19 16:35:35 +04:00
public List<ClientViewModel> GetFullList()
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
return context.Users.Select(x => x.GetViewModel).ToList();
}
2024-05-19 16:35:35 +04:00
public ClientViewModel? Insert(UserBindingModel model)
2024-04-29 01:55:20 +04:00
{
var newComponent = User.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new Database();
context.Users.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
2024-05-19 16:35:35 +04:00
public ClientViewModel? Update(UserBindingModel model)
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
var component = context.Users.FirstOrDefault(x => x.ID == model.ID);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}