Допилил

This commit is contained in:
Artyom_Yashin 2024-03-25 20:04:51 +04:00
parent 49a826f977
commit eb9b029ef0
6 changed files with 22 additions and 33 deletions

View File

@ -29,7 +29,7 @@ namespace ComputersShop
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["Id"].Visible = true;
dataGridView.Columns["ComputerName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["ComputerComponents"].Visible = false;
}

View File

@ -3,6 +3,7 @@ using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -16,8 +17,8 @@ namespace ComputersShopDatabaseImplement.Implements
public List<OrderViewModel> GetFullList()
{
using var context = new ComputersShopDatabase();
return context.Orders
.Select(x => AccessComputersStorage(x.GetViewModel))
return context.Orders.Include(x => x.Computer)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -29,7 +30,7 @@ namespace ComputersShopDatabaseImplement.Implements
using var context = new ComputersShopDatabase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessComputersStorage(x.GetViewModel))
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
@ -39,19 +40,19 @@ namespace ComputersShopDatabaseImplement.Implements
return null;
}
using var context = new ComputersShopDatabase();
return AccessComputersStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new ComputersShopDatabase();
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new ComputersShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessComputersStorage(newOrder.GetViewModel);
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
@ -64,7 +65,7 @@ namespace ComputersShopDatabaseImplement.Implements
}
order.Update(model);
context.SaveChanges();
return AccessComputersStorage(order.GetViewModel);
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
@ -75,25 +76,9 @@ namespace ComputersShopDatabaseImplement.Implements
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessComputersStorage(element.GetViewModel);
return element.GetViewModel;
}
return null;
}
public static OrderViewModel AccessComputersStorage(OrderViewModel model)
{
if (model == null)
return null;
using var context = new ComputersShopDatabase();
foreach (var Computers in context.Computers)
{
if (Computers.Id == model.ComputerId)
{
model.ComputerName = Computers.ComputerName;
break;
}
}
return model;
}
}
}

View File

@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ComputersShopDatabaseImplement.Migrations
{
[DbContext(typeof(ComputersShopDatabase))]
[Migration("20240312150451_InitialCreate")]
[Migration("20240324163854_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -145,11 +145,13 @@ namespace ComputersShopDatabaseImplement.Migrations
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", null)
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer")
.WithMany("Orders")
.HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Computer");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b =>

View File

@ -142,11 +142,13 @@ namespace ComputersShopDatabaseImplement.Migrations
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", null)
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer")
.WithMany("Orders")
.HasForeignKey("ComputerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Computer");
});
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b =>

View File

@ -3,6 +3,7 @@ using ComputersShopContracts.ViewModels;
using ComputersShopDataModels.Enums;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;
namespace ComputersShopDatabaseImplement.Models
{
@ -21,12 +22,10 @@ namespace ComputersShopDatabaseImplement.Models
[Required]
public int ComputerId { get; private set; }
public virtual Computer Computer { get; set; } = new();
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
@ -48,7 +47,7 @@ namespace ComputersShopDatabaseImplement.Models
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
ComputerId = ComputerId,
@ -58,6 +57,7 @@ namespace ComputersShopDatabaseImplement.Models
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
ComputerName = Computer.ComputerName
};
}
}