110 lines
3.7 KiB
C#
110 lines
3.7 KiB
C#
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using ShipyardContracts.BindingModels;
|
|||
|
using ShipyardContracts.SearchModels;
|
|||
|
using ShipyardContracts.StoragesContracts;
|
|||
|
using ShipyardContracts.ViewModels;
|
|||
|
using ShipyardDatabaseImplement;
|
|||
|
using ShipyardDatabaseImplement.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace ShipyardDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class ShipStorage : IShipStorage
|
|||
|
{
|
|||
|
public List<ShipViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new ShipyardDataBase();
|
|||
|
return context.Ships
|
|||
|
.Include(x => x.Components)
|
|||
|
.ThenInclude(x => x.Component)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public List<ShipViewModel> GetFilteredList(ShipSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.ShipName))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
using var context = new ShipyardDataBase();
|
|||
|
return context.Ships.Include(x => x.Components)
|
|||
|
.ThenInclude(x => x.Component)
|
|||
|
.Where(x => x.ShipName.Contains(model.ShipName))
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public ShipViewModel? GetElement(ShipSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.ShipName) &&
|
|||
|
!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new ShipyardDataBase();
|
|||
|
return context.Ships
|
|||
|
.Include(x => x.Components)
|
|||
|
.ThenInclude(x => x.Component)
|
|||
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShipName) &&
|
|||
|
x.ShipName == model.ShipName) ||
|
|||
|
(model.Id.HasValue && x.Id ==
|
|||
|
model.Id))
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
public ShipViewModel? Insert(ShipBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ShipyardDataBase();
|
|||
|
var newManufacture = Ship.Create(context, model);
|
|||
|
if (newManufacture == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Ships.Add(newManufacture);
|
|||
|
context.SaveChanges();
|
|||
|
return newManufacture.GetViewModel;
|
|||
|
}
|
|||
|
public ShipViewModel? Update(ShipBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ShipyardDataBase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var manufacture = context.Ships.FirstOrDefault(rec =>
|
|||
|
rec.Id == model.Id);
|
|||
|
if (manufacture == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
manufacture.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
manufacture.UpdateComponents(context, model);
|
|||
|
transaction.Commit();
|
|||
|
return manufacture.GetViewModel;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
public ShipViewModel? Delete(ShipBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ShipyardDataBase();
|
|||
|
var element = context.Ships
|
|||
|
.Include(x => x.Components)
|
|||
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Ships.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|