5 Commits
lab2 ... lab3

13 changed files with 1052 additions and 4 deletions

View File

@@ -0,0 +1,57 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace ShipyardDatabaseImplement.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<ShipComponent> ShipComponents { 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,89 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new ShipyardDataBase();
return context.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new ShipyardDataBase();
return context.Components
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new ShipyardDataBase();
return context.Components
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==
model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new ShipyardDataBase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new ShipyardDataBase();
var component = context.Components.FirstOrDefault(x => x.Id ==
model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new ShipyardDataBase();
var element = context.Components.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@@ -0,0 +1,168 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using ShipyardDatabaseImplement;
#nullable disable
namespace ShipyardDatabaseImplement.Migrations
{
[DbContext(typeof(ShipyardDataBase))]
[Migration("20240424073031_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("ShipId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ShipId");
b.ToTable("Orders");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Ship", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("ShipName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Ships");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.ShipComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShipId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("ShipId");
b.ToTable("ShipComponents");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Order", b =>
{
b.HasOne("ShipyardDatabaseImplement.Models.Ship", null)
.WithMany("Orders")
.HasForeignKey("ShipId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.ShipComponent", b =>
{
b.HasOne("ShipyardDatabaseImplement.Models.Component", "Component")
.WithMany("ShipComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ShipyardDatabaseImplement.Models.Ship", "Ship")
.WithMany("Components")
.HasForeignKey("ShipId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Ship");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Component", b =>
{
b.Navigation("ShipComponents");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Ship", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,122 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ShipyardDatabaseImplement.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Ships",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShipName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Ships", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true),
ShipId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Ships_ShipId",
column: x => x.ShipId,
principalTable: "Ships",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShipComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShipId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShipComponents", x => x.Id);
table.ForeignKey(
name: "FK_ShipComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShipComponents_Ships_ShipId",
column: x => x.ShipId,
principalTable: "Ships",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_ShipId",
table: "Orders",
column: "ShipId");
migrationBuilder.CreateIndex(
name: "IX_ShipComponents_ComponentId",
table: "ShipComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_ShipComponents_ShipId",
table: "ShipComponents",
column: "ShipId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShipComponents");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Ships");
}
}
}

View File

@@ -0,0 +1,166 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using ShipyardDatabaseImplement;
#nullable disable
namespace ShipyardDatabaseImplement.Migrations
{
[DbContext(typeof(ShipyardDataBase))]
partial class ShipyardDataBaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("ShipId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ShipId");
b.ToTable("Orders");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Ship", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("ShipName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Ships");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.ShipComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShipId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("ShipId");
b.ToTable("ShipComponents");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Order", b =>
{
b.HasOne("ShipyardDatabaseImplement.Models.Ship", null)
.WithMany("Orders")
.HasForeignKey("ShipId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.ShipComponent", b =>
{
b.HasOne("ShipyardDatabaseImplement.Models.Component", "Component")
.WithMany("ShipComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ShipyardDatabaseImplement.Models.Ship", "Ship")
.WithMany("Components")
.HasForeignKey("ShipId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Ship");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Component", b =>
{
b.Navigation("ShipComponents");
});
modelBuilder.Entity("ShipyardDatabaseImplement.Models.Ship", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,66 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Enums;
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; }
[Required]
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
[Required]
public int ShipId { get; private set; }
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
ShipId = model.ShipId,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
ShipId = ShipId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
};
}
}

View File

@@ -0,0 +1,99 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new ShipyardDataBase();
return context.Orders
.Select(x => AccessShipStorage(x.GetViewModel))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ShipyardDataBase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessShipStorage(x.GetViewModel))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ShipyardDataBase();
return AccessShipStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new ShipyardDataBase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessShipStorage(newOrder.GetViewModel);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new ShipyardDataBase();
var order = context.Orders.FirstOrDefault(x => x.Id ==
model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return AccessShipStorage(order.GetViewModel);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new ShipyardDataBase();
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessShipStorage(element.GetViewModel);
}
return null;
}
public static OrderViewModel AccessShipStorage(OrderViewModel model)
{
if (model == null)
return null;
using var context = new ShipyardDataBase();
foreach (var manufacture in context.Ships)
{
if (manufacture.Id == model.ShipId)
{
model.ShipName = manufacture.ShipName;
break;
}
}
return model;
}
}
}

View File

@@ -0,0 +1,100 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDatabaseImplement.Models;
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDatabaseImplement.Models
{
public class Ship : IShipModel
{
public int Id { get; set; }
[Required]
public string ShipName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _shipComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> ShipComponents
{
get
{
if (_shipComponents == null)
{
_shipComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _shipComponents;
}
}
[ForeignKey("ShipId")]
public virtual List<ShipComponent> Components { get; set; } = new();
[ForeignKey("ShipId")]
public virtual List<Order> Orders { get; set; } = new();
public static Ship Create(ShipyardDataBase context,
ShipBindingModel model)
{
return new Ship()
{
Id = model.Id,
ShipName = model.ShipName,
Price = model.Price,
Components = model.ShipComponents.Select(x => new
ShipComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShipBindingModel model)
{
ShipName = model.ShipName;
Price = model.Price;
}
public ShipViewModel GetViewModel => new()
{
Id = Id,
ShipName = ShipName,
Price = Price,
ShipComponents = ShipComponents
};
public void UpdateComponents(ShipyardDataBase context,
ShipBindingModel model)
{
var manufactureComponents = context.ShipComponents.Where(rec =>
rec.ShipId == model.Id).ToList();
if (manufactureComponents != null && ShipComponents.Count > 0)
{
context.ShipComponents.RemoveRange(manufactureComponents.Where(rec
=> !model.ShipComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in manufactureComponents)
{
updateComponent.Count =
model.ShipComponents[updateComponent.ComponentId].Item2;
model.ShipComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var manufacture = context.Ships.First(x => x.Id == Id);
foreach (var pc in model.ShipComponents)
{
context.ShipComponents.Add(new ShipComponent
{
Ship = manufacture,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_shipComponents = null;
}
}
}

View 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 ShipyardDatabaseImplement.Models
{
public class ShipComponent
{
public int Id { get; set; }
[Required]
public int ShipId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Ship Ship { get; set; } = new();
}
}

View File

@@ -0,0 +1,110 @@
using Microsoft.EntityFrameworkCore;
using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardDatabaseImplement;
using ShipyardDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDatabaseImplement.Implements
{
public class ShipStorage : IShipStorage
{
public List<ShipViewModel> GetFullList()
{
using var context = new ShipyardDataBase();
return context.Ships
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShipViewModel> GetFilteredList(ShipSearchModel model)
{
if (string.IsNullOrEmpty(model.ShipName))
{
return new();
}
using var context = new ShipyardDataBase();
return context.Ships.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.ShipName.Contains(model.ShipName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShipViewModel? GetElement(ShipSearchModel model)
{
if (string.IsNullOrEmpty(model.ShipName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ShipyardDataBase();
return context.Ships
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShipName) &&
x.ShipName == model.ShipName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public ShipViewModel? Insert(ShipBindingModel model)
{
using var context = new ShipyardDataBase();
var newManufacture = Ship.Create(context, model);
if (newManufacture == null)
{
return null;
}
context.Ships.Add(newManufacture);
context.SaveChanges();
return newManufacture.GetViewModel;
}
public ShipViewModel? Update(ShipBindingModel model)
{
using var context = new ShipyardDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var manufacture = context.Ships.FirstOrDefault(rec =>
rec.Id == model.Id);
if (manufacture == null)
{
return null;
}
manufacture.Update(model);
context.SaveChanges();
manufacture.UpdateComponents(context, model);
transaction.Commit();
return manufacture.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShipViewModel? Delete(ShipBindingModel model)
{
using var context = new ShipyardDataBase();
var element = context.Ships
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Ships.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using ShipyardDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore.SqlServer;
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardDatabaseImplement
{
public class ShipyardDataBase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ShipyardDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Ship> Ships { set; get; }
public virtual DbSet<ShipComponent> ShipComponents { 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="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="..\..\ShipyardContracts\ShipyardContracts\ShipyardContracts.csproj" />
<ProjectReference Include="..\..\ShipyardDataModels\ShipyardDataModels\ShipyardDataModels.csproj" />
<ProjectReference Include="..\..\ShipyardBusinessLogic\ShipyardBusinessLogic\ShipyardBusinessLogic.csproj" />
</ItemGroup>
</Project>

View File

@@ -6,10 +6,7 @@ using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;
using System; using System;
using ShipyardBusinessLogic; using ShipyardBusinessLogic;
using ShipyardFileImplement; using ShipyardDatabaseImplement.Implements;
using ShipyardFileImplement.Implements;
using OrderStorage = ShipyardFileImplement.Implements.OrderStorage;
using ShipStorage = ShipyardFileImplement.Implements.ShipStorage;
namespace ShipyardView namespace ShipyardView
{ {