пашет пашет

This commit is contained in:
kamilia 2024-06-21 14:01:04 +04:00
parent 9be26628db
commit e56dbb7f4b
8 changed files with 126 additions and 109 deletions

View File

@ -28,6 +28,7 @@ namespace AircraftPlantListImplement.Implements
{
_source = DataListSingleton.GetInstance();
}
/// <summary>
/// Получение полного списка
/// </summary>
@ -41,6 +42,7 @@ namespace AircraftPlantListImplement.Implements
}
return result;
}
/// <summary>
/// Получение фильтрованного списка
/// </summary>
@ -63,6 +65,7 @@ namespace AircraftPlantListImplement.Implements
}
return result;
}
/// <summary>
/// Получение элемента
/// </summary>
@ -84,6 +87,7 @@ namespace AircraftPlantListImplement.Implements
}
return null;
}
/// <summary>
/// Добавление элемента
/// </summary>
@ -109,6 +113,7 @@ namespace AircraftPlantListImplement.Implements
_source.Orders.Add(newOrder);
return GetViewModel(newOrder);
}
/// <summary>
/// Редактирование элемента
/// </summary>
@ -126,6 +131,7 @@ namespace AircraftPlantListImplement.Implements
}
return null;
}
/// <summary>
/// Удаление элемента
/// </summary>
@ -144,6 +150,7 @@ namespace AircraftPlantListImplement.Implements
}
return null;
}
/// <summary>
/// Получение модели заказа
/// </summary>
@ -152,15 +159,15 @@ namespace AircraftPlantListImplement.Implements
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
foreach (var plane in _source.Planes)
foreach (var sushi in _source.Planes)
{
if (plane.Id == order.PlaneId)
if (sushi.Id == order.PlaneId)
{
viewModel.PlaneName = plane.PlaneName;
viewModel.PlaneName = sushi.PlaneName;
break;
}
}
return viewModel;
}
}
}
}

View File

@ -16,7 +16,9 @@ namespace AircraftPlantDatabaseImplement.Implements
public List<ComponentViewModel> GetFullList()
{
using var context = new AircraftPlantDatabase();
return context.Components.Select(x => x.GetViewModel).ToList();
return context.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
@ -24,8 +26,12 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return new();
}
using var context = new AircraftPlantDatabase();
return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
return context.Components
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
@ -33,9 +39,13 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return null;
}
using var context = new AircraftPlantDatabase();
return context.Components.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==
model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
return context.Components
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) &&
x.ComponentName == model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
@ -44,6 +54,7 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return null;
}
using var context = new AircraftPlantDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
@ -57,6 +68,7 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
@ -65,13 +77,14 @@ namespace AircraftPlantDatabaseImplement.Implements
{
using var context = new AircraftPlantDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
if (element == null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
return null;
}
return null;
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
}
}
}

View File

@ -3,7 +3,11 @@ using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.StoragesContracts;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantDatabaseImplement.Implements
{
@ -13,38 +17,32 @@ namespace AircraftPlantDatabaseImplement.Implements
{
using var context = new AircraftPlantDatabase();
return context.Orders
.Include(x => x.Plane)
.Select(x => x.GetViewModel)
.ToList();
.Select(x => GetViewModel(x))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new AircraftPlantDatabase();
return context.Orders
.Include(x => x.Plane)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
.Where(x => x.Id.Equals(model.Id))
.Select(x => GetViewModel(x))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new AircraftPlantDatabase();
return context.Orders
.Include(x => x.Plane)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
using var context = new AircraftPlantDatabase();
return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)));
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
@ -52,15 +50,12 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return null;
}
using var context = new AircraftPlantDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders
.Include(x => x.Plane)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
return GetViewModel(newOrder);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new AircraftPlantDatabase();
@ -69,29 +64,33 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Orders
.Include(x => x.Plane)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
return GetViewModel(order);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new AircraftPlantDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
var deletedElement = context.Orders
.Include(x => x.Plane)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Orders.Remove(element);
context.SaveChanges();
return deletedElement;
return GetViewModel(element);
}
return null;
}
private static OrderViewModel GetViewModel(Order order)
{
using var context = new AircraftPlantDatabase();
var viewModel = order.GetViewModel;
var plane = context.Planes.FirstOrDefault(x => x.Id == order.PlaneId);
if (plane != null)
{
viewModel.PlaneName = plane.PlaneName;
}
return viewModel;
}
}
}

View File

