Upload files to 'STODataBaseImplement'
This commit is contained in:
parent
e22e0cc6d9
commit
2ae45ed3cd
22
STODataBaseImplement/CarTechnicalWork.cs
Normal file
22
STODataBaseImplement/CarTechnicalWork.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class CarTechnicalWork
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int CarId { get; set; }
|
||||
[Required]
|
||||
public int TechnicalWorkId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Car Car { get; set; } = new();
|
||||
public virtual TechnicalWork TechnicalWork { get; set; } = new();
|
||||
}
|
||||
}
|
59
STODataBaseImplement/Client.cs
Normal file
59
STODataBaseImplement/Client.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using STODataModels;
|
||||
using STOContracts.BindingModels;
|
||||
using STOContracts.ViewModels;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class Client: IClientModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public string ClientFIO { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string Email { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string Password { get; private set; }
|
||||
[ForeignKey("ClientId")]
|
||||
public virtual List<Car> Cars { get; private set; } = new();
|
||||
[ForeignKey("ClientId")]
|
||||
public virtual List<Service> Services { get; private set; } = new();
|
||||
public static Client? Create(ClientBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Client()
|
||||
{
|
||||
Id = model.Id,
|
||||
ClientFIO = model.ClientFIO,
|
||||
Email = model.Email,
|
||||
Password = model.Password
|
||||
};
|
||||
}
|
||||
public void Update(ClientBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ClientFIO = model.ClientFIO;
|
||||
Email = model.Email;
|
||||
Password = model.Password;
|
||||
}
|
||||
public ClientViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ClientFIO = ClientFIO,
|
||||
Email = Email,
|
||||
Password = Password
|
||||
};
|
||||
}
|
||||
}
|
83
STODataBaseImplement/ClientStorage.cs
Normal file
83
STODataBaseImplement/ClientStorage.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using STOContracts.BindingModels;
|
||||
using STOContracts.SearchModels;
|
||||
using STOContracts.ViewModels;
|
||||
using STODatabaseImplement;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class ClientStorage
|
||||
{
|
||||
public List<ClientViewModel> GetFullList()
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
return context.Clients.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ClientFIO))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public ClientViewModel? GetElement(ClientSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
return context.Clients.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public ClientViewModel? Insert(ClientBindingModel model)
|
||||
{
|
||||
var newComponent = Client.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new STODatabase();
|
||||
context.Clients.Add(newComponent);
|
||||
context.SaveChanges();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
|
||||
public ClientViewModel? Update(ClientBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var component = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
|
||||
public ClientViewModel? Delete(ClientBindingModel model)
|
||||
{
|
||||
using var context = new STODatabase();
|
||||
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Clients.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
24
STODataBaseImplement/DatabaseImplement.csproj
Normal file
24
STODataBaseImplement/DatabaseImplement.csproj
Normal file
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\STOContracts\STOContracts\STOContracts.csproj" />
|
||||
<ProjectReference Include="..\..\STODataModels\STODataModels\STODataModels.csproj" />
|
||||
<ProjectReference Include="..\..\STOBusinessLogic\STOBusinessLogic\STOBusinessLogic.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
58
STODataBaseImplement/Service.cs
Normal file
58
STODataBaseImplement/Service.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using STOContracts.BindingModels;
|
||||
using STOContracts.ViewModels;
|
||||
using STODatabaseImplement;
|
||||
using STODataModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DatabaseImplement
|
||||
{
|
||||
public class Service : IServiceModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public string Name {get; private set;} = string.Empty;
|
||||
|
||||
[Required]
|
||||
public int CarId { get; private set; }
|
||||
public virtual Car Car { get; set; } = new();
|
||||
|
||||
public int ClientId { get; private set; }
|
||||
public virtual Client Client { get; set; } = new();
|
||||
|
||||
public static Service? Create(STODatabase context, ServiceBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Service()
|
||||
{
|
||||
CarId = model.CarId,
|
||||
Car = context.Cars.First(x => x.Id == model.CarId),
|
||||
ClientId = model.ClientId,
|
||||
Client = context.Clients.First(x => x.Id == model.ClientId),
|
||||
Id = model.Id,
|
||||
Name = model.Name
|
||||
};
|
||||
}
|
||||
public void Update(ServiceBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
}
|
||||
public ServiceViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
CarId = CarId
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user