Implementer и Order для всех вариантов хранения данных.

This commit is contained in:
Programmist73 2023-04-05 11:00:15 +04:00
parent d15a6c79ee
commit 52d5d17600
13 changed files with 310 additions and 17 deletions

View File

@ -15,7 +15,9 @@ namespace BlacksmithWorkshopContracts.BindingModels
public int ClientId { get; set; }
public int ManufactureId { get; set; }
public int ImplementerId { get; set; }
public int ManufactureId { get; set; }
public int Count { get; set; }

View File

@ -15,6 +15,9 @@ namespace BlacksmithWorkshopContracts.SearchModels
//для поиска по клиенту
public int? ClientId { get; set; }
//для поиска по исполнителю
public int? ImplementerId { get; set; }
//два поля для возможности производить выборку
public DateTime? DateFrom { get; set; }

View File

@ -17,6 +17,8 @@ namespace BlacksmithWorkshopContracts.ViewModels
public int ClientId { get; set; }
public int ImplementerId { get; set; }
public int ManufactureId { get; set; }
[DisplayName("ФИО клиента")]
@ -25,7 +27,10 @@ namespace BlacksmithWorkshopContracts.ViewModels
[DisplayName("Изделие")]
public string ManufactureName { get; set; } = string.Empty;
[DisplayName("Количество")]
[DisplayName("ФИО исполнителя")]
public string ImplementerFIO { get; set; } = string.Empty;
[DisplayName("Количество")]
public int Count { get; set; }
[DisplayName("Сумма")]

View File

@ -16,8 +16,11 @@ namespace BlacksmithWorkshopDataModels.Models
//id клиента
int ClientId { get; }
//кол-во продуктов
int Count { get; }
//id исполнителя
int ImplementerId { get; }
//кол-во продуктов
int Count { get; }
//суммарная стоимость продуктов
double Sum { get; }

View File

@ -30,5 +30,7 @@ namespace BlacksmithWorkshopDatabaseImplement
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Client> Clients { get; set; }
}
public virtual DbSet<Implementer> Implementers { set; get; }
}
}

View File

@ -0,0 +1,128 @@
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 ImplementerStorage : IImplementerStorage
{
public List<ImplementerViewModel> GetFullList()
{
using var context = new BlacksmithWorkshopDatabase();
return context.Implementers
.Include(x => x.Orders)
.Select(x => x.GetViewModel)
.ToList();
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
if (string.IsNullOrEmpty(model.ImplementerFIO))
{
return new();
}
using var context = new BlacksmithWorkshopDatabase();
return context.Implementers
.Include(x => x.Orders)
.Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO))
.Select(x => x.GetViewModel)
.ToList();
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
using var context = new BlacksmithWorkshopDatabase();
if (model.Id.HasValue)
{
return context.Implementers
.Include(x => x.Orders)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
else if (!string.IsNullOrEmpty(model.ImplementerFIO) && !string.IsNullOrEmpty(model.Password))
{
return context.Implementers
.Include(x => x.Orders)
.FirstOrDefault(x => (x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password))
?.GetViewModel;
}
return new();
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
var newImplementer = Implementer.Create(model);
if (newImplementer == null)
{
return null;
}
using var context = new BlacksmithWorkshopDatabase();
context.Implementers.Add(newImplementer);
context.SaveChanges();
return context.Implementers
.Include(x => x.Orders)
.FirstOrDefault(x => x.Id == newImplementer.Id)
?.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var order = context.Implementers
.Include(x => x.Orders)
.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Implementers
.Include(x => x.Orders)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var element = context.Implementers
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
// для отображения КОРРЕКТНОЙ ViewModel-и
var deletedElement = context.Implementers
.Include(x => x.Orders)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Implementers.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null;
}
}
}

View File

@ -27,6 +27,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
var deletedElement = context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
@ -51,6 +52,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
@ -64,7 +66,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Include(x => x.Implementer)
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
@ -73,7 +76,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Where(x => x.Id == model.Id)
.Include(x => x.Implementer)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
@ -82,10 +86,21 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Include(x => x.Implementer)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.ImplementerId.HasValue)
{
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Include(x => x.Implementer)
.Where(x => x.ImplementerId == model.ImplementerId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
@ -97,6 +112,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Include(x => x.Implementer)
.Select(x => x.GetViewModel)
.ToList();
}
@ -118,6 +134,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
}
@ -125,10 +142,12 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var order = context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == model.Id);
.Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
@ -141,6 +160,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}

View File

