корректировка моделей

This commit is contained in:
Мк Игорь 2023-04-06 22:44:17 +04:00
parent 2b6e86ce12
commit 6c2a55ae40
20 changed files with 292 additions and 43 deletions

View File

@ -41,7 +41,7 @@ namespace CarServiceDatabase.Implements
public RepairRequestViewModel? Insert(RepairRequestBindingModel model) public RepairRequestViewModel? Insert(RepairRequestBindingModel model)
{ {
using var context = new CarServiceDbContext(); using var context = new CarServiceDbContext();
var newRepairRequest = RepairRequest.Create(model); var newRepairRequest = RepairRequest.Create(context, model);
if (newRepairRequest != null) if (newRepairRequest != null)
{ {
context.RepairRequests.Add(newRepairRequest); context.RepairRequests.Add(newRepairRequest);
@ -58,7 +58,7 @@ namespace CarServiceDatabase.Implements
{ {
return null; return null;
} }
repairRequest.Update(model); repairRequest.Update(context, model);
context.SaveChanges(); context.SaveChanges();
return repairRequest.GetViewModel; return repairRequest.GetViewModel;
} }

View File

@ -39,7 +39,7 @@ namespace CarServiceDatabase.Implements
public VehicleViewModel? Insert(VehicleBindingModel model) public VehicleViewModel? Insert(VehicleBindingModel model)
{ {
using var context = new CarServiceDbContext(); using var context = new CarServiceDbContext();
var newVehicle = Vehicle.Create(model); var newVehicle = Vehicle.Create(context, model);
if (newVehicle != null) if (newVehicle != null)
{ {
context.Vehicles.Add(newVehicle); context.Vehicles.Add(newVehicle);
@ -57,7 +57,7 @@ namespace CarServiceDatabase.Implements
{ {
return null; return null;
} }
vehicle.Update(model); vehicle.Update(context, model);
context.SaveChanges(); context.SaveChanges();
return vehicle.GetViewModel; return vehicle.GetViewModel;
} }

View File

@ -41,7 +41,7 @@ namespace CarServiceDatabase.Implements
public WorkInRequestViewModel? Insert(WorkInRequestBindingModel model) public WorkInRequestViewModel? Insert(WorkInRequestBindingModel model)
{ {
using var context = new CarServiceDbContext(); using var context = new CarServiceDbContext();
var newWorkInRequest = WorkInRequest.Create(model); var newWorkInRequest = WorkInRequest.Create(context, model);
if (newWorkInRequest != null) if (newWorkInRequest != null)
{ {
context.WorksInRequest.Add(newWorkInRequest); context.WorksInRequest.Add(newWorkInRequest);
@ -58,7 +58,7 @@ namespace CarServiceDatabase.Implements
{ {
return null; return null;
} }
workInRequest.Update(model); workInRequest.Update(context, model);
context.SaveChanges(); context.SaveChanges();
return workInRequest.GetViewModel; return workInRequest.GetViewModel;
} }

View File

@ -40,7 +40,7 @@ namespace CarServiceDatabase.Implements
public WorkPaymentViewModel? Insert(WorkPaymentBindingModel model) public WorkPaymentViewModel? Insert(WorkPaymentBindingModel model)
{ {
using var context = new CarServiceDbContext(); using var context = new CarServiceDbContext();
var newWorkPayment = WorkPayment.Create(model); var newWorkPayment = WorkPayment.Create(context, model);
if (newWorkPayment != null) if (newWorkPayment != null)
{ {
context.WorkPayments.Add(newWorkPayment); context.WorkPayments.Add(newWorkPayment);
@ -57,7 +57,7 @@ namespace CarServiceDatabase.Implements
{ {
return null; return null;
} }
workPayment.Update(model); workPayment.Update(context, model);
context.SaveChanges(); context.SaveChanges();
return workPayment.GetViewModel; return workPayment.GetViewModel;
} }

View File

@ -30,7 +30,6 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
Login = model.Login, Login = model.Login,
Password = model.Password, Password = model.Password,
Name = model.Name, Name = model.Name,

View File

@ -31,7 +31,6 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
Name = model.Name, Name = model.Name,
Price = model.Price, Price = model.Price,
Count = model.Count, Count = model.Count,
@ -44,7 +43,6 @@ namespace CarServiceDatabase.Models
{ {
return; return;
} }
Id = model.Id;
Name = model.Name; Name = model.Name;
Price = model.Price; Price = model.Price;
Count = model.Count; Count = model.Count;

View File

@ -24,7 +24,6 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
Count = model.Count, Count = model.Count,
Item = context.Items.First(x => x.Id == model.ItemId), Item = context.Items.First(x => x.Id == model.ItemId),
RepairRequest = context.RepairRequests.First(x => x.Id == model.RepairRequestId) RepairRequest = context.RepairRequests.First(x => x.Id == model.RepairRequestId)
@ -36,10 +35,9 @@ namespace CarServiceDatabase.Models
{ {
return; return;
} }
Id = model.Id;
Count = model.Count; Count = model.Count;
ItemId = model.ItemId; Item = context.Items.First(x => x.Id == model.ItemId);
RepairRequestId = model.RepairRequestId; RepairRequest = context.RepairRequests.First(x => x.Id == model.RepairRequestId);
} }
public ItemForRepairViewModel GetViewModel => new() public ItemForRepairViewModel GetViewModel => new()
{ {

View File

@ -24,7 +24,7 @@ namespace CarServiceDatabase.Models
[ForeignKey("RepairRequestId")] [ForeignKey("RepairRequestId")]
public virtual List<ItemForRepair> ItemsForRepair { get; set; } = new(); public virtual List<ItemForRepair> ItemsForRepair { get; set; } = new();
public virtual Vehicle Vehicle { get; set; } = new(); public virtual Vehicle Vehicle { get; set; } = new();
public static RepairRequest? Create(RepairRequestBindingModel? model) public static RepairRequest? Create(CarServiceDbContext context, RepairRequestBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
@ -32,20 +32,18 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
DateCreated = model.DateCreated, DateCreated = model.DateCreated,
VehicleId = model.VehicleId Vehicle = context.Vehicles.First(x => x.Id == model.VehicleId)
}; };
} }
public void Update(RepairRequestBindingModel? model) public void Update(CarServiceDbContext context, RepairRequestBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
return; return;
} }
Id = model.Id;
DateCreated = model.DateCreated; DateCreated = model.DateCreated;
VehicleId = model.VehicleId; Vehicle = context.Vehicles.First(x => x.Id == model.VehicleId);
} }
public RepairRequestViewModel GetViewModel => new() public RepairRequestViewModel GetViewModel => new()
{ {

View File

@ -21,7 +21,7 @@ namespace CarServiceDatabase.Models
[ForeignKey("VehicleId")] [ForeignKey("VehicleId")]
public virtual List<RepairRequest> RepairRequests { get; set; } = new(); public virtual List<RepairRequest> RepairRequests { get; set; } = new();
public virtual Customer Customer { get; set; } = new(); public virtual Customer Customer { get; set; } = new();
public static Vehicle? Create(VehicleBindingModel? model) public static Vehicle? Create(CarServiceDbContext context, VehicleBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
@ -29,24 +29,22 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
Name = model.Name, Name = model.Name,
Plate = model.Plate, Plate = model.Plate,
VIN = model.VIN, VIN = model.VIN,
CustomerId = model.CustomerId Customer = context.Customers.First(x => x.Id == model.CustomerId)
}; };
} }
public void Update(VehicleBindingModel? model) public void Update(CarServiceDbContext context, VehicleBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
return; return;
} }
Id = model.Id;
Name = model.Name; Name = model.Name;
Plate = model.Plate; Plate = model.Plate;
VIN = model.VIN; VIN = model.VIN;
CustomerId = model.CustomerId; Customer = context.Customers.First(x => x.Id == model.CustomerId);
} }
public VehicleViewModel GetViewModel => new() public VehicleViewModel GetViewModel => new()
{ {

View File

@ -31,7 +31,6 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
Name = model.Name, Name = model.Name,
Price = model.Price, Price = model.Price,
Duration = model.Duration, Duration = model.Duration,
@ -44,7 +43,6 @@ namespace CarServiceDatabase.Models
{ {
return; return;
} }
Id = model.Id;
Name = model.Name; Name = model.Name;
Price = model.Price; Price = model.Price;
Duration = model.Duration; Duration = model.Duration;

View File

@ -24,7 +24,7 @@ namespace CarServiceDatabase.Models
public virtual List<WorkPayment> WorkPayments { get; set; } = new(); public virtual List<WorkPayment> WorkPayments { get; set; } = new();
public virtual RepairRequest RepairRequest { get; set; } = new(); public virtual RepairRequest RepairRequest { get; set; } = new();
public virtual Work Work { get; set; } = new(); public virtual Work Work { get; set; } = new();
public static WorkInRequest? Create(WorkInRequestBindingModel? model) public static WorkInRequest? Create(CarServiceDbContext context, WorkInRequestBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
@ -32,24 +32,22 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
Count = model.Count, Count = model.Count,
Cost = model.Cost, Cost = model.Cost,
RepairRequestId = model.RepairRequestId, RepairRequestId = model.RepairRequestId,
WorkId = model.WorkId Work = context.Works.First(x => x.Id == model.WorkId)
}; };
} }
public void Update(WorkInRequestBindingModel? model) public void Update(CarServiceDbContext context, WorkInRequestBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
return; return;
} }
Id = model.Id;
Count = model.Count; Count = model.Count;
Cost = model.Cost; Cost = model.Cost;
RepairRequestId = model.RepairRequestId; RepairRequestId = model.RepairRequestId;
WorkId = model.WorkId; Work = context.Works.First(x => x.Id == model.WorkId);
} }
public WorkInRequestViewModel GetViewModel => new() public WorkInRequestViewModel GetViewModel => new()
{ {

View File

@ -17,7 +17,7 @@ namespace CarServiceDatabase.Models
[Required] [Required]
public int WorkInRequestId { get; private set; } public int WorkInRequestId { get; private set; }
public virtual WorkInRequest WorkInRequest { get; set; } = new(); public virtual WorkInRequest WorkInRequest { get; set; } = new();
public static WorkPayment? Create(WorkPaymentBindingModel? model) public static WorkPayment? Create(CarServiceDbContext context, WorkPaymentBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
@ -25,22 +25,20 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
DatePayment = model.DatePayment, DatePayment = model.DatePayment,
Sum = model.Sum, Sum = model.Sum,
WorkInRequestId = model.WorkInRequestId WorkInRequest = context.WorksInRequest.First(x => x.Id == model.WorkInRequestId)
}; };
} }
public void Update(WorkPaymentBindingModel? model) public void Update(CarServiceDbContext context, WorkPaymentBindingModel? model)
{ {
if (model == null) if (model == null)
{ {
return; return;
} }
Id = model.Id;
DatePayment = model.DatePayment; DatePayment = model.DatePayment;
Sum = model.Sum; Sum = model.Sum;
WorkInRequestId = model.WorkInRequestId; WorkInRequest = context.WorksInRequest.First(x => x.Id == model.WorkInRequestId);
} }
public WorkPaymentViewModel GetViewModel => new() public WorkPaymentViewModel GetViewModel => new()
{ {

View File

@ -35,7 +35,6 @@ namespace CarServiceDatabase.Models
} }
return new() return new()
{ {
Id = model.Id,
Login = model.Login, Login = model.Login,
Password = model.Password, Password = model.Password,
Name = model.Name, Name = model.Name,

View File

@ -31,4 +31,10 @@
<ProjectReference Include="..\CarServiceDatabase\CarServiceDatabase.csproj" /> <ProjectReference Include="..\CarServiceDatabase\CarServiceDatabase.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Update="FormUpdateItemForRepairTest.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
</Project> </Project>

View File

@ -61,6 +61,7 @@
this.updateToolStripMenuItem.Name = "updateToolStripMenuItem"; this.updateToolStripMenuItem.Name = "updateToolStripMenuItem";
this.updateToolStripMenuItem.Size = new System.Drawing.Size(73, 20); this.updateToolStripMenuItem.Size = new System.Drawing.Size(73, 20);
this.updateToolStripMenuItem.Text = "Изменить"; this.updateToolStripMenuItem.Text = "Изменить";
this.updateToolStripMenuItem.Click += new System.EventHandler(this.updateToolStripMenuItem_Click);
// //
// deleteToolStripMenuItem // deleteToolStripMenuItem
// //

View File

@ -33,5 +33,15 @@ namespace CarServiceView
LoadData(); LoadData();
} }
} }
private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormUpdateItemForRepairTest));
if (service is FormUpdateItemForRepairTest form)
{
form.ShowDialog();
LoadData();
}
}
} }
} }

