2023-02-26 23:01:50 +04:00
|
|
|
|
|
|
|
|
|
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 ReinforcedStorage : IReinforcedStorage
|
|
|
|
|
{
|
|
|
|
|
public ReinforcedViewModel? Delete(ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PrecastConcretePlantDataBase();
|
|
|
|
|
|
2023-03-28 10:53:40 +04:00
|
|
|
|
var element = context.Reinforcedies.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id);
|
2023-02-26 23:01:50 +04:00
|
|
|
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
2023-03-28 10:53:40 +04:00
|
|
|
|
context.Reinforcedies.Remove(element);
|
2023-02-26 23:01:50 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ReinforcedViewModel? GetElement(ReinforcedSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.ReinforcedName) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new PrecastConcretePlantDataBase();
|
|
|
|
|
|
2023-03-28 10:53:40 +04:00
|
|
|
|
return context.Reinforcedies.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;
|
2023-02-26 23:01:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ReinforcedViewModel> GetFilteredList(ReinforcedSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.ReinforcedName))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new PrecastConcretePlantDataBase();
|
|
|
|
|
|
2023-03-28 10:53:40 +04:00
|
|
|
|
return context.Reinforcedies.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ReinforcedName.Contains(model.ReinforcedName)).ToList().Select(x => x.GetViewModel).ToList();
|
2023-02-26 23:01:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ReinforcedViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new PrecastConcretePlantDataBase();
|
|
|
|
|
|
2023-03-28 10:53:40 +04:00
|
|
|
|
return context.Reinforcedies.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
|
2023-02-26 23:01:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ReinforcedViewModel? Insert(ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PrecastConcretePlantDataBase();
|
|
|
|
|
|
|
|
|
|
var newReinforced = Reinforced.Create(context, model);
|
|
|
|
|
|
|
|
|
|
if (newReinforced == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 10:53:40 +04:00
|
|
|
|
context.Reinforcedies.Add(newReinforced);
|
2023-02-26 23:01:50 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return newReinforced.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ReinforcedViewModel? Update(ReinforcedBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PrecastConcretePlantDataBase();
|
|
|
|
|
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-03-28 10:53:40 +04:00
|
|
|
|
var reinforced = context.Reinforcedies.FirstOrDefault(rec => rec.Id == model.Id);
|
2023-02-26 23:01:50 +04:00
|
|
|
|
|
|
|
|
|
if (reinforced == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reinforced.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
reinforced.UpdateComponents(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
|
|
|
|
return reinforced.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|