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

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.StoragesContracts;
using IceCreamShopContracts.ViewModels; using IceCreamShopContracts.ViewModels;
using IceCreamShopDatabaseImplement.Models; using IceCreamShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -16,8 +17,8 @@ namespace IceCreamShopDatabaseImplement.Implements
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {
using var context = new IceCreamShopDataBase(); using var context = new IceCreamShopDataBase();
return context.Orders return context.Orders.Include(x => x.IceCream)
.Select(x => AccessIceCreamStorage(x.GetViewModel)) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
public List<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -29,7 +30,7 @@ namespace IceCreamShopDatabaseImplement.Implements
using var context = new IceCreamShopDataBase(); using var context = new IceCreamShopDataBase();
return context.Orders return context.Orders
.Where(x => x.Id == model.Id) .Where(x => x.Id == model.Id)
.Select(x => AccessIceCreamStorage(x.GetViewModel)) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
public OrderViewModel? GetElement(OrderSearchModel model) public OrderViewModel? GetElement(OrderSearchModel model)
@ -39,19 +40,19 @@ namespace IceCreamShopDatabaseImplement.Implements
return null; return null;
} }
using var context = new IceCreamShopDataBase(); 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) public OrderViewModel? Insert(OrderBindingModel model)
{ {
var newOrder = Order.Create(model); using var context = new IceCreamShopDataBase();
var newOrder = Order.Create(context, model);
if (newOrder == null) if (newOrder == null)
{ {
return null; return null;
} }
using var context = new IceCreamShopDataBase();
context.Orders.Add(newOrder); context.Orders.Add(newOrder);
context.SaveChanges(); context.SaveChanges();
return AccessIceCreamStorage(newOrder.GetViewModel); return newOrder.GetViewModel;
} }
public OrderViewModel? Update(OrderBindingModel model) public OrderViewModel? Update(OrderBindingModel model)
{ {
@ -64,7 +65,7 @@ namespace IceCreamShopDatabaseImplement.Implements
} }
order.Update(model); order.Update(model);
context.SaveChanges(); context.SaveChanges();
return AccessIceCreamStorage(order.GetViewModel); return order.GetViewModel;
} }
public OrderViewModel? Delete(OrderBindingModel model) public OrderViewModel? Delete(OrderBindingModel model)
{ {
@ -75,21 +76,9 @@ namespace IceCreamShopDatabaseImplement.Implements
{ {
context.Orders.Remove(element); context.Orders.Remove(element);
context.SaveChanges(); context.SaveChanges();
return AccessIceCreamStorage(element.GetViewModel); return element.GetViewModel;
} }
return null; 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 namespace IceCreamShopDatabaseImplement.Migrations
{ {
[DbContext(typeof(IceCreamShopDataBase))] [DbContext(typeof(IceCreamShopDataBase))]
[Migration("20240304143043_InitialCreate")] [Migration("20240318125446_InitialCreate")]
partial class InitialCreate partial class InitialCreate
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -145,11 +145,13 @@ namespace IceCreamShopDatabaseImplement.Migrations
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
{ {
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("Orders") .WithMany("Orders")
.HasForeignKey("IceCreamId") .HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.Navigation("IceCream");
}); });
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>

View File

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

View File

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