Сделано хранилище для сотрудника, немного исправлений

This commit is contained in:
kagbie3nn@mail.ru 2024-04-27 17:00:02 +04:00
parent d1012a40ab
commit 135e7c9a1f
12 changed files with 350 additions and 8 deletions

View File

@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZooContracts", "ZooContract
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZooDataModels", "ZooDataModels\ZooDataModels.csproj", "{FBA80C4B-D1A0-42D4-A90B-123323E06314}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZooDataBaseImplement", "ZooDataBaseImplement\ZooDataBaseImplement.csproj", "{599C34A0-51C7-4782-9F9C-F2EFCC02FFA2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,10 @@ Global
{FBA80C4B-D1A0-42D4-A90B-123323E06314}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBA80C4B-D1A0-42D4-A90B-123323E06314}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FBA80C4B-D1A0-42D4-A90B-123323E06314}.Release|Any CPU.Build.0 = Release|Any CPU
{599C34A0-51C7-4782-9F9C-F2EFCC02FFA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{599C34A0-51C7-4782-9F9C-F2EFCC02FFA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{599C34A0-51C7-4782-9F9C-F2EFCC02FFA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{599C34A0-51C7-4782-9F9C-F2EFCC02FFA2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -5,8 +5,9 @@ namespace ZooContracts.BindingModels
public class EmployeeBindingModel : IEmployeeModel
{
public int Id { get; set; }
public string EmployeeFIO { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string EmployeeFIO { get; set; } = string.Empty;
public string Login { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
}

View File

@ -10,7 +10,7 @@ namespace ZooContracts.BindingModels
public class PreserveBindingModel : IPreserveModel
{
public int Id { get; set; }
public string PreserveName { get; set; }
public string PreserveName { get; set; } = string.Empty;
public double PreservePrice { get; set; }
}
}

View File

@ -10,5 +10,7 @@ namespace ZooContracts.SearchModels
{
public int? Id { get; set; }
public string? EmployeeFIO { get; set; }
public string Login { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
}

View File

@ -12,11 +12,11 @@ namespace ZooContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("ФИО сотрудника")]
public string EmployeeFIO { get; set; }
public string EmployeeFIO { get; set; } = string.Empty;
[DisplayName("Логин сотрудника")]
public string Login { get; set; }
public string Login { get; set; } = string.Empty;
[DisplayName("Пароль сотрудника")]
public string Password { get; set; }
public string Password { get; set; } = string.Empty;
}
}

View File

@ -12,7 +12,7 @@ namespace ZooContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Название заповедника")]
public string PreserveName { get; set; }
public string PreserveName { get; set; } = string.Empty;
[DisplayName("Стоимость посещения заповедника")]
public double PreservePrice { get; set; }
}

View File

@ -0,0 +1,93 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.SearchModels;
using ZooContracts.ViewModels;
using ZooDataBaseImplement.Models;
namespace ZooDataBaseImplement.Implements
{
public class EmployeeStorage : DbContext
{
public EmployeeViewModel? Delete(EmployeeBindingModel model)
{
using var context = new ZooDatabase();
var element = context.Employees.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Employees.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ZooDatabase();
return context.Employees.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
{
using var context = new ZooDatabase();
return context.Employees.Where(x => ((!model.Id.HasValue || x.Id == model.Id)
&& (!string.IsNullOrEmpty(model.Login) || x.Login == model.Login)
&& (!string.IsNullOrEmpty(model.Password) || x.Password == model.Password)
&& (!string.IsNullOrEmpty(model.EmployeeFIO) || x.EmployeeFIO == model.EmployeeFIO)
))
.Select(x => x.GetViewModel).ToList();
}
public List<EmployeeViewModel> GetFullList()
{
using var context = new ZooDatabase();
return context.Employees.Select(x => x.GetViewModel).ToList();
}
public EmployeeViewModel? Insert(EmployeeBindingModel model)
{
var newEmployee = Employee.Create(model);
if (newEmployee == null)
{
return null;
}
using var context = new ZooDatabase();
context.Employees.Add(newEmployee);
context.SaveChanges();
return newEmployee.GetViewModel;
}
public EmployeeViewModel? Update(EmployeeBindingModel model)
{
using var context = new ZooDatabase();
var Employee = context.Employees.FirstOrDefault(x => x.Id == model.Id);
if (Employee == null)
{
return null;
}
Employee.Update(model);
context.SaveChanges();
return Employee.GetViewModel;
}
}
}

View File

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.SearchModels;
using ZooContracts.ViewModels;
using ZooDataBaseImplement.Models;
namespace ZooDataBaseImplement.Implements
{
public class PreserveStorage
{
public List<PreserveViewModel> GetFullList()
{
using var context = new ZooDatabase();
return context.Preserves
.Select(x => x.GetViewModel)
.ToList();
}
public List<PreserveViewModel> GetFilteredList(PreserveSearchModel
model)
{
if (string.IsNullOrEmpty(model.PreserveName))
{
return new();
}
using var context = new ZooDatabase();
return context.Preserves
.Where(x => x.PreserveName.Contains(model.PreserveName))
.Select(x => x.GetViewModel)
.ToList();
}
public PreserveViewModel? GetElement(PreserveSearchModel model)
{
if (string.IsNullOrEmpty(model.PreserveName) && !model.Id.HasValue)
{
return null;
}
using var context = new ZooDatabase();
return context.Preserves
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.PreserveName) && x.PreserveName ==
model.PreserveName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public PreserveViewModel? Insert(PreserveBindingModel model)
{
var newPreserve = Preserve.Create(model);
if (newPreserve == null)
{
return null;
}
using var context = new ZooDatabase();
context.Preserves.Add(newPreserve);
context.SaveChanges();
return newPreserve.GetViewModel;
}
public PreserveViewModel? Update(PreserveBindingModel model)
{
using var context = new ZooDatabase();
var component = context.Preserves.FirstOrDefault(x => x.Id ==
model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public PreserveViewModel? Delete(PreserveBindingModel model)
{
using var context = new ZooDatabase();
var element = context.Preserves.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Preserves.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,53 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using ZooContracts.BindingModels;
using ZooContracts.ViewModels;
using ZooDataModels.Models;
namespace ZooDataBaseImplement.Models
{
public class Employee :IEmployeeModel
{
public int Id { get; set; }
[Required]
public string EmployeeFIO { get; set; } = string.Empty;
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[ForeignKey("EmployeeId")]
public virtual List<Preserve> Preserves { get; set; } = new();
public static Employee? Create(EmployeeBindingModel model)
{
if (model == null)
{
return null;
}
return new Employee()
{
Id = model.Id,
EmployeeFIO = model.EmployeeFIO,
Login = model.Login,
Password = model.Password
};
}
public void Update(EmployeeBindingModel model)
{
if (model == null)
{
return;
}
EmployeeFIO = model.EmployeeFIO;
Login = model.Login;
Password = model.Password;
}
public EmployeeViewModel GetViewModel => new()
{
Id = Id,
EmployeeFIO = EmployeeFIO,
Login = Login,
Password = Password
};
}
}

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.ViewModels;
using ZooDataModels.Models;
namespace ZooDataBaseImplement.Models
{
public class Preserve :IPreserveModel
{
public int Id { get; set; }
[Required]
public string PreserveName { get; set; } = string.Empty;
[Required]
public double PreservePrice { get; set; }
public static Preserve? Create(PreserveBindingModel model)
{
if (model == null)
{
return null;
}
return new Preserve()
{
Id = model.Id,
PreserveName = model.PreserveName,
PreservePrice = model.PreservePrice,
};
}
public void Update(PreserveBindingModel model)
{
if (model == null)
{
return;
}
PreserveName = model.PreserveName;
PreservePrice = model.PreservePrice;
}
public PreserveViewModel GetViewModel => new()
{
Id = Id,
PreserveName = PreserveName,
PreservePrice = PreservePrice,
};
}
}

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.SqlServer" 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>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZooContracts\ZooContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooDataBaseImplement.Models;
namespace ZooDataBaseImplement
{
public class ZooDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder
optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=ComputersShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Employee> Employees { set; get; }
public virtual DbSet<Preserve> Preserves { set; get; }
}
}