2023-03-18 19:33:11 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using PrecastConcretePlantContracts.BindingModels;
|
|
|
|
|
using PrecastConcretePlantContracts.SearchModels;
|
|
|
|
|
using PrecastConcretePlantContracts.StoragesContracts;
|
|
|
|
|
using PrecastConcretePlantContracts.ViewModels;
|
|
|
|
|
using PrecastConcretePlantDatabaseImplement.Models;
|
|
|
|
|
using System;
|
2023-03-18 18:19:08 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-03-18 19:33:11 +04:00
|
|
|
|
namespace PrecastConcretePlantDatabaseImplement.Implements
|
2023-03-18 18:19:08 +04:00
|
|
|
|
{
|
2023-03-18 19:33:11 +04:00
|
|
|
|
public class ReinforcedStorage : IReinforcedStorage
|
2023-03-18 18:19:08 +04:00
|
|
|
|
{
|
2023-03-18 19:33:11 +04:00
|
|
|
|
public List<ReinforcedViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new PrecastConcretePlantDatabase();
|
|
|
|
|
return context.Reinforceds
|
|
|
|
|
.Include(x => x.Components)
|
|
|
|
|
.ThenInclude(x => x.Component)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<ReinforcedViewModel> GetFilteredList(ReinforcedSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.ReinforcedName))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new PrecastConcretePlantDatabase();
|
|
|
|
|
return context.Reinforceds
|
|
|
|
|
.Include(x => x.Components)
|
|
|
|
|
.ThenInclude(x => x.Component)
|
|
|
|
|
.Where(x => x.ReinforcedName.Contains(model.ReinforcedName))
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public ReinforcedViewModel? GetElement(ReinforcedSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.ReinforcedName) &&
|
|
|
|
|
!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new PrecastConcretePlantDatabase();
|
|
|
|
|
return context.Reinforceds
|
|
|
|
|
.Include(x => x.Components)
|
|
|
|
|
.ThenInclude(x => x.Component)
|
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ReinforcedName) &&
|
|
|
|
|
x.ReinforcedName == model.ReinforcedName) ||
|
|
|
|
|
(model.Id.HasValue && x.Id ==
|
|
|
|
|
model.Id))
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public ReinforcedViewModel? Insert(ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PrecastConcretePlantDatabase();
|
|
|
|
|
var newReinforced = Reinforced.Create(context, model);
|
|
|
|
|
if (newReinforced == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Reinforceds.Add(newReinforced);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newReinforced.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public ReinforcedViewModel? Update(ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PrecastConcretePlantDatabase();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var Reinforced = context.Reinforceds.FirstOrDefault(rec =>
|
|
|
|
|
rec.Id == model.Id);
|
|
|
|
|
if (Reinforced == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Reinforced.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
Reinforced.UpdateComponents(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return Reinforced.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public ReinforcedViewModel? Delete(ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PrecastConcretePlantDatabase();
|
|
|
|
|
var element = context.Reinforceds
|
|
|
|
|
.Include(x => x.Components)
|
|
|
|
|
.Include(x=>x.Orders)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Reinforceds.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-03-18 18:19:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|