Исправляю ошибки молодости

This commit is contained in:
gg12 darfren 2024-03-18 17:16:29 +04:00
parent a94e774482
commit 7eb4e5cfb0
5 changed files with 21 additions and 26 deletions

View File

@ -3,6 +3,7 @@ using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -16,8 +17,8 @@ namespace IceCreamShopDatabaseImplement.Implements
public List<OrderViewModel> GetFullList()
{
using var context = new IceCreamShopDataBase();
return context.Orders
.Select(x => AccessIceCreamStorage(x.GetViewModel))
return context.Orders.Include(x => x.IceCream)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -29,7 +30,7 @@ namespace IceCreamShopDatabaseImplement.Implements
using var context = new IceCreamShopDataBase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessIceCreamStorage(x.GetViewModel))
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
@ -39,19 +40,19 @@ namespace IceCreamShopDatabaseImplement.Implements
return null;
}
using var context = new IceCreamShopDataBase();
return AccessIceCreamStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
using var context = new IceCreamShopDataBase();
var newOrder = Order.Create(context, model);
if (newOrder == null)
{
return null;
}
using var context = new IceCreamShopDataBase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessIceCreamStorage(newOrder.GetViewModel);
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
@ -64,7 +65,7 @@ namespace IceCreamShopDatabaseImplement.Implements
}
order.Update(model);
context.SaveChanges();
return AccessIceCreamStorage(order.GetViewModel);
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
@ -75,21 +76,9 @@ namespace IceCreamShopDatabaseImplement.Implements
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessIceCreamStorage(element.GetViewModel);
return element.GetViewModel;
}
return null;
}
public static OrderViewModel AccessIceCreamStorage(OrderViewModel model)
{
if (model == null)
return null;
using var context = new IceCreamShopDataBase();
var iceCream = context.IceCreams.FirstOrDefault(x => (x.Id == model.IceCreamId));
if (iceCream == null)
return model;
model.IceCreamName = iceCream.IceCreamName;
return model;
}
}
}

View File

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

View File

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

View File

@ -25,8 +25,8 @@ namespace IceCreamShopDatabaseImplement.Models
public DateTime? DateImplement { get; private set; }
[Required]
public int IceCreamId { get; private set; }
public static Order? Create(OrderBindingModel model)
public virtual IceCream IceCream { get; private set; }
public static Order? Create(IceCreamShopDataBase context, OrderBindingModel model)
{
if (model == null)
{
@ -41,6 +41,7 @@ namespace IceCreamShopDatabaseImplement.Models
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
IceCreamId = model.IceCreamId,
IceCream = context.IceCreams.FirstOrDefault(x => x.Id == model.IceCreamId)
};
}
@ -62,6 +63,7 @@ namespace IceCreamShopDatabaseImplement.Models
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
IceCreamName = IceCream.IceCreamName,
Id = Id,
};