Созданы классы
This commit is contained in:
parent
5d447b65cf
commit
db6fa68cae
16
LabWork/LabWork/ContactBindingModel.cs
Normal file
16
LabWork/LabWork/ContactBindingModel.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
95
LabWork/LabWork/ContactLogic.cs
Normal file
95
LabWork/LabWork/ContactLogic.cs
Normal file
@ -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<ContactViewModel>? 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("Компонент с таким названием уже есть");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
14
LabWork/LabWork/ContactSearchModel.cs
Normal file
14
LabWork/LabWork/ContactSearchModel.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
19
LabWork/LabWork/ContactViewModel.cs
Normal file
19
LabWork/LabWork/ContactViewModel.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
56
LabWork/LabWork/Database/Contact.cs
Normal file
56
LabWork/LabWork/Database/Contact.cs
Normal file
@ -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
|
||||
};
|
||||
}
|
||||
}
|
28
LabWork/LabWork/Database/ContactsDatabase.cs
Normal file
28
LabWork/LabWork/Database/ContactsDatabase.cs
Normal file
@ -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<Contact> Contacts { set; get; }
|
||||
}
|
||||
}
|
81
LabWork/LabWork/Database/ContactsStorage.cs
Normal file
81
LabWork/LabWork/Database/ContactsStorage.cs
Normal file
@ -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<ContactViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ContactsDatabase();
|
||||
return context.Contacts.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<ContactViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
12
LabWork/LabWork/FormContacts.Designer.cs
generated
12
LabWork/LabWork/FormContacts.Designer.cs
generated
@ -5,7 +5,7 @@
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
private System.ContactModel.IContainer Contacts = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
@ -13,9 +13,9 @@
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
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.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ namespace LabWork
|
||||
{
|
||||
public FormContacts()
|
||||
{
|
||||
InitializeComponent();
|
||||
InitializeContact();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
|
17
LabWork/LabWork/Interfaces/IContactLogic.cs
Normal file
17
LabWork/LabWork/Interfaces/IContactLogic.cs
Normal file
@ -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<ContactViewModel>? ReadList(ContactSearchModel? model);
|
||||
ContactViewModel? ReadElement(ContactSearchModel model);
|
||||
bool Create(ContactBindingModel model);
|
||||
bool Update(ContactBindingModel model);
|
||||
bool Delete(ContactBindingModel model);
|
||||
}
|
||||
}
|
15
LabWork/LabWork/Interfaces/IContactModel.cs
Normal file
15
LabWork/LabWork/Interfaces/IContactModel.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
18
LabWork/LabWork/Interfaces/IContactStorage.cs
Normal file
18
LabWork/LabWork/Interfaces/IContactStorage.cs
Normal file
@ -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<ContactViewModel> GetFullList();
|
||||
List<ContactViewModel> GetFilteredList(ContactSearchModel model);
|
||||
ContactViewModel? GetElement(ContactSearchModel model);
|
||||
ContactViewModel? Insert(ContactBindingModel model);
|
||||
ContactViewModel? Update(ContactBindingModel model);
|
||||
ContactViewModel? Delete(ContactBindingModel model);
|
||||
}
|
||||
}
|
@ -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;
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[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<FormContacts>());
|
||||
}
|
||||
private static void ConfigureServices(ServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IContactStorage, ContactStorage>();
|
||||
services.AddTransient<IContactLogic, ContactLogic>();
|
||||
services.AddTransient<FormContact>();
|
||||
services.AddTransient<FormContacts>();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user