2024-05-24 12:23:50 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public class Users : IUsersModel
|
2024-05-24 12:23:50 +04:00
|
|
|
|
{
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
|
|
|
|
[Required]
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public string UsersName { get; set; } = string.Empty;
|
2024-05-24 12:23:50 +04:00
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public double Price { get; set; }
|
|
|
|
|
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public Dictionary<int, (IKommentModel, int)>? _furnitureWorkPieces = null;
|
2024-05-24 12:23:50 +04:00
|
|
|
|
|
|
|
|
|
[NotMapped]
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public Dictionary<int, (IKommentModel, int)> FurnitureWorkPieces
|
2024-05-24 12:23:50 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_furnitureWorkPieces == null)
|
|
|
|
|
{
|
|
|
|
|
_furnitureWorkPieces = WorkPieces
|
2024-05-24 13:05:47 +04:00
|
|
|
|
.ToDictionary(recPC => recPC.WorkPieceId, recPC => (recPC.WorkPiece as IKommentModel, recPC.Count));
|
2024-05-24 12:23:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _furnitureWorkPieces;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("FurnitureId")]
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public virtual List<UsersKomment> WorkPieces { get; set; } = new();
|
2024-05-24 12:23:50 +04:00
|
|
|
|
|
|
|
|
|
[ForeignKey("FurnitureId")]
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public virtual List<Ad> Orders { get; set; } = new();
|
2024-05-24 12:23:50 +04:00
|
|
|
|
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public static Users Create(FurnitureAssemblyDatabase context, UsersBindingModel model)
|
2024-05-24 12:23:50 +04:00
|
|
|
|
{
|
2024-05-24 13:05:47 +04:00
|
|
|
|
return new Users()
|
2024-05-24 12:23:50 +04:00
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
2024-05-24 13:05:47 +04:00
|
|
|
|
UsersName = model.UsersName,
|
2024-05-24 12:23:50 +04:00
|
|
|
|
Price = model.Price,
|
2024-05-24 13:05:47 +04:00
|
|
|
|
WorkPieces = model.FurnitureWorkPieces.Select(x => new UsersKomment
|
2024-05-24 12:23:50 +04:00
|
|
|
|
{
|
|
|
|
|
WorkPiece = context.WorkPieces.First(y => y.Id == x.Key),
|
|
|
|
|
Count = x.Value.Item2
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public void Update(UsersBindingModel model)
|
2024-05-24 12:23:50 +04:00
|
|
|
|
{
|
2024-05-24 13:05:47 +04:00
|
|
|
|
UsersName = model.UsersName;
|
2024-05-24 12:23:50 +04:00
|
|
|
|
Price = model.Price;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FurnitureViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
2024-05-24 13:05:47 +04:00
|
|
|
|
UsersName = UsersName,
|
2024-05-24 12:23:50 +04:00
|
|
|
|
Price = Price,
|
|
|
|
|
FurnitureWorkPieces = FurnitureWorkPieces
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-24 13:05:47 +04:00
|
|
|
|
Dictionary<int, (IKommentModel, int)> IUsersModel.FurnitureWorkPieces => throw new NotImplementedException();
|
2024-05-24 12:23:50 +04:00
|
|
|
|
|
|
|
|
|
|
2024-05-24 13:05:47 +04:00
|
|
|
|
public void UpdateWorkPieces(FurnitureAssemblyDatabase context, UsersBindingModel model)
|
2024-05-24 12:23:50 +04:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2024-05-24 13:05:47 +04:00
|
|
|
|
context.FurnitureWorkPieces.Add(new UsersKomment
|
2024-05-24 12:23:50 +04:00
|
|
|
|
{
|
|
|
|
|
Furniture = furniture,
|
|
|
|
|
WorkPiece = context.WorkPieces.First(x => x.Id == pc.Key),
|
|
|
|
|
Count = pc.Value.Item2
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_furnitureWorkPieces = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|