@ -18,11 +18,11 @@ namespace AircraftPlantDatabaseImplement.Implements
{
using var context = new AircraftPlantDatabase();
return context.Planes
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<PlaneViewModel> GetFilteredList(PlaneSearchModel model)
{
@ -30,31 +30,31 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return new();
}
using var context = new AircraftPlantDatabase();
return context.Planes
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.PlaneName.Contains(model.PlaneName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.PlaneName.Contains(model.PlaneName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public PlaneViewModel? GetElement(PlaneSearchModel model)
{
if (string.IsNullOrEmpty(model.PlaneName) &&
!model.Id.HasValue)
if (string.IsNullOrEmpty(model.PlaneName) && !model.Id.HasValue)
{
return null;
}
using var context = new AircraftPlantDatabase();
return context.Planes
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) &&
x.PlaneName == model.PlaneName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) &&
x.PlaneName == model.PlaneName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public PlaneViewModel? Insert(PlaneBindingModel model)
{
@ -64,6 +64,7 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return null;
}
context.Planes.Add(newPlane);
context.SaveChanges();
return newPlane.GetViewModel;
@ -74,12 +75,12 @@ namespace AircraftPlantDatabaseImplement.Implements
using var transaction = context.Database.BeginTransaction();
try
{
var plane = context.Planes.FirstOrDefault(rec =>
rec.Id == model.Id);
var plane = context.Planes.FirstOrDefault(rec => rec.Id == model.Id);
if (plane == null)
{
return null;
}
plane.Update(model);
context.SaveChanges();
plane.UpdateComponents(context, model);
@ -96,8 +97,8 @@ namespace AircraftPlantDatabaseImplement.Implements
{
using var context = new AircraftPlantDatabase();
var element = context.Planes
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Planes.Remove(element);

View File

@ -5,28 +5,29 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AircraftPlantDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<PlaneComponent> PlaneComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
@ -34,27 +35,25 @@ namespace AircraftPlantDatabaseImplement.Models
Cost = model.Cost
};
}
public static Component Create(ComponentViewModel model)
{
return new Component
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,

View File

@ -2,40 +2,40 @@
using AircraftPlantContracts.ViewModels;
using AircraftPlantDataModels.Enums;
using AircraftPlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantDatabaseImplement.Models
{
/// <summary>
/// Сущность "Заказ"
/// </summary>
public class Order : IOrderModel
{
public int Id { get; set; }
[Required]
public int PlaneId { get; set; }
[Required]
public int Count { get; set; }
[Required]
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; }
[Required]
public DateTime DateCreate { get; set; }
public DateTime? DateImplement { get; set; }
public virtual Plane Plane { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
return new Order
{
Id = model.Id,
PlaneId = model.PlaneId,
@ -46,17 +46,16 @@ namespace AircraftPlantDatabaseImplement.Models
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
@ -65,8 +64,7 @@ namespace AircraftPlantDatabaseImplement.Models
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
PlaneName = Plane.PlaneName
DateImplement = DateImplement
};
}
}

View File

@ -1,13 +1,13 @@
using System;
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AircraftPlantDatabaseImplement.Models
{
@ -19,6 +19,7 @@ namespace AircraftPlantDatabaseImplement.Models
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _planeComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> PlaneComponents
{
@ -27,7 +28,7 @@ namespace AircraftPlantDatabaseImplement.Models
if (_planeComponents == null)
{
_planeComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _planeComponents;
}
@ -55,6 +56,7 @@ namespace AircraftPlantDatabaseImplement.Models
PlaneName = model.PlaneName;
Price = model.Price;
}
public PlaneViewModel GetViewModel => new()
{
Id = Id,
@ -66,10 +68,11 @@ namespace AircraftPlantDatabaseImplement.Models
{
var planeComponents = context.PlaneComponents.Where(rec => rec.PlaneId == model.Id).ToList();
if (planeComponents != null && planeComponents.Count > 0)
{ // удалили те, которых нет в модели
{
// Удаление компонентов, которых нет в модели
context.PlaneComponents.RemoveRange(planeComponents.Where(rec => !model.PlaneComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
// Обновление количества у существующих записей
foreach (var updateComponent in planeComponents)
{
updateComponent.Count = model.PlaneComponents[updateComponent.ComponentId].Item2;
@ -91,4 +94,4 @@ namespace AircraftPlantDatabaseImplement.Models
_planeComponents = null;
}
}
}
}

View File

@ -12,19 +12,18 @@ using System.Threading.Tasks;
namespace AircraftPlantDatabaseImplement.Models
{
/// <summary>
/// Сущность "Магазин"
/// </summary>
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime DateOpening { get; set; }
private Dictionary<int, (IPlaneModel, int)>? _shopPlanes = null;
[NotMapped]
@ -41,10 +40,8 @@ namespace AircraftPlantDatabaseImplement.Models
}
}
public int MaxPlanes { get; set; }
[ForeignKey("ShopId")]
public List<ShopPlane> Planes { get; set; } = new();
public static Shop Create(AircraftPlantDatabase context, ShopBindingModel model)
{
return new Shop