Lab03: pre-migration commit

This commit is contained in:
abazov73 2023-11-16 20:39:00 +04:00
parent 173f299f93
commit c4a1c3a46d
14 changed files with 573 additions and 0 deletions

View File

@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsContracts", "Accoun
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsBusinessLogic", "AccountsBusinessLogic\AccountsBusinessLogic.csproj", "{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsBusinessLogic", "AccountsBusinessLogic\AccountsBusinessLogic.csproj", "{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AccountsDataBaseImplement", "AccountsDataBaseImplement\AccountsDataBaseImplement.csproj", "{ACF95C0D-CEB3-41B9-8B7F-149BEEEE53CE}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -45,6 +47,10 @@ Global
{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}.Debug|Any CPU.Build.0 = 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.ActiveCfg = Release|Any CPU
{D98A5D9F-A491-4A4B-A4A3-B1F388BAD18D}.Release|Any CPU.Build.0 = 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

@ -16,5 +16,11 @@ namespace AccountsContracts.BindingModels
public string Email { get; set; } = string.Empty; public string Email { get; set; } = string.Empty;
public int Id { get; set; } public int Id { get; set; }
public Dictionary<int, IInterestModel> AccountInterests
{
get;
set;
} = new();
} }
} }

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,
};
}
}