SUBD_Gerimovich_Ilya_PIbd-22/FurnitureAssembly/FurnitureAssemblyDatabaseImplement/Models/Users.cs
platoff aeeee 4b0700dee1 20
2024-05-24 14:23:37 +04:00

118 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)>? _usersKomments = null;
[NotMapped]
public Dictionary<int, (IKommentModel, int)> UsersKomments
{
get
{
if (_usersKomments == null)
{
_usersKomments = Komments
.ToDictionary(recPC => recPC.KommentId, recPC => (recPC.Komment as IKommentModel, recPC.Count));
}
return _usersKomments;
}
}
[ForeignKey("UsersId")]
public virtual List<UsersKomment> Komments { get; set; } = new();
[ForeignKey("UsersId")]
public virtual List<Ad> Ads { get; set; } = new();
public static Users Create(FurnitureAssemblyDatabase context, UsersBindingModel model)
{
return new Users()
{
Id = model.Id,
UsersName = model.UsersName,
Price = model.Price,
Komments = model.UsersKomments.Select(x => new UsersKomment
{
Komment = context.Komments.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(UsersBindingModel model)
{
UsersName = model.UsersName;
Price = model.Price;
}
public UsersViewModel GetViewModel => new()
{
Id = Id,
UsersName = UsersName,
Price = Price,
UsersKomments = UsersKomments
};
Dictionary<int, (IKommentModel, int)> IUsersModel.UsersKomments => throw new NotImplementedException();
public void UpdateWorkPieces(FurnitureAssemblyDatabase context, UsersBindingModel model)
{
var usersKomments = context.UsersKomments.Where(rec => rec.UsersId == model.Id).ToList();
if (usersKomments != null && usersKomments.Count > 0)
{
// удалили те, которых нет в модели
context.UsersKomments.RemoveRange(usersKomments.Where(rec => !model.UsersKomments.ContainsKey(rec.UsersId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateUsers in usersKomments)
{
updateUsers.Count = model.UsersKomments[updateUsers.UsersId].Item2;
model.UsersKomments.Remove(updateUsers.UsersId);
}
context.SaveChanges();
}
var user = context.Userss.First(x => x.Id == Id);
foreach (var pc in model.UsersKomments)
{
context.UsersKomments.Add(new UsersKomment
{
Users = user,
Komment = context.Komments.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_usersKomments = null;
}
}
}