PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesDatabaseImplement/Implements/ImplementStorage.cs

66 lines
2.3 KiB
C#

using SewingDressesContracts.BindingModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.ViewModels;
using SewingDressesDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SewingDressesDatabaseImplement.Implements
{
public class ImplementStorage : IImplementStorage
{
public ImplementViewModel? Insert(ImplementBindingModel? model)
{
using var context = new SewingDressesDatabase();
var newImplement = Implement.Create(model);
if (newImplement == null)
return null;
context.Implements.Add(newImplement);
context.SaveChanges();
return newImplement.GetViewModel;
}
public ImplementViewModel? Update(ImplementBindingModel? model)
{
using var context = new SewingDressesDatabase();
if (model == null)
return null;
var implement = context.Implements.FirstOrDefault(x => x.Id == model.Id);
if (implement == null) return null;
implement.Update(model);
context.SaveChanges();
return implement.GetViewModel;
}
public ImplementViewModel? Delete(ImplementBindingModel? model)
{
using var context = new SewingDressesDatabase();
var implement = context.Implements.FirstOrDefault(x => x.Id == model.Id);
if (implement == null) return null;
context.Implements.Remove(implement);
context.SaveChanges();
return implement.GetViewModel;
}
public ImplementViewModel? GetElement(ImplementSearchModel model)
{
if (string.IsNullOrEmpty(model.ImplementFIO) && !model.Id.HasValue)
return null;
using var context = new SewingDressesDatabase();
return context.Implements.FirstOrDefault(x => (string.IsNullOrEmpty(model.ImplementFIO) || x.ImplementFIO.Equals(model.ImplementFIO)) && (!model.Id.HasValue || model.Id == x.Id))?.GetViewModel;
}
public List<ImplementViewModel> GetFullList()
{
using var context = new SewingDressesDatabase();
return context.Implements.Select(x => x.GetViewModel).ToList();
}
public List<ImplementViewModel> GetFilteredList(ImplementSearchModel model)
{
if (string.IsNullOrEmpty(model.ImplementFIO)) return new();
using var context = new SewingDressesDatabase();
return context.Implements.Where(x => x.ImplementFIO.Equals(model.ImplementFIO)).Select(x => x.GetViewModel).ToList();
}
}
}