diff --git a/LabWork/LabWork/ContactBindingModel.cs b/LabWork/LabWork/ContactBindingModel.cs new file mode 100644 index 0000000..45e6536 --- /dev/null +++ b/LabWork/LabWork/ContactBindingModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LabWork.Interfaces; + +namespace LabWork +{ + public class ContactBindingModel : IContactModel + { + public int Id { get; set; } + public string ContactName { get; set; } = string.Empty; + public double Number { get; set; } + } +} diff --git a/LabWork/LabWork/ContactLogic.cs b/LabWork/LabWork/ContactLogic.cs new file mode 100644 index 0000000..2462395 --- /dev/null +++ b/LabWork/LabWork/ContactLogic.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LabWork.Interfaces; + +namespace LabWork +{ + public class ContactLogic : IContactLogic + { + private readonly IContactStorage _ContactStorage; + public ContactLogic(IContactStorage ContactStorage) + { + _ContactStorage = ContactStorage; + } + public List? ReadList(ContactSearchModel? model) + { + var list = model == null ? _ContactStorage.GetFullList() : _ContactStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + public ContactViewModel? ReadElement(ContactSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _ContactStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + public bool Create(ContactBindingModel model) + { + CheckModel(model); + if (_ContactStorage.Insert(model) == null) + { + return false; + } + return true; + } + public bool Update(ContactBindingModel model) + { + CheckModel(model); + if (_ContactStorage.Update(model) == null) + { + return false; + } + return true; + } + public bool Delete(ContactBindingModel model) + { + CheckModel(model, false); + if (_ContactStorage.Delete(model) == null) + { + return false; + } + return true; + } + private void CheckModel(ContactBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ContactName)) + { + throw new ArgumentNullException("Нет названия компонента", + nameof(model.ContactName)); + } + if (model.Number <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Number)); + } + var element = _ContactStorage.GetElement(new ContactSearchModel + { + ContactName = model.ContactName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} diff --git a/LabWork/LabWork/ContactSearchModel.cs b/LabWork/LabWork/ContactSearchModel.cs new file mode 100644 index 0000000..27842f9 --- /dev/null +++ b/LabWork/LabWork/ContactSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LabWork +{ + public class ContactSearchModel + { + public int? Id { get; set; } + public string? ContactName { get; set; } + } +} diff --git a/LabWork/LabWork/ContactViewModel.cs b/LabWork/LabWork/ContactViewModel.cs new file mode 100644 index 0000000..732c716 --- /dev/null +++ b/LabWork/LabWork/ContactViewModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.ContactModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LabWork.Interfaces; + +namespace LabWork +{ + public class ContactViewModel + { + public int Id { get; set; } + [DisplayName("Название компонента")] + public string ContactName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Number { get; set; } + } +} diff --git a/LabWork/LabWork/Database/Contact.cs b/LabWork/LabWork/Database/Contact.cs new file mode 100644 index 0000000..5b9f52b --- /dev/null +++ b/LabWork/LabWork/Database/Contact.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LabWork.Interfaces; +using System.ContactModel.DataAnnotations; + +namespace LabWork.Database +{ + public class Contact : IContactModel + { + public int Id { get; private set; } + [Required] + public string ContactName { get; private set; } = string.Empty; + [Required] + public double Number { get; set; } + public static Contact? Create(ContactBindingModel model) + { + if (model == null) + { + return null; + } + return new Contact() + { + Id = model.Id, + ContactName = model.ContactName, + Number = model.Number + }; + } + public static Contact Create(ContactViewModel model) + { + return new Contact + { + Id = model.Id, + ContactName = model.ContactName, + Number = model.Number + }; + } + public void Update(ContactBindingModel model) + { + if (model == null) + { + return; + } + ContactName = model.ContactName; + Number = model.Number; + } + public ContactViewModel GetViewModel => new() + { + Id = Id, + ContactName = ContactName, + Number = Number + }; + } +} diff --git a/LabWork/LabWork/Database/ContactsDatabase.cs b/LabWork/LabWork/Database/ContactsDatabase.cs new file mode 100644 index 0000000..c5f0013 --- /dev/null +++ b/LabWork/LabWork/Database/ContactsDatabase.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ContactModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LabWork.Database +{ + public class ContactsDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder) + { + if (OptionsBuilder.IsConfigured == false) + { + OptionsBuilder.UseNpgsql(@"Host=localhost;Database=Contacts;Username=postgres;Password=postgres"); + } + + base.OnConfiguring(OptionsBuilder); + + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + + public virtual DbSet Contacts { set; get; } + } +} diff --git a/LabWork/LabWork/Database/ContactsStorage.cs b/LabWork/LabWork/Database/ContactsStorage.cs new file mode 100644 index 0000000..c60d950 --- /dev/null +++ b/LabWork/LabWork/Database/ContactsStorage.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using LabWork.Interfaces; +using System.ContactModel; + +namespace LabWork.Database +{ + public class ContactStorage : IContactStorage + { + public List GetFullList() + { + using var context = new ContactsDatabase(); + return context.Contacts.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ContactSearchModel model) + { + if (string.IsNullOrEmpty(model.ContactName)) + { + return new(); + } + using var context = new ContactsDatabase(); + return context.Contacts.Where(x => x.ContactName.Contains(model.ContactName)).Select(x => x.GetViewModel).ToList(); + } + + public ContactViewModel? GetElement(ContactSearchModel model) + { + if (string.IsNullOrEmpty(model.ContactName) && !model.Id.HasValue) + { + return null; + } + using var context = new ContactsDatabase(); + return context.Contacts.FirstOrDefault(x => + !string.IsNullOrEmpty(model.ContactName) && x.ContactName == model.ContactName || + model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public ContactViewModel? Insert(ContactBindingModel model) + { + var newContact = Contact.Create(model); + if (newContact == null) + { + return null; + } + using var context = new ContactsDatabase(); + context.Contacts.Add(newContact); + context.SaveChanges(); + return newContact.GetViewModel; + } + + public ContactViewModel? Update(ContactBindingModel model) + { + using var context = new ContactsDatabase(); + var Contact = context.Contacts.FirstOrDefault(x => x.Id == model.Id); + if (Contact == null) + { + return null; + } + Contact.Update(model); + context.SaveChanges(); + return Contact.GetViewModel; + } + + public ContactViewModel? Delete(ContactBindingModel model) + { + using var context = new ContactsDatabase(); + var element = context.Contacts.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Contacts.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/LabWork/LabWork/FormContacts.Designer.cs b/LabWork/LabWork/FormContacts.Designer.cs index abd0328..efbd17c 100644 --- a/LabWork/LabWork/FormContacts.Designer.cs +++ b/LabWork/LabWork/FormContacts.Designer.cs @@ -5,7 +5,7 @@ /// /// Required designer variable. /// - private System.ComponentModel.IContainer components = null; + private System.ContactModel.IContainer Contacts = null; /// /// Clean up any resources being used. @@ -13,9 +13,9 @@ /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { - if (disposing && (components != null)) + if (disposing && (Contacts != null)) { - components.Dispose(); + Contacts.Dispose(); } base.Dispose(disposing); } @@ -26,14 +26,14 @@ /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// - private void InitializeComponent() + private void InitializeContact() { dataGridView = new DataGridView(); buttonAdd = new Button(); buttonUpd = new Button(); buttonRef = new Button(); buttonDel = new Button(); - ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ContactModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // dataGridView @@ -95,7 +95,7 @@ Controls.Add(dataGridView); Name = "FormContacts"; Text = "Менеджер контактов"; - ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ContactModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); } diff --git a/LabWork/LabWork/FormContacts.cs b/LabWork/LabWork/FormContacts.cs index 27039b4..3eef2cb 100644 --- a/LabWork/LabWork/FormContacts.cs +++ b/LabWork/LabWork/FormContacts.cs @@ -4,7 +4,7 @@ namespace LabWork { public FormContacts() { - InitializeComponent(); + InitializeContact(); } } } diff --git a/LabWork/LabWork/FormContacts.resx b/LabWork/LabWork/FormContacts.resx index af32865..f7ea5a3 100644 --- a/LabWork/LabWork/FormContacts.resx +++ b/LabWork/LabWork/FormContacts.resx @@ -56,7 +56,7 @@ mimetype: application/x-microsoft.net.object.bytearray.base64 value : The object must be serialized into a byte array - : using a System.ComponentModel.TypeConverter + : using a System.ContactModel.TypeConverter : and then encoded with base64 encoding. --> diff --git a/LabWork/LabWork/Interfaces/IContactLogic.cs b/LabWork/LabWork/Interfaces/IContactLogic.cs new file mode 100644 index 0000000..66d0563 --- /dev/null +++ b/LabWork/LabWork/Interfaces/IContactLogic.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LabWork.Interfaces +{ + public interface IContactLogic + { + List? ReadList(ContactSearchModel? model); + ContactViewModel? ReadElement(ContactSearchModel model); + bool Create(ContactBindingModel model); + bool Update(ContactBindingModel model); + bool Delete(ContactBindingModel model); + } +} diff --git a/LabWork/LabWork/Interfaces/IContactModel.cs b/LabWork/LabWork/Interfaces/IContactModel.cs new file mode 100644 index 0000000..3b796e7 --- /dev/null +++ b/LabWork/LabWork/Interfaces/IContactModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LabWork.Interfaces +{ + public interface IContactModel + { + int Id { get; } + string ContactName { get; } + double Number { get; } + } +} diff --git a/LabWork/LabWork/Interfaces/IContactStorage.cs b/LabWork/LabWork/Interfaces/IContactStorage.cs new file mode 100644 index 0000000..6b1ef11 --- /dev/null +++ b/LabWork/LabWork/Interfaces/IContactStorage.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LabWork.Interfaces +{ + public interface IContactStorage + { + List GetFullList(); + List GetFilteredList(ContactSearchModel model); + ContactViewModel? GetElement(ContactSearchModel model); + ContactViewModel? Insert(ContactBindingModel model); + ContactViewModel? Update(ContactBindingModel model); + ContactViewModel? Delete(ContactBindingModel model); + } +} diff --git a/LabWork/LabWork/Program.cs b/LabWork/LabWork/Program.cs index 5d93bba..b4f749f 100644 --- a/LabWork/LabWork/Program.cs +++ b/LabWork/LabWork/Program.cs @@ -1,17 +1,32 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using LabWork.Interfaces; +using LabWork.Database; + namespace LabWork { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// [STAThread] static void Main() { - // To customize application configuration such as set high DPI settings or default font, - // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormContacts()); + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + Application.Run(_serviceProvider.GetRequiredService()); + } + private static void ConfigureServices(ServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file