This commit is contained in:
devil_1nc 2024-06-24 16:04:56 +04:00
commit 5119290baa
10 changed files with 365 additions and 80 deletions

View File

@ -0,0 +1,21 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contracts.StorageContracts
{
public interface IMediaFileStorage
{
List<MediaFileViewModel> GetFullList();
List<MediaFileViewModel> GetFilteredList(MediaFileSearchModel model);
MediaFileViewModel? GetElement(MediaFileSearchModel model);
MediaFileViewModel? Insert(MediaFileBindingModel model);
MediaFileViewModel? Update(MediaFileBindingModel model);
MediaFileViewModel? Delete(MediaFileBindingModel model);
}
}

View File

@ -14,6 +14,7 @@ namespace Contracts.ViewModels
public string Location { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public Guid ProductId { get; set; }
public string ProductName { get; set; } = string.Empty;
public MediaFileBindingModel GetBindingModel()
{
return new MediaFileBindingModel

View File

@ -0,0 +1,101 @@
using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Implements
{
public class MediaFileStorage : IMediaFileStorage
{
public MediaFileViewModel? Delete(MediaFileBindingModel model)
{
using var context = new Database();
var element = context.MediaFiles
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.MediaFiles.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public MediaFileViewModel? GetElement(MediaFileSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.MediaFiles
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<MediaFileViewModel> GetFilteredList(MediaFileSearchModel model)
{
if (!model.ProductId.HasValue && !model.Id.HasValue)
{
return new();
}
using var context = new Database();
if (model.ProductId.HasValue)
{
return context.MediaFiles.Where(x => x.ProductId == model.ProductId).Select(x => x.GetViewModel).ToList();
}
return context.MediaFiles.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public List<MediaFileViewModel> GetFullList()
{
using var context = new Database();
return context.MediaFiles
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public MediaFileViewModel? Insert(MediaFileBindingModel model)
{
using var context = new Database();
var newProduct = MediaFile.Create(model);
if (newProduct == null)
{
return null;
}
context.MediaFiles.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public MediaFileViewModel? Update(MediaFileBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.MediaFiles.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -18,11 +18,6 @@ namespace DatabaseImplement.Implements
{
using var context = new Database();
var element = context.Products
//.Include(x => x.Name)
//.Include(x => x.Price)
//.Include(x => x.IsBeingSold)
//.Include(x => x.Rate)
//.Include(x => x.Amount)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
@ -41,11 +36,6 @@ namespace DatabaseImplement.Implements
}
using var context = new Database();
return context.Products
//.Include(x => x.Name)
//.Include(x => x.Price)
//.Include(x => x.IsBeingSold)
//.Include(x => x.Rate)
//.Include(x => x.Amount)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
@ -59,11 +49,6 @@ namespace DatabaseImplement.Implements
if (model.Price.HasValue)
{
return context.Products
//.Include(x => x.Name)
//.Include(x => x.Price)
//.Include(x => x.IsBeingSold)
//.Include(x => x.Rate)
//.Include(x => x.Amount)
.Where(x => x.Price <= model.Price)
.ToList()
.Select(x => x.GetViewModel)
@ -72,11 +57,6 @@ namespace DatabaseImplement.Implements
if (model.Rate.HasValue)
{
return context.Products
//.Include(x => x.Name)
//.Include(x => x.Price)
//.Include(x => x.IsBeingSold)
//.Include(x => x.Rate)
//.Include(x => x.Amount)
.Where(x => x.Rate <= model.Rate)
.ToList()
.Select(x => x.GetViewModel)
@ -85,22 +65,12 @@ namespace DatabaseImplement.Implements
if (model.Amount.HasValue && model.IsBeingSold.HasValue)
{
return context.Products
//.Include(x => x.Name)
//.Include(x => x.Price)
//.Include(x => x.IsBeingSold)
//.Include(x => x.Rate)
//.Include(x => x.Amount)
.Where(x => x.IsBeingSold == model.IsBeingSold && x.Amount >= model.Amount)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Products
//.Include(x => x.Name)
//.Include(x => x.Price)
//.Include(x => x.IsBeingSold)
//.Include(x => x.Rate)
//.Include(x => x.Amount)
.Where(x => x.Name == model.Name)
.ToList()
.Select(x => x.GetViewModel)
@ -111,11 +81,6 @@ namespace DatabaseImplement.Implements
{
using var context = new Database();
return context.Products
//.Include(x => x.Name)
//.Include(x => x.Price)
//.Include(x => x.IsBeingSold)
//.Include(x => x.Rate)
//.Include(x => x.Amount)
.ToList()
.Select(x => x.GetViewModel)
.ToList();

View File

@ -21,50 +21,47 @@ namespace DatabaseImplement.Models
[Required]
public Guid ProductId { get; set; }
public MediaFileBindingModel GetBindingModel()
{
return new MediaFileBindingModel
{
Id = Id,
Name = Name,
Location = Location,
ProductId = ProductId
};
}
public virtual Product Product { get; set; }
public static MediaFile ToMediaFileFromView(MediaFileViewModel model, MediaFile mediaFile)
{
return new MediaFile
{
Id = model.Id,
Name = model.Name,
Location = model.Location,
ProductId = model.ProductId
};
}
public static MediaFile? Create(MediaFileBindingModel? model)
{
if (model == null)
{
return null;
}
return new MediaFile
{
Id = model.Id,
Name = model.Name,
Location = model.Location,
ProductId = model.ProductId,
};
}
public static MediaFile ToMediaFileFromBinding(MediaFileBindingModel model, MediaFile mediaFile)
{
return new MediaFile
{
Id = model.Id,
Name = model.Name,
Location = model.Location,
ProductId = model.ProductId
};
}
public void Update(MediaFileBindingModel? model)
{
if (model == null)
{
return;
}
Location = model.Location;
Name = model.Name;
}
public void Update(MediaFileBindingModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
// Обновление свойств на основе модели привязки
Name = model.Name;
Location = model.Location;
ProductId = model.ProductId;
}
}
public MediaFileViewModel GetViewModel
{
get
{
var context = new Database();
return new()
{
Id = Id,
Name = Name,
Location = Location,
ProductId = ProductId,
ProductName = context.Products.FirstOrDefault(x => x.Id == ProductId)?.Name ?? string.Empty,
};
}
}
}
}

58
WinFormsApp/FormMediaFiles.Designer.cs generated Normal file
View File

@ -0,0 +1,58 @@
namespace WinFormsApp
{
partial class FormMediaFiles
{
/// <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()
{
groupBox1 = new GroupBox();
SuspendLayout();
//
// groupBox1
//
groupBox1.Location = new Point(12, 12);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(271, 434);
groupBox1.TabIndex = 0;
groupBox1.TabStop = false;
groupBox1.Text = "groupBox1";
//
// FormMediaFiles
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(groupBox1);
Name = "FormMediaFiles";
Text = "FormMediaFiles";
ResumeLayout(false);
}
#endregion
private GroupBox groupBox1;
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp
{
public partial class FormMediaFiles : Form
{
public FormMediaFiles()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<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

@ -24,6 +24,7 @@ namespace WinFormsApp
private readonly IProductLogic _productLogic;
private readonly BarcodeLogic _barcodeLogic;
private BarcodeResults? _barcode;
List<string> _mediaFiles;
public FormProducts(ILogger<FormMain> logger, IProductLogic productLogic)
{
InitializeComponent();
@ -31,6 +32,7 @@ namespace WinFormsApp
_logger = logger;
_barcodeLogic = new BarcodeLogic();
_barcode = null;
_mediaFiles = new List<string>();
}
private void FormProducts_Load(object sender, EventArgs e)
@ -217,7 +219,7 @@ namespace WinFormsApp
});
groupBoxControls.Hide();
groupBoxCreateProduct.Show();
_logger.LogInformation("Получение товара");
_logger.LogInformation("Получение товара по штрихкоду");
textBoxName.Text = product.Name;
numericUpDownPrice.Value = Convert.ToDecimal(product.Price);
numericUpDownAmount.Value = product.Amount;

View File

@ -38,7 +38,6 @@ namespace WinFormsApp
//services.AddTransient<ISupplyDocStorage, SupplyDocStorage>();
services.AddTransient<IProductStorage, ProductStorage>();
services.AddTransient<ISupplierStorage, SupplierStorage>();
//services.AddTransient<, ImplementerStorage>();
services.AddTransient<ISupplyLogic, SupplyLogic>();
services.AddTransient<ISupplierLogic, SupplierLogic>();
@ -51,6 +50,7 @@ namespace WinFormsApp
services.AddTransient<FormSupply>();
services.AddTransient<FormSupplyProduct>();
services.AddTransient<FormSupplierProduct>();
services.AddTransient<FormMediaFiles>();
}
}