View File

@ -0,0 +1,132 @@
namespace CarServiceView
{
partial class FormUpdateItemForRepairTest
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonUpdate = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBoxCount = new System.Windows.Forms.TextBox();
this.comboBoxRepairRequest = new System.Windows.Forms.ComboBox();
this.comboBoxItem = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// buttonUpdate
//
this.buttonUpdate.Location = new System.Drawing.Point(36, 125);
this.buttonUpdate.Name = "buttonUpdate";
this.buttonUpdate.Size = new System.Drawing.Size(75, 23);
this.buttonUpdate.TabIndex = 9;
this.buttonUpdate.Text = "Обновить";
this.buttonUpdate.UseVisualStyleBackColor = true;
this.buttonUpdate.Click += new System.EventHandler(this.buttonUpdate_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 67);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 15);
this.label3.TabIndex = 6;
this.label3.Text = "Количество";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 38);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 15);
this.label2.TabIndex = 7;
this.label2.Text = "Статья затрат";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(44, 15);
this.label1.TabIndex = 8;
this.label1.Text = "Заявка";
//
// textBoxCount
//
this.textBoxCount.Location = new System.Drawing.Point(110, 64);
this.textBoxCount.Name = "textBoxCount";
this.textBoxCount.Size = new System.Drawing.Size(100, 23);
this.textBoxCount.TabIndex = 3;
//
// comboBoxRepairRequest
//
this.comboBoxRepairRequest.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxRepairRequest.FormattingEnabled = true;
this.comboBoxRepairRequest.Location = new System.Drawing.Point(110, 12);
this.comboBoxRepairRequest.Name = "comboBoxRepairRequest";
this.comboBoxRepairRequest.Size = new System.Drawing.Size(121, 23);
this.comboBoxRepairRequest.TabIndex = 10;
//
// comboBoxItem
//
this.comboBoxItem.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxItem.FormattingEnabled = true;
this.comboBoxItem.Location = new System.Drawing.Point(110, 38);
this.comboBoxItem.Name = "comboBoxItem";
this.comboBoxItem.Size = new System.Drawing.Size(121, 23);
this.comboBoxItem.TabIndex = 10;
//
// FormUpdateItemForRepairTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.comboBoxItem);
this.Controls.Add(this.comboBoxRepairRequest);
this.Controls.Add(this.buttonUpdate);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBoxCount);
this.Name = "FormUpdateItemForRepairTest";
this.Text = "FormAddItemForRepairTest";
this.Load += new System.EventHandler(this.FormAddItemForRepairTest_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Button buttonUpdate;
private Label label3;
private Label label2;
private Label label1;
private TextBox textBoxCount;
private ComboBox comboBoxRepairRequest;
private ComboBox comboBoxItem;
}
}

