Database basics done
This commit is contained in:
parent
9f505c53e6
commit
0fb8aaed4b
@ -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
|
||||
|
@ -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<T>? GetList<T>() where T : class, new()
|
||||
{
|
||||
using var context = new PersonnelDepartmentDatabase();
|
||||
return context.Set<T>().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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<DealViewModel> 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<DealViewModel> 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 });
|
||||
}
|
||||
}
|
||||
}
|
@ -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<DepartmentViewModel> 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<DepartmentViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<EmployeeViewModel> 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<EmployeeViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<PositionViewModel> 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<PositionViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<TypeViewModel> 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<TypeViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
@ -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<Deal> Deals { get; set; }
|
||||
public virtual DbSet<Department> Departments { get; set; }
|
||||
public virtual DbSet<Employee> Employees { get; set; }
|
||||
public virtual DbSet<Position> Positions { get; set; }
|
||||
public virtual DbSet<Type> Types { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user