Browse Source

Рефакторинг Companies -> Company, addTextToBlock -> updateCompanyGrid

master
Никита 6 years ago
parent
commit
f695dd282e
  1. 6
      db-creation.sql
  2. 12
      eCompanies/App.xaml.cs
  3. 4
      eCompanies/Companies.cs
  4. 6
      eCompanies/CompaniesDBContext.cs
  5. 4
      eCompanies/Contract.cs
  6. 6
      eCompanies/MainWindow.xaml.cs
  7. 7
      eCompanies/Users.cs
  8. 12
      eCompanies/UsersWindow.xaml.cs

6
db-creation.sql

@ -4,7 +4,7 @@ GO
USE [CompaniesDB]; USE [CompaniesDB];
GO GO
CREATE TABLE [Companies] ( CREATE TABLE [Company] (
[CompanyId] int NOT NULL IDENTITY, [CompanyId] int NOT NULL IDENTITY,
[Name] nvarchar(max) NOT NULL, [Name] nvarchar(max) NOT NULL,
[ContractStatus] int NOT NULL CHECK ([ContractStatus] IN(0, 1, 2)), [ContractStatus] int NOT NULL CHECK ([ContractStatus] IN(0, 1, 2)),
@ -19,11 +19,11 @@ CREATE TABLE [Users] (
[Login] nvarchar(max) NOT NULL, [Login] nvarchar(max) NOT NULL,
[Password] nvarchar(max) NOT NULL, [Password] nvarchar(max) NOT NULL,
CONSTRAINT [PK_UserId] PRIMARY KEY ([UserId]), CONSTRAINT [PK_UserId] PRIMARY KEY ([UserId]),
CONSTRAINT [FK_Users_Companies] FOREIGN KEY (CompanyID) REFERENCES [Companies] (CompanyId) ON DELETE CASCADE CONSTRAINT [FK_Users_Company] FOREIGN KEY (CompanyID) REFERENCES [Company] (CompanyId) ON DELETE CASCADE
); );
GO GO
INSERT INTO [Companies] (Name, ContractStatus) VALUES INSERT INTO [Company] (Name, ContractStatus) VALUES
('Defend Studio', '0'), ('Defend Studio', '0'),
('Yandex Music', '1') ('Yandex Music', '1')
GO GO

12
eCompanies/App.xaml.cs

@ -11,19 +11,19 @@ namespace eCompanies
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
private void addCompany(Companies c) private void addCompany(Company c)
{ {
using (var db = new CompaniesDBContext()) using (var db = new CompaniesDBContext())
{ {
db.Companies.Add(c); db.Company.Add(c);
var count = db.SaveChanges(); var count = db.SaveChanges();
} }
} }
public static ObservableCollection<Companies> getCompanies() public static ObservableCollection<Company> getCompanies()
{ {
using (var db = new CompaniesDBContext()) using (var db = new CompaniesDBContext())
return new ObservableCollection<Companies>(db.Companies.AsEnumerable()); return new ObservableCollection<Company>(db.Company.AsEnumerable());
} }
public static HashSet<Users> getUsers(int id) public static HashSet<Users> getUsers(int id)
@ -32,11 +32,11 @@ namespace eCompanies
return new HashSet<Users>(db.Users.Where(b => b.CompanyId.Equals(id))); return new HashSet<Users>(db.Users.Where(b => b.CompanyId.Equals(id)));
} }
public static void updateCompany(Companies c) public static void updateCompany(Company c)
{ {
using (var db = new CompaniesDBContext()) using (var db = new CompaniesDBContext())
{ {
db.Companies.Update(c); db.Company.Update(c);
db.SaveChanges(); db.SaveChanges();
} }
} }

4
eCompanies/Companies.cs

@ -3,9 +3,9 @@ using System.Collections.Generic;
namespace eCompanies namespace eCompanies
{ {
public partial class Companies public partial class Company
{ {
public Companies() public Company()
{ {
Users = new HashSet<Users>(); Users = new HashSet<Users>();
contract = new Contract(this); contract = new Contract(this);

6
eCompanies/CompaniesDBContext.cs

@ -15,7 +15,7 @@ namespace eCompanies
{ {
} }
public virtual DbSet<Companies> Companies { get; set; } public virtual DbSet<Company> Company { get; set; }
public virtual DbSet<Users> Users { get; set; } public virtual DbSet<Users> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
@ -28,7 +28,7 @@ namespace eCompanies
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.Entity<Companies>(entity => modelBuilder.Entity<Company>(entity =>
{ {
entity.HasKey(e => e.CompanyId); entity.HasKey(e => e.CompanyId);
@ -55,7 +55,7 @@ namespace eCompanies
entity.HasOne(d => d.Company) entity.HasOne(d => d.Company)
.WithMany(p => p.Users) .WithMany(p => p.Users)
.HasForeignKey(d => d.CompanyId) .HasForeignKey(d => d.CompanyId)
.HasConstraintName("FK_Users_Companies"); .HasConstraintName("FK_Users_Company");
}); });
} }
} }

4
eCompanies/Contract.cs

@ -15,8 +15,8 @@ namespace eCompanies
public class Contract public class Contract
{ {
private Companies c; private Company c;
public Contract (Companies c) public Contract (Company c)
{ {
this.c = c; this.c = c;
} }

6
eCompanies/MainWindow.xaml.cs

@ -25,10 +25,10 @@ namespace eCompanies
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
addTextToBlock(); updateCompanyGrid();
} }
private void addTextToBlock() private void updateCompanyGrid()
{ {
companyGrid.DataContext = App.getCompanies(); companyGrid.DataContext = App.getCompanies();
} }
@ -37,7 +37,7 @@ namespace eCompanies
{ {
try try
{ {
Companies company = (Companies)companyGrid.CurrentItem; Company company = (Company)companyGrid.CurrentItem;
if (usersWindowInstance != null) usersWindowInstance.Close(); if (usersWindowInstance != null) usersWindowInstance.Close();
usersWindowInstance = new UsersWindow(company); usersWindowInstance = new UsersWindow(company);
usersWindowInstance.Show(); usersWindowInstance.Show();

7
eCompanies/Users.cs

@ -1,7 +1,4 @@
using System; namespace eCompanies
using System.Collections.Generic;
namespace eCompanies
{ {
public partial class Users public partial class Users
{ {
@ -11,6 +8,6 @@ namespace eCompanies
public string Login { get; set; } public string Login { get; set; }
public string Password { get; set; } public string Password { get; set; }
public Companies Company { get; set; } public Company Company { get; set; }
} }
} }

12
eCompanies/UsersWindow.xaml.cs

@ -1,17 +1,9 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace eCompanies namespace eCompanies
{ {
@ -20,8 +12,8 @@ namespace eCompanies
/// </summary> /// </summary>
public partial class UsersWindow : Window public partial class UsersWindow : Window
{ {
Companies currentCompany; Company currentCompany;
public UsersWindow(Companies c) public UsersWindow(Company c)
{ {
InitializeComponent(); InitializeComponent();
currentCompany = c; currentCompany = c;

Loading…
Cancel
Save