CourseWork_Bank/Bank/BankDataBaseImplement/Implemets/ClercStorage.cs
2023-04-09 12:14:52 +04:00

123 lines
3.7 KiB
C#

using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BankDataBaseImplement.Implemets
{
public class ClercStorage : IClercStorage
{
public ClercViewModel? Delete(ClercBindingModel model)
{
using var context = new BankDataBase();
var element = context.Clercs.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clercs.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ClercViewModel? GetElement(ClercSearchModel model)
{
using var context = new BankDataBase();
if (model.Id.HasValue)
return context.Clercs
.Include(x => x.Additions)
.Include(x => x.Members)
.Include(x => x.Conferences)
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
if (!string.IsNullOrEmpty(model.ClercLogin) && !string.IsNullOrEmpty(model.ClercPassword))
return context.Clercs
.Include(x => x.Additions)
.Include(x => x.Members)
.Include(x => x.Conferences)
.FirstOrDefault(x => x.ClercLogin.Equals(model.ClercLogin) && x.ClercPassword.Equals(model.ClercPassword))?
.GetViewModel;
if (!string.IsNullOrEmpty(model.ClercLogin))
return context.Clercs
.Include(x => x.Additions)
.Include(x => x.Members)
.Include(x => x.Conferences)
.FirstOrDefault(x => x.ClercLogin.Equals(model.ClercLogin))?
.GetViewModel;
return null;
}
public List<ClercViewModel> GetFilteredList(ClercSearchModel model)
{
if (string.IsNullOrEmpty(model.ClercFIO))
{
return new();
}
using var context = new BankDataBase();
return context.Clercs
.Include(x => x.Additions)
.Include(x => x.Members)
.Include(x => x.Conferences)
.Where(x => x.ClercLogin.Contains(model.ClercLogin) && x.ClercPassword == model.ClercPassword)
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClercViewModel> GetFullList()
{
using var context = new BankDataBase();
return context.Clercs
.Select(x => x.GetViewModel)
.ToList();
}
public ClercViewModel? Insert(ClercBindingModel model)
{
var newOrganiser = Clerc.Create(model);
if (newOrganiser == null)
{
return null;
}
using var context = new BankDataBase();
context.Clercs.Add(newOrganiser);
context.SaveChanges();
return newOrganiser.GetViewModel;
}
public ClercViewModel? Update(ClercBindingModel model)
{
using var context = new BankDataBase();
var organiser = context.Clercs
.FirstOrDefault(x => x.Id == model.Id);
if (organiser == null)
{
return null;
}
organiser.Update(model);
context.SaveChanges();
return organiser.GetViewModel;
}
}
}