View File

@ -0,0 +1,55 @@
using CarServiceContracts.BusinessLogicsContracts;
namespace CarServiceView
{
public partial class FormUpdateItemForRepairTest : Form
{
IItemLogic _itemLogic;
IRepairRequestLogic _repairRequestLogic;
IItemForRepairLogic _itemForRepairLogic;
public FormUpdateItemForRepairTest(IItemLogic itemLogic, IRepairRequestLogic repairRequestLogic, IItemForRepairLogic itemForRepairLogic)
{
_itemLogic = itemLogic;
_repairRequestLogic = repairRequestLogic;
_itemForRepairLogic = itemForRepairLogic;
InitializeComponent();
}
private void FormAddItemForRepairTest_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
//загружаем заказы
var listRR = _repairRequestLogic.ReadList(null);
if (listRR != null)
{
comboBoxRepairRequest.DisplayMember = "Id";
comboBoxRepairRequest.ValueMember = "Id";
comboBoxRepairRequest.DataSource = listRR;
comboBoxRepairRequest.SelectedItem = null;
}
//загружаем статьи затрат
var listI = _itemLogic.ReadList(null);
if (listI != null)
{
comboBoxItem.DisplayMember = "Name";
comboBoxItem.ValueMember = "Id";
comboBoxItem.DataSource = listI;
comboBoxItem.SelectedItem = null;
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
_itemForRepairLogic.Update(new()
{
Id = 1,
Count = Convert.ToInt32(textBoxCount.Text),
ItemId = Convert.ToInt32(comboBoxItem.SelectedValue),
RepairRequestId = Convert.ToInt32(comboBoxRepairRequest.SelectedValue)
});
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -19,7 +19,7 @@ namespace CarServiceView
var services = new ServiceCollection(); var services = new ServiceCollection();
ConfigureServices(services); ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider(); _serviceProvider = services.BuildServiceProvider();
Application.Run(_serviceProvider.GetRequiredService<FormItemForRepairTest>()); Application.Run(_serviceProvider.GetRequiredService<FormRegistrationWorker>());
} }
private static void ConfigureServices(ServiceCollection services) private static void ConfigureServices(ServiceCollection services)
{ {
@ -51,6 +51,7 @@ namespace CarServiceView
services.AddTransient<FormAddItemTest>(); services.AddTransient<FormAddItemTest>();
services.AddTransient<FormItemForRepairTest>(); services.AddTransient<FormItemForRepairTest>();
services.AddTransient<FormAddItemForRepairTest>(); services.AddTransient<FormAddItemForRepairTest>();
services.AddTransient<FormUpdateItemForRepairTest>();
} }
} }
} }