73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
using ConstructionFirmDatabaseImplement.Models;
|
|
using Subd_4.BindingModels;
|
|
using Subd_4.SearchModels;
|
|
using Subd_4.StoragesContracts;
|
|
using Subd_4.ViewModels;
|
|
|
|
namespace ConstructionFirmDatabaseImplement.Implements
|
|
{
|
|
public class SpecialtyStorage : ISpecialtyStorage
|
|
{
|
|
public List<SpecialtyViewModel> GetFilteredList(SpecialtySearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.SpecialtyName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Specialtys.Where(x => x.SpecialtyName.Contains(model.SpecialtyName)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<SpecialtyViewModel> GetFullList()
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Specialtys.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public SpecialtyViewModel? Delete(SpecialtyBindingModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
var element = context.Specialtys.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Specialtys.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public SpecialtyViewModel? GetElement(SpecialtySearchModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Specialtys.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SpecialtyName)) && x.SpecialtyName == model.SpecialtyName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public SpecialtyViewModel? Insert(SpecialtyBindingModel model)
|
|
{
|
|
var newSpecialty = Specialty.Create(model);
|
|
if (newSpecialty == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new ConstructionFirmDatabase();
|
|
context.Specialtys.Add(newSpecialty);
|
|
context.SaveChanges();
|
|
return newSpecialty.GetViewModel;
|
|
}
|
|
|
|
public SpecialtyViewModel? Update(SpecialtyBindingModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
var component = context.Specialtys.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
}
|
|
}
|