PIbd-21_Ihonkina_E.S._Preca.../PrecastConcretePlantDataBaseImplemet/Implements/ImplementerStorage.cs
2023-04-28 21:24:51 +03:00

116 lines
3.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
public List<ImplementerViewModel> GetFullList()
{
using var context = new PrecastConcretePlantDatabase();
return context.Implementers
.Select(x => x.GetViewModel)
.ToList();
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
if (model == null)
{
return new();
}
if (model.Id.HasValue)
{
var implementer = GetElement(model);
return implementer != null ? new() { implementer } : new();
}
if (model.ImplementerFIO != null)
{
using var context = new SoftwareInstallationDataBase();
return context.Implementers
.Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO))
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
using var context = new PrecastConcretePlantDatabase();
if (model.Id.HasValue)
return context.Implementers
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
if (model.ImplementerFIO != null && model.Password != null)
return context.Implementers
.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO)
&& x.Password.Equals(model.Password))
?.GetViewModel;
if (model.ImplementerFIO != null)
return context.Implementers
.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))
?.GetViewModel;
return null;
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
using var context = new PrecastConcretePlantDatabase();
var implementer = Implementer.Create(model);
if (implementer != null)
{
context.Implementers.Add(implementer);
context.SaveChanges();
}
return implementer?.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
using var context = new PrecastConcretePlantDatabase();
var implementer = context.Implementers
.FirstOrDefault(x => x.Id == model.Id);
if (implementer != null)
{
implementer.Update(model);
context.SaveChanges();
}
return implementer?.GetViewModel;
}
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
using var context = new PrecastConcretePlantDatabase();
var implementer = context.Implementers
.FirstOrDefault(rec => rec.Id == model.Id);
if (implementer != null)
{
context.Implementers.Remove(implementer);
context.SaveChanges();
}
return implementer?.GetViewModel;
}
}
}