CourseWork_Bank/Bank/BankDatabaseImplement/Implements/BankOperatorStorage.cs

89 lines
2.7 KiB
C#

using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
public class BankOperatorStorage : IBankOperatorStorage
{
public BankOperatorViewModel? Delete(BankOperatorBindingModel model)
{
using var context = new BankDatabase();
var element = context.BankOperators.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.BankOperators.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public BankOperatorViewModel? GetElement(BankOperatorSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.BankOperators
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public List<BankOperatorViewModel> GetFilteredList(BankOperatorSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new BankDatabase();
return context.BankOperators
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<BankOperatorViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.BankOperators
.Select(x => x.GetViewModel)
.ToList();
}
public BankOperatorViewModel? Insert(BankOperatorBindingModel model)
{
using var context = new BankDatabase();
var newBankOperator = BankOperator.Create(model);
if (newBankOperator == null)
{
return null;
}
context.BankOperators.Add(newBankOperator);
context.SaveChanges();
return newBankOperator.GetViewModel;
}
public BankOperatorViewModel? Update(BankOperatorBindingModel model)
{
using var context = new BankDatabase();
var bankOperator = context.BankOperators.FirstOrDefault(x => x.Id == model.Id);
if (bankOperator == null)
{
return null;
}
bankOperator.Update(model);
context.SaveChanges();
return bankOperator.GetViewModel;
}
}
}