118 lines
3.9 KiB
C#
118 lines
3.9 KiB
C#
using FurnitureAssemblyContracts.BindingModels;
|
||
using FurnitureAssemblyContracts.ViewModels;
|
||
using FurnitureAssemblyDataModels.Models;
|
||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace FurnitureAssemblyDatabaseImplement.Models
|
||
{
|
||
public class Users : IUsersModel
|
||
{
|
||
public int Id { get; set; }
|
||
|
||
[Required]
|
||
public string UsersName { get; set; } = string.Empty;
|
||
|
||
[Required]
|
||
public double Price { get; set; }
|
||
|
||
public Dictionary<int, (IKommentModel, int)>? _furnitureWorkPieces = null;
|
||
|
||
[NotMapped]
|
||
public Dictionary<int, (IKommentModel, int)> FurnitureWorkPieces
|
||
{
|
||
get
|
||
{
|
||
if (_furnitureWorkPieces == null)
|
||
{
|
||
_furnitureWorkPieces = WorkPieces
|
||
.ToDictionary(recPC => recPC.WorkPieceId, recPC => (recPC.WorkPiece as IKommentModel, recPC.Count));
|
||
}
|
||
|
||
return _furnitureWorkPieces;
|
||
}
|
||
}
|
||
|
||
[ForeignKey("FurnitureId")]
|
||
public virtual List<UsersKomment> WorkPieces { get; set; } = new();
|
||
|
||
[ForeignKey("FurnitureId")]
|
||
public virtual List<Ad> Orders { get; set; } = new();
|
||
|
||
public static Users Create(FurnitureAssemblyDatabase context, UsersBindingModel model)
|
||
{
|
||
return new Users()
|
||
{
|
||
Id = model.Id,
|
||
UsersName = model.UsersName,
|
||
Price = model.Price,
|
||
WorkPieces = model.FurnitureWorkPieces.Select(x => new UsersKomment
|
||
{
|
||
WorkPiece = context.WorkPieces.First(y => y.Id == x.Key),
|
||
Count = x.Value.Item2
|
||
}).ToList()
|
||
};
|
||
}
|
||
|
||
public void Update(UsersBindingModel model)
|
||
{
|
||
UsersName = model.UsersName;
|
||
Price = model.Price;
|
||
}
|
||
|
||
public FurnitureViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
UsersName = UsersName,
|
||
Price = Price,
|
||
FurnitureWorkPieces = FurnitureWorkPieces
|
||
};
|
||
|
||
Dictionary<int, (IKommentModel, int)> IUsersModel.FurnitureWorkPieces => throw new NotImplementedException();
|
||
|
||
|
||
public void UpdateWorkPieces(FurnitureAssemblyDatabase context, UsersBindingModel model)
|
||
{
|
||
var furnitureWorkPieces = context.FurnitureWorkPieces.Where(rec => rec.FurnitureId == model.Id).ToList();
|
||
|
||
if (furnitureWorkPieces != null && furnitureWorkPieces.Count > 0)
|
||
{
|
||
// удалили те, которых нет в модели
|
||
context.FurnitureWorkPieces.RemoveRange(furnitureWorkPieces.Where(rec => !model.FurnitureWorkPieces.ContainsKey(rec.FurnitureId)));
|
||
context.SaveChanges();
|
||
|
||
// обновили количество у существующих записей
|
||
foreach (var updateFurniture in furnitureWorkPieces)
|
||
{
|
||
updateFurniture.Count = model.FurnitureWorkPieces[updateFurniture.FurnitureId].Item2;
|
||
model.FurnitureWorkPieces.Remove(updateFurniture.FurnitureId);
|
||
}
|
||
|
||
context.SaveChanges();
|
||
}
|
||
|
||
var furniture = context.Furnitures.First(x => x.Id == Id);
|
||
|
||
foreach (var pc in model.FurnitureWorkPieces)
|
||
{
|
||
context.FurnitureWorkPieces.Add(new UsersKomment
|
||
{
|
||
Furniture = furniture,
|
||
WorkPiece = context.WorkPieces.First(x => x.Id == pc.Key),
|
||
Count = pc.Value.Item2
|
||
});
|
||
|
||
context.SaveChanges();
|
||
}
|
||
|
||
_furnitureWorkPieces = null;
|
||
}
|
||
}
|
||
}
|