Compare commits

...

5 Commits

Author SHA1 Message Date
abazov73
c4a1c3a46d Lab03: pre-migration commit 2023-11-16 20:39:00 +04:00
abazov73
173f299f93 Lab03: create AccountsBusinessLogic project 2023-11-16 19:36:15 +04:00
abazov73
1f2abc1e93 Lab03: spelling fixes 2023-11-16 19:26:25 +04:00
abazov73
bfa38fafb0 Lab03: create AccountContracts project 2023-11-16 19:22:13 +04:00
abazov73
35bc837d82 Lab03: create DataModels project 2023-11-16 19:01:46 +04:00
31 changed files with 1050 additions and 2 deletions

View File

@ -3,9 +3,19 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.3.32825.248 VisualStudioVersion = 17.3.32825.248
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbazovAppView", "AbazovAppView\AbazovAppView.csproj", "{D7EF43DC-2F09-4EAD-8161-6D4A08BC7786}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbazovAppView", "AbazovAppView\AbazovAppView.csproj", "{D7EF43DC-2F09-4EAD-8161-6D4A08BC7786}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbazovViewComponents", "AbazovViewComponents\AbazovViewComponents.csproj", "{070AAA71-3C9B-4E19-A89D-83372C630C6E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbazovViewComponents", "AbazovViewComponents\AbazovViewComponents.csproj", "{070AAA71-3C9B-4E19-A89D-83372C630C6E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsApp", "AccountsApp\AccountsApp.csproj", "{AFCB57A0-4332-4C32-9FB1-60548F7D7276}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsDataModels", "AccountsDataModels\AccountsDataModels.csproj", "{31DEDC68-889D-427B-ADDE-60F2ADE2C596}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsContracts", "AccountsContracts\AccountsContracts.csproj", "{CDA8B86A-9645-4D83-92D0-C8D2D85B734F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsBusinessLogic", "AccountsBusinessLogic\AccountsBusinessLogic.csproj", "{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsDataBaseImplement", "AccountsDataBaseImplement\AccountsDataBaseImplement.csproj", "{ACF95C0D-CEB3-41B9-8B7F-149BEEEE53CE}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,6 +31,26 @@ Global
{070AAA71-3C9B-4E19-A89D-83372C630C6E}.Debug|Any CPU.Build.0 = Debug|Any CPU {070AAA71-3C9B-4E19-A89D-83372C630C6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{070AAA71-3C9B-4E19-A89D-83372C630C6E}.Release|Any CPU.ActiveCfg = Release|Any CPU {070AAA71-3C9B-4E19-A89D-83372C630C6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{070AAA71-3C9B-4E19-A89D-83372C630C6E}.Release|Any CPU.Build.0 = Release|Any CPU {070AAA71-3C9B-4E19-A89D-83372C630C6E}.Release|Any CPU.Build.0 = Release|Any CPU
{AFCB57A0-4332-4C32-9FB1-60548F7D7276}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFCB57A0-4332-4C32-9FB1-60548F7D7276}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFCB57A0-4332-4C32-9FB1-60548F7D7276}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFCB57A0-4332-4C32-9FB1-60548F7D7276}.Release|Any CPU.Build.0 = Release|Any CPU
{31DEDC68-889D-427B-ADDE-60F2ADE2C596}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31DEDC68-889D-427B-ADDE-60F2ADE2C596}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31DEDC68-889D-427B-ADDE-60F2ADE2C596}.Release|Any CPU.ActiveCfg = Release|Any CPU
{31DEDC68-889D-427B-ADDE-60F2ADE2C596}.Release|Any CPU.Build.0 = Release|Any CPU
{CDA8B86A-9645-4D83-92D0-C8D2D85B734F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDA8B86A-9645-4D83-92D0-C8D2D85B734F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDA8B86A-9645-4D83-92D0-C8D2D85B734F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDA8B86A-9645-4D83-92D0-C8D2D85B734F}.Release|Any CPU.Build.0 = Release|Any CPU
{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}.Release|Any CPU.Build.0 = Release|Any CPU
{ACF95C0D-CEB3-41B9-8B7F-149BEEEE53CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACF95C0D-CEB3-41B9-8B7F-149BEEEE53CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACF95C0D-CEB3-41B9-8B7F-149BEEEE53CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACF95C0D-CEB3-41B9-8B7F-149BEEEE53CE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ComponentsLibraryNet60" Version="1.0.0" />
<PackageReference Include="ControlsLibraryNet60" Version="1.0.0" />
<PackageReference Include="NevaevaLibrary" Version="1.0.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,46 @@
namespace AccountsApp
{
partial class FormMain
{
/// <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.SuspendLayout();
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Name = "FormMain";
this.Text = "FormMain";
this.ResumeLayout(false);
}
#endregion
}
}

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 AccountsApp
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
}
}

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

