107 lines
3.4 KiB
C#
107 lines
3.4 KiB
C#
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using SoftwareInstallationContracts.BindingModels;
|
|||
|
using SoftwareInstallationContracts.SearchModels;
|
|||
|
using SoftwareInstallationContracts.StoragesContracts;
|
|||
|
using SoftwareInstallationContracts.ViewModels;
|
|||
|
using SoftwareInstallationDataBaseImplement.Models;
|
|||
|
|
|||
|
namespace SoftwareInstallationDataBaseImplement.Implements
|
|||
|
{
|
|||
|
public class PackageStorage : IPackageStorage
|
|||
|
{
|
|||
|
public PackageViewModel? Delete(PackageBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SoftwareInstallationDataBase();
|
|||
|
|
|||
|
var element = context.Packages.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Packages.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public PackageViewModel? GetElement(PackageSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.PackageName) && !model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
using var context = new SoftwareInstallationDataBase();
|
|||
|
|
|||
|
return context.Packages.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.PackageName) && x.PackageName == model.PackageName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public List<PackageViewModel> GetFilteredList(PackageSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.PackageName))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
using var context = new SoftwareInstallationDataBase();
|
|||
|
|
|||
|
return context.Packages.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.PackageName.Contains(model.PackageName)).ToList().Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<PackageViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new SoftwareInstallationDataBase();
|
|||
|
|
|||
|
return context.Packages.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public PackageViewModel? Insert(PackageBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SoftwareInstallationDataBase();
|
|||
|
|
|||
|
var newPackage = Package.Create(context, model);
|
|||
|
|
|||
|
if (newPackage == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
context.Packages.Add(newPackage);
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
return newPackage.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public PackageViewModel? Update(PackageBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SoftwareInstallationDataBase();
|
|||
|
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
var package = context.Packages.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
|
|||
|
if (package == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
package.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
package.UpdateComponents(context, model);
|
|||
|
transaction.Commit();
|
|||
|
|
|||
|
return package.GetViewModel;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|