+классы
This commit is contained in:
parent
8ccddf69ea
commit
2b108bd95e
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ConfectioneryDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
||||
namespace ConfectioneryDatabaseImplement
|
||||
{
|
||||
public class ConfectioneryDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ConfectioneryDatabaseFull;Integrated
|
||||
Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Component> Components { set; get; }
|
||||
public virtual DbSet<Pastry> Pastrys { set; get; }
|
||||
public virtual DbSet<PastryComponent> PastryComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
}
|
70
Confectionery/ConfectioneryDatabaseImplement/Order.cs
Normal file
70
Confectionery/ConfectioneryDatabaseImplement/Order.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDataModels.Enums;
|
||||
using ConfectioneryDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ConfectioneryDatabaseImplement.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 PastryId { get; private set; }
|
||||
public virtual Pastry Pastry { get; private set; }
|
||||
public static Order? Create(ConfectioneryDatabase context, 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,
|
||||
PastryId = model.PastryId,
|
||||
Pastry = context.Pastrys.FirstOrDefault(x => x.Id == model.PastryId)
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
|
||||
public OrderViewModel GetViewModel => new()
|
||||
{
|
||||
PastryId = PastryId,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
PastryName = Pastry.PastryName,
|
||||
Id = Id,
|
||||
};
|
||||
}
|
||||
}
|
96
Confectionery/ConfectioneryDatabaseImplement/Pastry.cs
Normal file
96
Confectionery/ConfectioneryDatabaseImplement/Pastry.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
|
||||
namespace ConfectioneryDatabaseImplement.Models
|
||||
{
|
||||
public class Pastry : IPastryModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string PastryName { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
private Dictionary<int, (IComponentModel, int)>? _pastryComponents = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> PastryComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_pastryComponents == null)
|
||||
{
|
||||
_pastryComponents = Components
|
||||
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
||||
(recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _pastryComponents;
|
||||
}
|
||||
}
|
||||
[ForeignKey("PastryId")]
|
||||
public virtual List<PastryComponent> Components { get; set; } = new();
|
||||
[ForeignKey("PastryId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model)
|
||||
{
|
||||
return new Pastry()
|
||||
{
|
||||
Id = model.Id,
|
||||
PastryName = model.PastryName,
|
||||
Price = model.Price,
|
||||
Components = model.PastryComponents.Select(x => new PastryComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(PastryBindingModel model)
|
||||
{
|
||||
PastryName = model.PastryName;
|
||||
Price = model.Price;
|
||||
}
|
||||
public PastryViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
PastryName = PastryName,
|
||||
Price = Price,
|
||||
PastryComponents = PastryComponents
|
||||
};
|
||||
public void UpdateComponents(ConfectioneryDatabase context, PastryBindingModel model)
|
||||
{
|
||||
var pastryComponents = context.PastryComponents.Where(rec => rec.PastryId == model.Id).ToList();
|
||||
if (pastryComponents != null && pastryComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.PastryComponents.RemoveRange(pastryComponents.Where(rec => !model.PastryComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in pastryComponents)
|
||||
{
|
||||
updateComponent.Count = model.PastryComponents[updateComponent.ComponentId].Item2;
|
||||
model.PastryComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var pastry = context.Pastrys.First(x => x.Id == Id);
|
||||
foreach (var pc in model.PastryComponents)
|
||||
{
|
||||
context.PastryComponents.Add(new PastryComponent
|
||||
{
|
||||
Pastry = pastry,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_pastryComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
|
||||
namespace ConfectioneryDatabaseImplement.Models
|
||||
{
|
||||
public class PastryComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int PastryId { get; set; }
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Component Component { get; set; } = new();
|
||||
public virtual Pastry Pastry { get; set; } = new();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user