@ -0,0 +1,17 @@
namespace AccountsApp
{
internal static class Program
{
/// <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 FormMain());
}
}
}

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AccountsContracts\AccountsContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,100 @@
using AccountsContracts.BindingModels;
using AccountsContracts.BusinessLogicContracts;
using AccountsContracts.SearchModels;
using AccountsContracts.StorageContracts;
using AccountsContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsBusinessLogic.BusinessLogics
{
public class AccountLogic : IAccountLogic
{
private readonly IAccountStorage _accountStorage;
public AccountLogic(IAccountStorage accountStorage)
{
_accountStorage = accountStorage;
}
public List<AccountViewModel>? ReadList(AccountSearchModel? model)
{
var list = model == null ? _accountStorage.GetFullList() : _accountStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public AccountViewModel? ReadElement(AccountSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _accountStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(AccountBindingModel model)
{
CheckModel(model);
if (_accountStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(AccountBindingModel model)
{
CheckModel(model);
if (_accountStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(AccountBindingModel model)
{
CheckModel(model, false);
if (_accountStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(AccountBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина пользователя", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет электронной почты пользователя", nameof(model.Email));
}
}
}
}

View File

@ -0,0 +1,93 @@
using AccountsContracts.BindingModels;
using AccountsContracts.BusinessLogicContracts;
using AccountsContracts.SearchModels;
using AccountsContracts.StorageContracts;
using AccountsContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AccountsBusinessLogic.BusinessLogics
{
public class InterestLogic : IInterestLogic
{
private readonly IInterestStorage _interestStorage;
public InterestLogic(IInterestStorage interestStorage)
{
_interestStorage = interestStorage;
}
public List<InterestViewModel>? ReadList(InterestSearchModel? model)
{
var list = model == null ? _interestStorage.GetFullList() : _interestStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public InterestViewModel? ReadElement(InterestSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _interestStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(InterestBindingModel model)
{
CheckModel(model);
if (_interestStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(InterestBindingModel model)
{
CheckModel(model);
if (_interestStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(InterestBindingModel model)
{
CheckModel(model, false);
if (_interestStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(InterestBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия интереса", nameof(model.Name));
}
}
}
}

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AccountsDataModels\AccountsDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
using AccountsDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.BindingModels
{
public class AccountBindingModel : IAccountModel
{
public string Login { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public int Id { get; set; }
public Dictionary<int, IInterestModel> AccountInterests
{
get;
set;
} = new();
}
}

View File

@ -0,0 +1,16 @@
using AccountsDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.BindingModels
{
public class InterestBindingModel : IInterestModel
{
public string Name { get; set;} = String.Empty;
public int Id { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using AccountsContracts.BindingModels;
using AccountsContracts.SearchModels;
using AccountsContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.BusinessLogicContracts
{
public interface IAccountLogic
{
List<AccountViewModel>? ReadList(AccountSearchModel? model);
AccountViewModel? ReadElement(AccountSearchModel model);
bool Create(AccountBindingModel model);
bool Update(AccountBindingModel model);
bool Delete(AccountBindingModel model);
}
}

View File

@ -0,0 +1,20 @@
using AccountsContracts.BindingModels;
using AccountsContracts.SearchModels;
using AccountsContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.BusinessLogicContracts
{
public interface IInterestLogic
{
List<InterestViewModel>? ReadList(InterestSearchModel? model);
InterestViewModel? ReadElement(InterestSearchModel model);
bool Create(InterestBindingModel model);
bool Update(InterestBindingModel model);
bool Delete(InterestBindingModel model);
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.SearchModels
{
public class AccountSearchModel
{
public int? Id { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.SearchModels
{
public class InterestSearchModel
{
public int? Id { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using AccountsContracts.BindingModels;
using AccountsContracts.SearchModels;
using AccountsContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.StorageContracts
{
public interface IAccountStorage
{
List<AccountViewModel> GetFullList();
List<AccountViewModel> GetFilteredList(AccountSearchModel model);
AccountViewModel? GetElement(AccountSearchModel model);
AccountViewModel? Insert(AccountBindingModel model);
AccountViewModel? Update(AccountBindingModel model);
AccountViewModel? Delete(AccountBindingModel model);
}
}

View File

@ -0,0 +1,21 @@
using AccountsContracts.BindingModels;
using AccountsContracts.SearchModels;
using AccountsContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.StorageContracts
{
public interface IInterestStorage
{
List<InterestViewModel> GetFullList();
List<InterestViewModel> GetFilteredList(InterestSearchModel model);
InterestViewModel? GetElement(InterestSearchModel model);
InterestViewModel? Insert(InterestBindingModel model);
InterestViewModel? Update(InterestBindingModel model);
InterestViewModel? Delete(InterestBindingModel model);
}
}

View File

@ -0,0 +1,21 @@
using AccountsDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.ViewModels
{
public class AccountViewModel : IAccountModel
{
[DisplayName("Почта")]
public string Login { get; set; } = string.Empty;
[DisplayName("Пароль")]
public string Password { get; set; } = string.Empty;
[DisplayName("Электронная почта")]
public string Email { get; set; } = string.Empty;
public int Id { get; set; }
}
}

View File

@ -0,0 +1,17 @@
using AccountsDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsContracts.ViewModels
{
public class InterestViewModel : IInterestModel
{
[DisplayName("Название")]
public string Name { get; set; } = string.Empty;
public int Id { get; set; }
}
}

View File

@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AccountsContracts\AccountsContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,20 @@
using AccountsDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataBaseImplement
{
public class AccountsDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql("Host=localhost;Database=AccountsDB;Username=postgres;Password=postgres");
public virtual DbSet<Account> Accounts { set; get; }
public virtual DbSet<Interest> Interests { set; get; }
public virtual DbSet<AccountInterest> AccountInterests { set; get; }
}
}

View File

@ -0,0 +1,94 @@
using AccountsContracts.BindingModels;
using AccountsContracts.SearchModels;
using AccountsContracts.StorageContracts;
using AccountsContracts.ViewModels;
using AccountsDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataBaseImplement.Implements
{
public class AccountStorage : IAccountStorage
{
public List<AccountViewModel> GetFullList()
{
using var context = new AccountsDatabase();
return context.Accounts
.Include(x => x.Interests)
.ThenInclude(x => x.Interest)
.Select(x => x.GetViewModel)
.ToList();
}
public List<AccountViewModel> GetFilteredList(AccountSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new AccountsDatabase();
return context.Accounts
.Include(x => x.Interests)
.ThenInclude(x => x.Interest)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public AccountViewModel? GetElement(AccountSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new AccountsDatabase();
return context.Accounts
.Include(x => x.Interests)
.ThenInclude(x => x.Interest)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public AccountViewModel? Insert(AccountBindingModel model)
{
var newAccount = Account.Create(model);
if (newAccount == null)
{
return null;
}
using var context = new AccountsDatabase();
context.Accounts.Add(newAccount);
context.SaveChanges();
return newAccount.GetViewModel;
}
public AccountViewModel? Update(AccountBindingModel model)
{
using var context = new AccountsDatabase();
var account = context.Accounts.FirstOrDefault(x => x.Id == model.Id);
if (account == null)
{
return null;
}
account.Update(model);
context.SaveChanges();
return account.GetViewModel;
}
public AccountViewModel? Delete(AccountBindingModel model)
{
using var context = new AccountsDatabase();
var element = context.Accounts
.Include(x => x.Interests)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Accounts.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,83 @@
using AccountsContracts.BindingModels;
using AccountsContracts.SearchModels;
using AccountsContracts.StorageContracts;
using AccountsContracts.ViewModels;
using AccountsDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataBaseImplement.Implements
{
public class InterestStorage : IInterestStorage
{
public List<InterestViewModel> GetFullList()
{
using var context = new AccountsDatabase();
return context.Interests
.Select(x => x.GetViewModel)
.ToList();
}
public List<InterestViewModel> GetFilteredList(InterestSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new AccountsDatabase();
return context.Interests
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public InterestViewModel? GetElement(InterestSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new AccountsDatabase();
return context.Interests
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public InterestViewModel? Insert(InterestBindingModel model)
{
var newInterest = Interest.Create(model);
if (newInterest == null)
{
return null;
}
using var context = new AccountsDatabase();
context.Interests.Add(newInterest);
context.SaveChanges();
return newInterest.GetViewModel;
}
public InterestViewModel? Update(InterestBindingModel model)
{
using var context = new AccountsDatabase();
var component = context.Interests.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public InterestViewModel? Delete(InterestBindingModel model)
{
using var context = new AccountsDatabase();
var element = context.Interests.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Interests.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,109 @@
using AccountsContracts.BindingModels;
using AccountsContracts.ViewModels;
using AccountsDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataBaseImplement.Models
{
public class Account : IAccountModel
{
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[Required]
public string Email { get; set; } = string.Empty;
public int Id { get; private set; }
private Dictionary<int, IInterestModel>? _accountInterests = null;
[NotMapped]
public Dictionary<int, IInterestModel> AccountInterests
{
get
{
if (_accountInterests == null)
{
_accountInterests = Interests
.ToDictionary(recPI => recPI.InterestId, recPI => recPI.Interest as IInterestModel);
}
return _accountInterests;
}
}
[ForeignKey("AccountId")]
public virtual List<AccountInterest> Interests { get; set; } = new();
public static Account? Create(AccountBindingModel? model)
{
if (model == null)
{
return null;
}
return new Account()
{
Id = model.Id,
Login = model.Login,
Password = model.Password,
Email = model.Email,
};
}
public static Account? Create(AccountViewModel? model)
{
return new Account()
{
Id = model.Id,
Login = model.Login,
Password = model.Password,
Email = model.Email,
};
}
public void Update(AccountBindingModel? model)
{
if (model == null)
{
return;
}
Login = model.Login;
Password = model.Password;
}
public AccountViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
Password = Password,
Email = Email,
};
public void UpdateInterests(AccountsDatabase context, AccountBindingModel model)
{
var accountInterests = context.AccountInterests.Where(rec => rec.AccountId == model.Id).ToList();
if (accountInterests != null && accountInterests.Count > 0)
{
// удалили те, которых нет в модели
context.AccountInterests.RemoveRange(accountInterests.Where(rec => !model.AccountInterests.ContainsKey(rec.InterestId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateInterest in accountInterests)
{
model.AccountInterests.Remove(updateInterest.InterestId);
}
var account = context.Accounts.First(x => x.Id == Id);
foreach (var pc in model.AccountInterests)
{
context.AccountInterests.Add(new AccountInterest
{
Account = account,
Interest = context.Interests.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_accountInterests = null;
}
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataBaseImplement.Models
{
public class AccountInterest
{
public int Id { get; set; }
[Required]
public int AccountId { get; set; }
[Required]
public int InterestId { get; set; }
public virtual Account Account { get; set; } = new();
public virtual Interest Interest { get; set; } = new();
}
}

View File

@ -0,0 +1,53 @@
using AccountsContracts.BindingModels;
using AccountsContracts.ViewModels;
using AccountsDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataBaseImplement.Models
{
public class Interest : IInterestModel
{
public int Id { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
public static Interest? Create(InterestBindingModel? model)
{
if (model == null)
{
return null;
}
return new Interest()
{
Id = model.Id,
Name = model.Name,
};
}
public static Interest? Create(InterestViewModel? model)
{
return new Interest()
{
Id = model.Id,
Name = model.Name,
};
}
public void Update(InterestBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
}
public InterestViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
};
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataModels
{
public interface IId
{
int Id { get; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataModels.Models
{
public interface IAccountModel : IId
{
string Login { get; }
string Password { get; }
string Email { get; }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccountsDataModels.Models
{
public interface IInterestModel : IId
{
string Name { get; }
}
}