2024-05-11 02:30:02 +04:00
|
|
|
|
using FurnitureAssemblyContracts.BindingModels;
|
|
|
|
|
using FurnitureAssemblyContracts.SearchModels;
|
|
|
|
|
using FurnitureAssemblyContracts.StoragesContracts;
|
|
|
|
|
using FurnitureAssemblyContracts.ViewModels;
|
|
|
|
|
using FurnitureAssemblyDatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace FurnitureAssemblyDatabaseImplement.Implements
|
|
|
|
|
{
|
2024-05-11 23:46:17 +04:00
|
|
|
|
public class ImplementerStorage : IImplementerStorage
|
|
|
|
|
{
|
|
|
|
|
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FurnitureAssemblyDatabase();
|
|
|
|
|
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Implementers
|
2024-05-12 00:01:23 +04:00
|
|
|
|
.Include(x => x.Order)
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(model.ImplementerFIO) && !string.IsNullOrEmpty(model.Password))
|
|
|
|
|
{
|
|
|
|
|
return context.Implementers
|
2024-05-12 00:01:23 +04:00
|
|
|
|
.Include(x => x.Order)
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.FirstOrDefault(x => (x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password))
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ImplementerViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new FurnitureAssemblyDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Implementers
|
2024-05-12 00:01:23 +04:00
|
|
|
|
.Include(x => x.Order)
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.ImplementerFIO))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new FurnitureAssemblyDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Implementers
|
2024-05-12 00:01:23 +04:00
|
|
|
|
.Include(x => x.Order)
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImplementerViewModel? Insert(ImplementerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var newImplementer = Implementer.Create(model);
|
|
|
|
|
|
|
|
|
|
if (newImplementer == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new FurnitureAssemblyDatabase();
|
|
|
|
|
|
|
|
|
|
context.Implementers.Add(newImplementer);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return context.Implementers
|
2024-05-12 00:01:23 +04:00
|
|
|
|
.Include(x => x.Order)
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == newImplementer.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImplementerViewModel? Update(ImplementerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FurnitureAssemblyDatabase();
|
|
|
|
|
|
|
|
|
|
var order = context.Implementers
|
2024-05-12 00:01:23 +04:00
|
|
|
|
.Include(x => x.Order)
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
order.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return context.Implementers
|
2024-05-12 00:01:23 +04:00
|
|
|
|
.Include(x => x.Order)
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImplementerViewModel? Delete(ImplementerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new FurnitureAssemblyDatabase();
|
|
|
|
|
|
|
|
|
|
var element = context.Implementers
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
// для отображения КОРРЕКТНОЙ ViewModel-и
|
|
|
|
|
var deletedElement = context.Implementers
|
2024-05-12 00:01:23 +04:00
|
|
|
|
.Include(x => x.Order)
|
2024-05-11 23:46:17 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
|
|
|
|
|
context.Implementers.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return deletedElement;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-11 02:30:02 +04:00
|
|
|
|
}
|