PIbd-22. Shabunov O.A. Lab work 03 #4

Closed
olshab wants to merge 6 commits from Lab3 into Lab2
9 changed files with 321 additions and 1 deletions
Showing only changes of commit 27449f920f - Show all commits

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoWorkshopBusinessLogic",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoWorkshopListImplement", "AutoWorkshopImplement\AutoWorkshopListImplement.csproj", "{B564F5E8-2F14-4816-8481-1F9649F1F414}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoWorkshopFileImplement", "AutoWorkshopFileImplement\AutoWorkshopFileImplement.csproj", "{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoWorkshopFileImplement", "AutoWorkshopFileImplement\AutoWorkshopFileImplement.csproj", "{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoWorkshopDatabaseImplement", "AutoWorkshopDatabaseImplement\AutoWorkshopDatabaseImplement.csproj", "{751668F7-01A3-43F0-BDD8-ABB3A8F5A955}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}.Release|Any CPU.Build.0 = Release|Any CPU
{751668F7-01A3-43F0-BDD8-ABB3A8F5A955}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{751668F7-01A3-43F0-BDD8-ABB3A8F5A955}.Debug|Any CPU.Build.0 = Debug|Any CPU
{751668F7-01A3-43F0-BDD8-ABB3A8F5A955}.Release|Any CPU.ActiveCfg = Release|Any CPU
{751668F7-01A3-43F0-BDD8-ABB3A8F5A955}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,29 @@
using AutoWorkshopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace AutoWorkshopDatabaseImplement
{
public class AutoWorkshopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder)
{
if (OptionsBuilder.IsConfigured == false)
{
OptionsBuilder.UseNpgsql(@"Host=localhost;Database=AutoWorkshop;Username=postgres;Password=postgres");
}
base.OnConfiguring(OptionsBuilder);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Repair> Repairs { set; get; }
public virtual DbSet<RepairComponent> RepairComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View 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="7.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="netcore-psql-util" Version="1.2.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AutoWorkshopContracts\AutoWorkshopContracts.csproj" />
<ProjectReference Include="..\AutoWorkshopDataModels\AutoWorkshopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,61 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AutoWorkshopDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<RepairComponent> RepairComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel Model)
{
if (Model == null)
return null;
return new Component()
{
Id = Model.Id,
ComponentName = Model.ComponentName,
Cost = Model.Cost
};
}
public static Component Create(ComponentViewModel Model)
{
return new Component
{
Id = Model.Id,
ComponentName = Model.ComponentName,
Cost = Model.Cost
};
}
public void Update(ComponentBindingModel Model)
{
if (Model == null)
return;
ComponentName = Model.ComponentName;
Cost = Model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,70 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Enums;
using System.ComponentModel.DataAnnotations;
namespace AutoWorkshopDatabaseImplement.Models
{
public class Order
{
public int Id { get; private set; }
[Required]
public int RepairId { get; private set; }
public virtual Repair Repair { get; set; } = new();
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Undefined;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public static Order Create(AutoWorkshopDatabase context, OrderBindingModel Model)
{
return new Order()
{
Id = Model.Id,
RepairId = Model.RepairId,
Count = Model.Count,
Sum = Model.Sum,
Status = Model.Status,
DateCreate = Model.DateCreate,
DateImplement = Model.DateImplement,
};
}
public void Update(OrderBindingModel Model)
{
if (Model == null)
return;
Id = Model.Id;
RepairId = Model.RepairId;
Sum = Model.Sum;
Status = Model.Status;
DateCreate = Model.DateCreate;
DateImplement = Model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
RepairId = RepairId,
RepairName = Repair.RepairName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
}

View File

@ -0,0 +1,104 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AutoWorkshopDatabaseImplement.Models
{
public class Repair
{
public int Id { get; set; }
[Required]
public string RepairName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _RepairComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> RepairComponents
{
get
{
if (_RepairComponents == null)
{
_RepairComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _RepairComponents;
}
}
[ForeignKey("RepairId")]
public virtual List<RepairComponent> Components { get; set; } = new();
[ForeignKey("RepairId")]
public virtual List<Order> Orders { get; set; } = new();
public static Repair Create(AutoWorkshopDatabase Context, RepairBindingModel Model)
{
return new Repair()
{
Id = Model.Id,
RepairName = Model.RepairName,
Price = Model.Price,
Components = Model.RepairComponents.Select(x => new RepairComponent
{
Component = Context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(RepairBindingModel Model)
{
RepairName = Model.RepairName;
Price = Model.Price;
}
public RepairViewModel GetViewModel => new()
{
Id = Id,
RepairName = RepairName,
Price = Price,
RepairComponents = RepairComponents
};
public void UpdateComponents(AutoWorkshopDatabase Context, RepairBindingModel Model)
{
var RepairComponents = Context.RepairComponents.Where(rec => rec.RepairId == Model.Id).ToList();
if (RepairComponents != null && RepairComponents.Count > 0)
{
Context.RepairComponents.RemoveRange(RepairComponents.Where(rec => !Model.RepairComponents.ContainsKey(rec.ComponentId)));
Context.SaveChanges();
foreach (var ComponentToUpdate in RepairComponents)
{
ComponentToUpdate.Count = Model.RepairComponents[ComponentToUpdate.ComponentId].Item2;
Model.RepairComponents.Remove(ComponentToUpdate.ComponentId);
}
Context.SaveChanges();
}
var Repair = Context.Repairs.First(x => x.Id == Id);
foreach (var RepairComponent in Model.RepairComponents)
{
Context.RepairComponents.Add(new RepairComponent
{
Repair = Repair,
Component = Context.Components.First(x => x.Id == RepairComponent.Key),
Count = RepairComponent.Value.Item2
});
Context.SaveChanges();
}
_RepairComponents = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace AutoWorkshopDatabaseImplement.Models
{
public class RepairComponent
{
public int Id { get; set; }
[Required]
public int RepairId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Repair Repair { get; set; } = new();
}
}

View File

@ -12,6 +12,10 @@
<ProjectReference Include="..\AutoWorkshopBusinessLogic\AutoWorkshopBusinessLogic.csproj" />
<ProjectReference Include="..\AutoWorkshopFileImplement\AutoWorkshopFileImplement.csproj" />
<ProjectReference Include="..\AutoWorkshopImplement\AutoWorkshopListImplement.csproj" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />

BIN
Задание.pdf Normal file

Binary file not shown.