diff --git a/Contracts/StorageContracts/IMediaFileStorage.cs b/Contracts/StorageContracts/IMediaFileStorage.cs new file mode 100644 index 0000000..f637aab --- /dev/null +++ b/Contracts/StorageContracts/IMediaFileStorage.cs @@ -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 GetFullList(); + List GetFilteredList(MediaFileSearchModel model); + MediaFileViewModel? GetElement(MediaFileSearchModel model); + MediaFileViewModel? Insert(MediaFileBindingModel model); + MediaFileViewModel? Update(MediaFileBindingModel model); + MediaFileViewModel? Delete(MediaFileBindingModel model); + } +} diff --git a/Contracts/ViewModels/MediaFileViewModel.cs b/Contracts/ViewModels/MediaFileViewModel.cs index 6acc484..c2f25bd 100644 --- a/Contracts/ViewModels/MediaFileViewModel.cs +++ b/Contracts/ViewModels/MediaFileViewModel.cs @@ -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 diff --git a/DatabaseImplement/Implements/MediaFileStorage.cs b/DatabaseImplement/Implements/MediaFileStorage.cs new file mode 100644 index 0000000..16e2921 --- /dev/null +++ b/DatabaseImplement/Implements/MediaFileStorage.cs @@ -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 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 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; + } + } + } +} diff --git a/DatabaseImplement/Implements/ProductStorage.cs b/DatabaseImplement/Implements/ProductStorage.cs index 415eaed..d3de1d9 100644 --- a/DatabaseImplement/Implements/ProductStorage.cs +++ b/DatabaseImplement/Implements/ProductStorage.cs @@ -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(); diff --git a/DatabaseImplement/Models/MediaFile.cs b/DatabaseImplement/Models/MediaFile.cs index bc3c9ec..829e792 100644 --- a/DatabaseImplement/Models/MediaFile.cs +++ b/DatabaseImplement/Models/MediaFile.cs @@ -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, + }; + } + } + } } diff --git a/WinFormsApp/FormMediaFiles.Designer.cs b/WinFormsApp/FormMediaFiles.Designer.cs new file mode 100644 index 0000000..730d963 --- /dev/null +++ b/WinFormsApp/FormMediaFiles.Designer.cs @@ -0,0 +1,58 @@ +namespace WinFormsApp +{ + partial class FormMediaFiles + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + 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; + } +} \ No newline at end of file diff --git a/WinFormsApp/FormMediaFiles.cs b/WinFormsApp/FormMediaFiles.cs new file mode 100644 index 0000000..47132d4 --- /dev/null +++ b/WinFormsApp/FormMediaFiles.cs @@ -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(); + } + } +} diff --git a/WinFormsApp/FormMediaFiles.resx b/WinFormsApp/FormMediaFiles.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/WinFormsApp/FormMediaFiles.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/WinFormsApp/FormProducts.cs b/WinFormsApp/FormProducts.cs index 7398d23..bad2a98 100644 --- a/WinFormsApp/FormProducts.cs +++ b/WinFormsApp/FormProducts.cs @@ -24,6 +24,7 @@ namespace WinFormsApp private readonly IProductLogic _productLogic; private readonly BarcodeLogic _barcodeLogic; private BarcodeResults? _barcode; + List _mediaFiles; public FormProducts(ILogger logger, IProductLogic productLogic) { InitializeComponent(); @@ -31,6 +32,7 @@ namespace WinFormsApp _logger = logger; _barcodeLogic = new BarcodeLogic(); _barcode = null; + _mediaFiles = new List(); } 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; diff --git a/WinFormsApp/Program.cs b/WinFormsApp/Program.cs index 1bc3510..6b43ae2 100644 --- a/WinFormsApp/Program.cs +++ b/WinFormsApp/Program.cs @@ -38,7 +38,6 @@ namespace WinFormsApp //services.AddTransient(); services.AddTransient(); services.AddTransient(); - //services.AddTransient<, ImplementerStorage>(); services.AddTransient(); services.AddTransient(); @@ -51,6 +50,7 @@ namespace WinFormsApp services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } }