Eliseev E.E. LabWork03 #4
@ -0,0 +1,127 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.SearchModels;
|
||||
using BlacksmithWorkshopContracts.StoragesContracts;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
{
|
||||
public class ManufactureStorage : IManufactureStorage
|
||||
{
|
||||
public List<ManufactureViewModel> GetFullList()
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
return context.Manufactures
|
||||
.Include(x => x.WorkPieces)
|
||||
.ThenInclude(x => x.WorkPiece)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ManufactureViewModel> GetFilteredList(ManufactureSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ManufactureName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
return context.Manufactures
|
||||
.Include(x => x.WorkPieces)
|
||||
.ThenInclude(x => x.WorkPiece)
|
||||
.Where(x => x.ManufactureName.Contains(model.ManufactureName))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public ManufactureViewModel? GetElement(ManufactureSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ManufactureName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
return context.Manufactures
|
||||
.Include(x => x.WorkPieces)
|
||||
.ThenInclude(x => x.WorkPiece)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ManufactureName) && x.ManufactureName == model.ManufactureName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public ManufactureViewModel? Insert(ManufactureBindingModel model)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var newManufacture = Manufacture.Create(context, model);
|
||||
|
||||
if (newManufacture == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
context.Manufactures.Add(newManufacture);
|
||||
context.SaveChanges();
|
||||
|
||||
return newManufacture.GetViewModel;
|
||||
}
|
||||
|
||||
public ManufactureViewModel? Update(ManufactureBindingModel model)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
|
||||
try
|
||||
{
|
||||
var manufacture = context.Manufactures.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
|
||||
if (manufacture == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
manufacture.Update(model);
|
||||
context.SaveChanges();
|
||||
manufacture.UpdateWorkPieces(context, model);
|
||||
transaction.Commit();
|
||||
|
||||
return manufacture.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public ManufactureViewModel? Delete(ManufactureBindingModel model)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var element = context.Manufactures
|
||||
.Include(x => x.WorkPieces)
|
||||
.Include(x => x.Orders)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
|
||||
if (element != null)
|
||||
{
|
||||
context.Manufactures.Remove(element);
|
||||
context.SaveChanges();
|
||||
|
||||
return element.GetViewModel;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.SearchModels;
|
||||
using BlacksmithWorkshopContracts.StoragesContracts;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
{
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var element = context.Orders
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
|
||||
if (element != null)
|
||||
{
|
||||
context.Orders.Remove(element);
|
||||
context.SaveChanges();
|
||||
|
||||
return element.GetViewModel;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
return context.Orders
|
||||
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
return context.Orders
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static OrderViewModel GetViewModel(Order order)
|
||||
{
|
||||
var viewModel = order.GetViewModel;
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var element = context.Manufactures
|
||||
.FirstOrDefault(x => x.Id == order.ManufactureId);
|
||||
viewModel.ManufactureName = element.ManufactureName;
|
||||
|
||||
return viewModel;
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
return context.Orders
|
||||
.Select(x => new OrderViewModel
|
||||
{
|
||||
Id = x.Id,
|
||||
ManufactureId = x.ManufactureId,
|
||||
Count = x.Count,
|
||||
Sum = x.Sum,
|
||||
Status = x.Status,
|
||||
DateCreate = x.DateCreate,
|
||||
DateImplement = x.DateImplement,
|
||||
//ManufactureName = x.Manufacture.ManufactureName //тут не будет работать
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
var newOrder = Order.Create(model);
|
||||
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
context.Orders.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
|
||||
return newOrder.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
|
||||
return order.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
{
|
||||
internal class WorkPieceStorage : IWorkPieceStorage
|
||||
public class WorkPieceStorage : IWorkPieceStorage
|
||||
{
|
||||
public List<WorkPieceViewModel> GetFullList()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user