@ -0,0 +1,86 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.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;
namespace BlacksmithWorkshopDatabaseImplement.Models
{
public class Implementer : IImplementerModel
{
public int Id { get; set; }
[Required]
public string ImplementerFIO { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[Required]
public int WorkExperience { get; set; }
[Required]
public int Qualification { get; set; }
//для реализации связи один ко многим с заказами
[ForeignKey("ImplementerId")]
public virtual List<Order> Orders { get; set; } = new();
public static Implementer? Create(ImplementerBindingModel model)
{
if (model == null)
{
return null;
}
return new Implementer()
{
Id = model.Id,
Password = model.Password,
ImplementerFIO = model.ImplementerFIO,
Qualification = model.Qualification,
WorkExperience = model.WorkExperience
};
}
public static Implementer Create(ImplementerViewModel model)
{
return new Implementer
{
Id = model.Id,
Password = model.Password,
ImplementerFIO = model.ImplementerFIO,
Qualification = model.Qualification,
WorkExperience = model.WorkExperience
};
}
public void Update(ImplementerBindingModel model)
{
if (model == null)
{
return;
}
Id = model.Id;
Password = model.Password;
ImplementerFIO = model.ImplementerFIO;
Qualification = model.Qualification;
WorkExperience = model.WorkExperience;
}
public ImplementerViewModel GetViewModel => new()
{
Id = Id,
Password = Password,
ImplementerFIO = ImplementerFIO,
Qualification = Qualification,
WorkExperience = WorkExperience
};
}
}

View File

@ -22,6 +22,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
[Required]
public int ClientId { get; private set; }
[Required]
public int ImplementerId { get; private set; }
[Required]
public int Count { get; private set; }
@ -42,7 +45,10 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
//для передачи имени клиента
public virtual Client Client { get; set; }
public static Order? Create(OrderBindingModel model)
//для передачи имени исполнителя
public virtual Implementer Implementer { get; set; }
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
@ -54,6 +60,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
Id = model.Id,
ManufactureId = model.ManufactureId,
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -78,13 +85,15 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
Id = Id,
ManufactureId = ManufactureId,
ClientId = ClientId,
ImplementerId = ImplementerId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ManufactureName = Manufacture.ManufactureName,
ClientFIO = Client.ClientFIO
ClientFIO = Client.ClientFIO,
ImplementerFIO = Implementer.ImplementerFIO
};
}
}

View File

@ -48,8 +48,15 @@ namespace BlacksmithWorkshopFileImplement.Implements
.Select(x => GetViewModel(x))
.ToList();
}
else if (model.ImplementerId.HasValue)
{
return source.Orders
.Where(x => x.ImplementerId == model.ImplementerId)
.Select(x => GetViewModel(x))
.ToList();
}
return new();
return new();
}
public OrderViewModel? GetElement(OrderSearchModel model)
@ -62,7 +69,7 @@ namespace BlacksmithWorkshopFileImplement.Implements
return source.Orders.FirstOrDefault(x =>(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
//для загрузки названий изделия в заказе
//для загрузки названий и имён в заказ
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
@ -71,6 +78,8 @@ namespace BlacksmithWorkshopFileImplement.Implements
var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
var implementer = source.Implementers.FirstOrDefault(x => x.Id == order.ImplementerId);
if(manufacture != null)
{
viewModel.ManufactureName = manufacture.ManufactureName;
@ -81,7 +90,12 @@ namespace BlacksmithWorkshopFileImplement.Implements
viewModel.ClientFIO = client.ClientFIO;
}
return viewModel;
if (implementer != null)
{
viewModel.ImplementerFIO = implementer.ImplementerFIO;
}
return viewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)

View File

@ -19,6 +19,8 @@ namespace BlacksmithWorkshopFileImplement.Models
public int ClientId { get; private set; }
public int ImplementerId { get; private set; }
public int ManufactureId { get; private set; }
public int Count { get; private set; }
@ -43,6 +45,7 @@ namespace BlacksmithWorkshopFileImplement.Models
Id = model.Id,
ClientId = model.ClientId,
ManufactureId = model.ManufactureId,
ImplementerId = model.ImplementerId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -61,7 +64,8 @@ namespace BlacksmithWorkshopFileImplement.Models
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ClientId = Convert.ToInt32(element.Attribute("Id")!.Value),
ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value),
ImplementerId = Convert.ToInt32(element.Attribute("ImplementerId")!.Value),
ManufactureId = Convert.ToInt32(element.Element("ManufactureId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
@ -87,7 +91,8 @@ namespace BlacksmithWorkshopFileImplement.Models
{
Id = Id,
ClientId = ClientId,
ManufactureId = ManufactureId,
ImplementerId = ImplementerId,
ManufactureId = ManufactureId,
Count = Count,
Sum = Sum,
Status = Status,
@ -98,6 +103,7 @@ namespace BlacksmithWorkshopFileImplement.Models
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("ClientId", ClientId.ToString()),
new XElement("ImplementerId", ClientId.ToString()),
new XElement("ManufactureId", ManufactureId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),

View File

@ -100,7 +100,7 @@ namespace BlacksmithWorkshopListImplement.Implements
return null;
}
//метод для записи названия изделия на форме с заказами
//метод для передачи названий и имён на форму
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
@ -124,6 +124,16 @@ namespace BlacksmithWorkshopListImplement.Implements
}
}
foreach (var implementer in _source.Implementers)
{
if (implementer.Id == order.ImplementerId)
{
viewModel.ImplementerFIO = implementer.ImplementerFIO;
break;
}
}
return viewModel;
}

View File

@ -5,6 +5,7 @@ using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
@ -19,6 +20,8 @@ namespace BlacksmithWorkshopListImplement.Models
public int ClientId { get; private set; }
public int ImplementerId { get; private set; }
public int ManufactureId { get; private set; }
public int Count { get; private set; }
@ -43,6 +46,7 @@ namespace BlacksmithWorkshopListImplement.Models
Id = model.Id,
ManufactureId = model.ManufactureId,
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -69,7 +73,8 @@ namespace BlacksmithWorkshopListImplement.Models
Id = Id,
ManufactureId = ManufactureId,
ClientId = ClientId,
Count = Count,
ImplementerId = ImplementerId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,