From 0fb8aaed4b3d509613eb46b25f28c2b59e55d35d Mon Sep 17 00:00:00 2001 From: bocchanskyy Date: Mon, 20 May 2024 01:14:24 +0400 Subject: [PATCH] Database basics done --- PersonnelDepartment/PersonnelDepartment.sln | 6 + .../Implements/BackUpInfo.cs | 32 +++ .../Implements/DealStorage.cs | 223 ++++++++++++++++++ .../Implements/DepartmentStorage.cs | 88 +++++++ .../Implements/EmployeeStorage.cs | 89 +++++++ .../Implements/PositionStorage.cs | 85 +++++++ .../Implements/TypeStorage.cs | 86 +++++++ .../Models/Deal.cs | 75 ++++++ .../Models/Department.cs | 52 ++++ .../Models/Employee.cs | 56 +++++ .../Models/Position.cs | 47 ++++ .../Models/Type.cs | 47 ++++ .../PersonnelDepartmentDatabase.cs | 29 +++ ...ersonnelDepartmentDatabaseImplement.csproj | 9 + 14 files changed, 924 insertions(+) create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/BackUpInfo.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/DealStorage.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/DepartmentStorage.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/EmployeeStorage.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/PositionStorage.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/TypeStorage.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Deal.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Department.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Employee.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Position.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Type.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/PersonnelDepartmentDatabase.cs create mode 100644 PersonnelDepartment/PersonnelDepartmentDatabaseImplement/PersonnelDepartmentDatabaseImplement.csproj diff --git a/PersonnelDepartment/PersonnelDepartment.sln b/PersonnelDepartment/PersonnelDepartment.sln index 7d25452..967cd84 100644 --- a/PersonnelDepartment/PersonnelDepartment.sln +++ b/PersonnelDepartment/PersonnelDepartment.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersonnelDepartmentContract EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersonnelDepartmentBusinessLogic", "PersonnelDepartmentBusinessLogic\PersonnelDepartmentBusinessLogic.csproj", "{068D2B5D-C715-428B-A77D-A9D1E8849762}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersonnelDepartmentDatabaseImplement", "PersonnelDepartmentDatabaseImplement\PersonnelDepartmentDatabaseImplement.csproj", "{B0B74022-23FC-4AE0-98F2-7F8171ACA715}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {068D2B5D-C715-428B-A77D-A9D1E8849762}.Debug|Any CPU.Build.0 = Debug|Any CPU {068D2B5D-C715-428B-A77D-A9D1E8849762}.Release|Any CPU.ActiveCfg = Release|Any CPU {068D2B5D-C715-428B-A77D-A9D1E8849762}.Release|Any CPU.Build.0 = Release|Any CPU + {B0B74022-23FC-4AE0-98F2-7F8171ACA715}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B0B74022-23FC-4AE0-98F2-7F8171ACA715}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B0B74022-23FC-4AE0-98F2-7F8171ACA715}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B0B74022-23FC-4AE0-98F2-7F8171ACA715}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/BackUpInfo.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/BackUpInfo.cs new file mode 100644 index 0000000..abc04ab --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/BackUpInfo.cs @@ -0,0 +1,32 @@ +using PersonnelDepartmentContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Implements +{ + public class BackUpInfo : IBackUpInfo + { + public List? GetList() where T : class, new() + { + using var context = new PersonnelDepartmentDatabase(); + return context.Set().ToList(); + } + public Type? GetTypeByModelInterface(string modelInterfaceName) + { + var assembly = typeof(BackUpInfo).Assembly; + var types = assembly.GetTypes(); + foreach (var type in types) + { + if (type.IsClass && + type.GetInterface(modelInterfaceName) != null) + { + return type; + } + } + return null; + } + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/DealStorage.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/DealStorage.cs new file mode 100644 index 0000000..927206f --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/DealStorage.cs @@ -0,0 +1,223 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.SearchModels; +using PersonnelDepartmentContracts.StoragesContracts; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Implements +{ + public class DealStorage : IDealStorage + { + public bool ClearList() + { + try + { + using var context = new PersonnelDepartmentDatabase(); + var tableName = context.Model.FindEntityType(typeof(Deal)).GetTableName(); + context.Database.ExecuteSqlRaw($"DELETE FROM \"{ tableName }\""); + } + catch (Exception) + { + return false; + } + return true; + } + + public DealViewModel? Delete(DealBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Deals + .Include(x => x.Employee) + .Include(x => x.Department) + .Include(x => x.Type) + .Include(x => x.Position) + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + context.Deals.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + public string DiffGetTest(int count) + { + using var context = new PersonnelDepartmentDatabase(); + Stopwatch stopwatch = new(); + stopwatch.Start(); + var list = context.Deals + .Include(x => x.Department) + .Include(x => x.Employee) + .Include(x => x.Type) + .Include(x => x.Position) + .Join(context.Departments, + deal => deal.DepartmentId, + department => department.Id, + (deal, department) => new { Deal = deal, Department = department }) + .Join(context.Employees, + deal => deal.Deal.EmployeeId, + employee => employee.Id, + (deal, employee) => new { deal.Deal, deal.Department, Employee = employee }) + .Join(context.Positions, + deal => deal.Deal.PositionId, + position => position.Id, + (deal, position) => new { deal.Deal, deal.Department, deal.Employee, Position = position }) + .Join(context.Types, + deal => deal.Deal.TypeId, + type => type.Id, + (deal, type) => new { deal.Deal, deal.Department, deal.Employee, deal.Position, Type = type }) + .Select(deal => new + { + Deal = deal.Deal.GetViewModel, + Department = deal.Department.GetViewModel, + Type = deal.Type.GetViewModel, + Position = deal.Position.GetViewModel + }) + .Take(count) + .ToList(); + stopwatch.Stop(); + return stopwatch.ElapsedMilliseconds.ToString(); + } + + public DealViewModel? GetElement(DealSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + return context.Deals + .Include(x => x.Employee) + .Include(x => x.Department) + .Include(x => x.Type) + .Include(x => x.Position) + .FirstOrDefault(x => (model.Id.HasValue && model.Id == x.Id) || + (model.DateFrom != null && x.DateFrom == model.DateFrom) || + (model.DateTo != null && x.DateTo == model.DateTo) || + (model.EmployeeId.HasValue && x.EmployeeId == model.EmployeeId) || + (model.DepartmentId.HasValue && x.DepartmentId == model.DepartmentId) || + (model.PositionId.HasValue && x.PositionId == model.PositionId) || + (model.TypeId.HasValue && x.TypeId == model.TypeId))?.GetViewModel; + } + + public List GetFilteredList(DealSearchModel model) + { + using var context = new PersonnelDepartmentDatabase(); + return context.Deals + .Include(x => x.Employee) + .Include(x => x.Department) + .Include(x => x.Type) + .Include(x => x.Position) + .Where(x => (model.Id.HasValue && model.Id == x.Id) || + (model.DateFrom != null && x.DateFrom == model.DateFrom) || + (model.DateTo != null && x.DateTo == model.DateTo) || + (model.EmployeeId.HasValue && x.EmployeeId == model.EmployeeId) || + (model.DepartmentId.HasValue && x.DepartmentId == model.DepartmentId) || + (model.PositionId.HasValue && x.PositionId == model.PositionId) || + (model.TypeId.HasValue && x.TypeId == model.TypeId)) + .Select(x => x.GetViewModel) .ToList(); + } + + public List GetFullList() + { + using var context = new PersonnelDepartmentDatabase(); + return context.Deals + .Include(x => x.Department) + .Include(x => x.Position) + .Include(x => x.Employee) + .Include(x => x.Type) + .Select(x => x.GetViewModel) + .ToList(); + } + + public string GetTest(int count) + { + using var context = new PersonnelDepartmentDatabase(); + Stopwatch stopwatch = new(); + stopwatch.Start(); + var list = context.Deals + .Include(x => x.Department) + .Include(x => x.Employee) + .Include(x => x.Type) + .Include(x => x.Position) + .Take(count) + .Select(x => x.GetViewModel) + .ToList(); + stopwatch.Stop(); + return stopwatch.ElapsedMilliseconds.ToString(); + } + + public DealViewModel? Insert(DealBindingModel model) + { + var newElement = Deal.Create(model); + if (newElement == null) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + context.Deals.Add(newElement); + context.SaveChanges(); + return GetElement(new DealSearchModel { Id = newElement.Id } ); + } + + public string SetTest(int count) + { + Random rnd = new Random(); + using var context = new PersonnelDepartmentDatabase(); + var listDepartments = context.Departments.Select(x => x.GetViewModel).ToList(); + var listEmployees = context.Employees.Select(x => x.GetViewModel).ToList(); + var listPositions = context.Positions.Select(x => x.GetViewModel).ToList(); + var listTypes = context.Types.Select(x => x.GetViewModel).ToList(); + if (listDepartments.Count < 1 || listEmployees.Count < 1 || listPositions.Count < 1 || listTypes.Count < 1) + { + throw new Exception("Недостаточно для генерации!"); + } + for (int i = 0; i < count; ++i) + { + context.Deals.Add(Deal.Create(new DealBindingModel + { + Id = 0, + DateFrom = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), + DateTo = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), + DepartmentId = listDepartments[rnd.Next(listDepartments.Count)].Id, + EmployeeId = listEmployees[rnd.Next(listEmployees.Count)].Id, + TypeId = listTypes[rnd.Next(listTypes.Count)].Id, + PositionId = listPositions[rnd.Next(listPositions.Count)].Id + })); + } + Stopwatch stopwatch = new(); + stopwatch.Start(); + context.SaveChanges(); + stopwatch.Stop(); + return stopwatch.ElapsedMilliseconds.ToString(); + } + + public DealViewModel? Update(DealBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Deals + .Include(x => x.Department) + .Include(x => x.Employee) + .Include(x => x.Type) + .Include (x => x.Position) + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return GetElement(new DealSearchModel { Id = element.Id }); + } + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/DepartmentStorage.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/DepartmentStorage.cs new file mode 100644 index 0000000..c971ee8 --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/DepartmentStorage.cs @@ -0,0 +1,88 @@ +using Microsoft.EntityFrameworkCore; +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.SearchModels; +using PersonnelDepartmentContracts.StoragesContracts; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Implements +{ + public class DepartmentStorage : IDepartmentStorage + { + public DepartmentViewModel? Delete(DepartmentBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Departments + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + context.Departments.Remove(element); + return element.GetViewModel; + } + + public DepartmentViewModel? GetElement(DepartmentSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + return context.Departments + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Telephone.HasValue && x.Telephone == model.Telephone))?.GetViewModel; + } + + public List GetFilteredList(DepartmentSearchModel model) + { + using var context = new PersonnelDepartmentDatabase(); + return context.Departments + .Where(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Telephone.HasValue && x.Telephone == model.Telephone)) + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new PersonnelDepartmentDatabase(); + return context.Departments + .Select(x => x.GetViewModel) + .ToList(); + } + + public DepartmentViewModel? Insert(DepartmentBindingModel model) + { + var newElement = Department.Create(model); + if (newElement == null) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + context.Departments.Add(newElement); + context.SaveChanges(); + return newElement.GetViewModel; + } + + public DepartmentViewModel? Update(DepartmentBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Departments + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/EmployeeStorage.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/EmployeeStorage.cs new file mode 100644 index 0000000..fec4773 --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/EmployeeStorage.cs @@ -0,0 +1,89 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.SearchModels; +using PersonnelDepartmentContracts.StoragesContracts; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Implements +{ + public class EmployeeStorage : IEmployeeStorage + { + public EmployeeViewModel? Delete(EmployeeBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Employees + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + context.Employees.Remove(element); + return element.GetViewModel; + } + + public EmployeeViewModel? GetElement(EmployeeSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + return context.Employees + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.FirstName) && x.FirstName == model.FirstName) || + (!string.IsNullOrEmpty(model.LastName) && x.LastName == model.LastName) || + (!string.IsNullOrEmpty(model.Patronymic) && x.Patronymic == model.Patronymic))?.GetViewModel; + } + + public List GetFilteredList(EmployeeSearchModel model) + { + using var context = new PersonnelDepartmentDatabase(); + return context.Employees + .Where(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.FirstName) && x.FirstName == model.FirstName) || + (!string.IsNullOrEmpty(model.LastName) && x.LastName == model.LastName) || + (!string.IsNullOrEmpty(model.Patronymic) && x.Patronymic == model.Patronymic)) + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new PersonnelDepartmentDatabase(); + return context.Employees + .Select(x => x.GetViewModel) + .ToList(); + } + + public EmployeeViewModel? Insert(EmployeeBindingModel model) + { + var newElement = Employee.Create(model); + if (newElement == null) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + context.Employees.Add(newElement); + context.SaveChanges(); + return newElement.GetViewModel; + } + + public EmployeeViewModel? Update(EmployeeBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Employees + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/PositionStorage.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/PositionStorage.cs new file mode 100644 index 0000000..cf3f634 --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/PositionStorage.cs @@ -0,0 +1,85 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.SearchModels; +using PersonnelDepartmentContracts.StoragesContracts; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Implements +{ + public class PositionStorage : IPositionStorage + { + public PositionViewModel? Delete(PositionBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Positions + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + context.Positions.Remove(element); + return element.GetViewModel; + } + + public PositionViewModel? GetElement(PositionSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + return context.Positions + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name))?.GetViewModel; + } + + public List GetFilteredList(PositionSearchModel model) + { + using var context = new PersonnelDepartmentDatabase(); + return context.Positions + .Where(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name)) + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new PersonnelDepartmentDatabase(); + return context.Positions + .Select(x => x.GetViewModel) + .ToList(); + } + + public PositionViewModel? Insert(PositionBindingModel model) + { + var newElement = Position.Create(model); + if (newElement == null) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + context.Positions.Add(newElement); + context.SaveChanges(); + return newElement.GetViewModel; + } + + public PositionViewModel? Update(PositionBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Positions + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/TypeStorage.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/TypeStorage.cs new file mode 100644 index 0000000..d3df8bc --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Implements/TypeStorage.cs @@ -0,0 +1,86 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.SearchModels; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDatabaseImplement.Models; +using PersonnelTypeContracts.StoragesContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Type = PersonnelDepartmentDatabaseImplement.Models.Type; + +namespace PersonnelDepartmentDatabaseImplement.Implements +{ + public class TypeStorage : ITypeStorage + { + public TypeViewModel? Delete(TypeBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Types + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + context.Types.Remove(element); + return element.GetViewModel; + } + + public TypeViewModel? GetElement(TypeSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + return context.Types + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name))?.GetViewModel; + } + + public List GetFilteredList(TypeSearchModel model) + { + using var context = new PersonnelDepartmentDatabase(); + return context.Types + .Where(x => (model.Id.HasValue && x.Id == model.Id) || + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name)) + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new PersonnelDepartmentDatabase(); + return context.Types + .Select(x => x.GetViewModel) + .ToList(); + } + + public TypeViewModel? Insert(TypeBindingModel model) + { + var newElement = Type.Create(model); + if (newElement == null) + { + return null; + } + using var context = new PersonnelDepartmentDatabase(); + context.Types.Add(newElement); + context.SaveChanges(); + return newElement.GetViewModel; + } + + public TypeViewModel? Update(TypeBindingModel model) + { + using var context = new PersonnelDepartmentDatabase(); + var element = context.Types + .FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Deal.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Deal.cs new file mode 100644 index 0000000..a5e9731 --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Deal.cs @@ -0,0 +1,75 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Models +{ + public class Deal : IDealModel + { + [Required] + public DateTime DateFrom { get; set; } + public DateTime DateTo { get; set; } + [Required] + public int PositionId { get; set; } + [Required] + public int EmployeeId { get; set; } + [Required] + public int DepartmentId { get; set; } + [Required] + public int TypeId { get; set; } + public int Id { get; set; } + public virtual Position Position { get; set; } + public virtual Employee Employee { get; set; } + public virtual Department Department { get; set; } + public virtual Type Type { get; set; } + + public static Deal? Create(DealBindingModel model) + { + if(model == null) + { + return null; + } + return new Deal + { + Id = model.Id, + DateFrom = DateTime.SpecifyKind(model.DateFrom, DateTimeKind.Utc), + DateTo = DateTime.SpecifyKind(model.DateTo, DateTimeKind.Utc), + PositionId = model.PositionId, + EmployeeId = model.EmployeeId, + DepartmentId = model.DepartmentId, + TypeId = model.TypeId + }; + } + + public void Update(DealBindingModel model) + { + if (model == null) + { + return; + } + DateFrom = DateTime.SpecifyKind(model.DateFrom, DateTimeKind.Utc); + DateTo = DateTime.SpecifyKind(model.DateTo, DateTimeKind.Utc); + } + + public DealViewModel GetViewModel => new() + { + Id = Id, + DateFrom = DateFrom, + DateTo = DateTo, + PositionId = PositionId, + EmployeeId = EmployeeId, + DepartmentId = DepartmentId, + TypeId = TypeId, + PositionName = Position.Name, + DepartmentName = Department.Name, + EmployeeName = Employee.LastName + " " + Employee.FirstName[0] + "." + Employee.Patronymic[0] + ".", + TypeName = Type.Name + }; + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Department.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Department.cs new file mode 100644 index 0000000..02a8886 --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Department.cs @@ -0,0 +1,52 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Models +{ + public class Department : IDepartmentModel + { + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public long Telephone { get; set; } + public int Id { get; set; } + + public static Department? Create(DepartmentBindingModel model) + { + if (model == null) + { + return null; + } + return new Department + { + Id = model.Id, + Name = model.Name, + Telephone = model.Telephone + }; + } + + public void Update(DepartmentBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Telephone = model.Telephone; + } + + public DepartmentViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Telephone = Telephone + }; + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Employee.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Employee.cs new file mode 100644 index 0000000..f3d07a1 --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Employee.cs @@ -0,0 +1,56 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Models +{ + public class Employee : IEmployeeModel + { + [Required] + public string FirstName { get; set; } = string.Empty; + [Required] + public string LastName { get; set; } = string.Empty; + public string Patronymic { get; set; } = string.Empty; + public int Id { get; set; } + + public static Employee? Create(EmployeeBindingModel model) + { + if (model == null) + { + return null; + } + return new Employee + { + Id = model.Id, + FirstName = model.FirstName, + LastName = model.LastName, + Patronymic = model.Patronymic + }; + } + + public void Update(EmployeeBindingModel model) + { + if (model == null) + { + return; + } + FirstName = model.FirstName; + LastName = model.LastName; + Patronymic = model.Patronymic; + } + + public EmployeeViewModel GetViewModel => new() + { + Id = Id, + FirstName = FirstName, + LastName = LastName, + Patronymic = Patronymic + }; + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Position.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Position.cs new file mode 100644 index 0000000..fd4ea7e --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Position.cs @@ -0,0 +1,47 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Models +{ + public class Position : IPositionModel + { + [Required] + public string Name { get; set; } = string.Empty; + public int Id { get; set; } + + public static Position? Create(PositionBindingModel model) + { + if (model == null) + { + return null; + } + return new Position + { + Id = model.Id, + Name = model.Name + }; + } + + public void Update(PositionBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + } + + public PositionViewModel GetViewModel => new() + { + Id = Id, + Name = Name + }; + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Type.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Type.cs new file mode 100644 index 0000000..6bfad66 --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/Models/Type.cs @@ -0,0 +1,47 @@ +using PersonnelDepartmentContracts.BindingModels; +using PersonnelDepartmentContracts.ViewModels; +using PersonnelDepartmentDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonnelDepartmentDatabaseImplement.Models +{ + public class Type : ITypeModel + { + [Required] + public string Name { get; set; } = string.Empty; + public int Id { get; set; } + + public static Type? Create(TypeBindingModel model) + { + if (model == null) + { + return null; + } + return new Type + { + Id = model.Id, + Name = model.Name + }; + } + + public void Update(TypeBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + } + + public TypeViewModel GetViewModel => new() + { + Id = Id, + Name = Name + }; + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/PersonnelDepartmentDatabase.cs b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/PersonnelDepartmentDatabase.cs new file mode 100644 index 0000000..7b1d742 --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/PersonnelDepartmentDatabase.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using PersonnelDepartmentDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Type = PersonnelDepartmentDatabaseImplement.Models.Type; + +namespace PersonnelDepartmentDatabaseImplement +{ + public class PersonnelDepartmentDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@"Host=192.168.0.104;Port=5432;Database=PersonnelDepartment;Username=postgres;Password=postgres"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Deals { get; set; } + public virtual DbSet Departments { get; set; } + public virtual DbSet Employees { get; set; } + public virtual DbSet Positions { get; set; } + public virtual DbSet Types { get; set; } + } +} diff --git a/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/PersonnelDepartmentDatabaseImplement.csproj b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/PersonnelDepartmentDatabaseImplement.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/PersonnelDepartment/PersonnelDepartmentDatabaseImplement/PersonnelDepartmentDatabaseImplement.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + +