Многие-ко-многим Предпродажная работа Комплектации

This commit is contained in:
Леонид Малафеев 2024-04-28 16:58:32 +04:00
parent 8a4a34b232
commit 923255d8ec
7 changed files with 64 additions and 2 deletions

View File

@ -18,6 +18,6 @@ namespace CarCenterContracts.BindingModels
public DateTime DueTill { get; set; }
public double Price { get; set; }
public Dictionary<int, IBundlingModel> PresaleBundlings { get; set; } = new();
}
}

View File

@ -20,5 +20,6 @@ namespace CarCenterContracts.ViewModels
public DateTime DueTill { get; set; }
[DisplayName("Цена")]
public double Price { get; set; }
public Dictionary<int, IBundlingModel> PresaleBundlings { get; set; } = new();
}
}

View File

@ -17,5 +17,6 @@ namespace CarCenterDataModels.Models
double Price { get; }
Dictionary<int, IBundlingModel> PresaleBundlings { get; }
}
}

View File

@ -30,5 +30,6 @@ namespace CarCenterDatabaseImplement
public virtual DbSet<Feature> Features { set; get; }
public virtual DbSet<Bundling> Bundlings { set; get; }
public virtual DbSet<CarBundling> CarBundlings { set; get; }
public virtual DbSet<PresaleBundling> PresaleBundlings { set; get; }
}
}

View File

@ -26,6 +26,8 @@ namespace CarCenterDatabaseImplement.Models
public double Price { get; set; }
[ForeignKey("BundlingId")]
public virtual List<CarBundling> CarBundling { get; set; } = new();
[ForeignKey("BundlingId")]
public virtual List<PresaleBundling> PresaleBundling { get; set; } = new();
public static Bundling? Create(BundlingBindingModel model)
{
if (model == null)

View File

@ -27,7 +27,22 @@ namespace CarCenterDatabaseImplement.Models
public virtual List<Request> Requests { get; set; } = new();
[ForeignKey("PresaleId")]
public virtual List<OrderPresale> OrderPresales { get; set; } = new();
public static Presale? Create(PresaleBindingModel? model)
private Dictionary<int, IBundlingModel>? _presaleBundlings = null;
[ForeignKey("PresaleId")]
public virtual List<PresaleBundling> Bundlings { get; set; } = new();
public Dictionary<int, IBundlingModel> PresaleBundlings
{
get
{
if (_presaleBundlings == null)
{
_presaleBundlings = Bundlings.ToDictionary(recPc => recPc.BundlingId, recPc => recPc.Bundling as IBundlingModel);
}
return _presaleBundlings;
}
}
public static Presale? Create(CarCenterDatabase context, PresaleBindingModel model)
{
if (model == null)
{
@ -40,9 +55,28 @@ namespace CarCenterDatabaseImplement.Models
Description = model.Description,
Price = model.Price,
DueTill = model.DueTill,
Bundlings = model.PresaleBundlings.Select(x => new PresaleBundling
{
Bundling = context.Bundlings.First(y => y.Id == x.Key)
}).ToList()
};
}
public void UpdateBundlings(CarCenterDatabase context, PresaleBindingModel model)
{
var presale = context.Presales.First(x => x.Id == Id);
foreach (var pc in model.PresaleBundlings)
{
context.PresaleBundlings.Add(new PresaleBundling
{
Presale = presale,
Bundling = context.Bundlings.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_presaleBundlings = null;
}
public void Update(PresaleBindingModel? model)
{
if (model == null)
@ -62,6 +96,7 @@ namespace CarCenterDatabaseImplement.Models
Description = Description,
DueTill = DueTill,
Price = Price,
PresaleBundlings = PresaleBundlings,
};
}
}

View File

@ -0,0 +1,22 @@
using CarCenterContracts.SearchModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterDatabaseImplement.Models
{
public class PresaleBundling
{
public int Id { get; set; }
[Required]
public int PresaleId { get; set; }
[Required]
public int BundlingId { get; set; }
[Required]
public virtual Presale Presale { get; set; } = new();
public virtual Bundling Bundling { get; set; } = new();
}
}