using Contracts.BindingModels; using Contracts.ViewModels; using DataModels.Models; 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; using System.Xml.Linq; namespace DatabaseImplement.Models { public class Machine : IMachineModel { public int Id { get; private set; } [Required] public string Title { get; private set; } = string.Empty; [Required] public string Country { get; private set; } = string.Empty; [Required] public int UserId { get; private set; } [Required] public int ProductId { get; private set; } public virtual Product Product { get; set; } [ForeignKey("WorkerId")] public virtual List WorkerMachines { get; set; } = new(); public static Machine? Create(MachineBindingModel model, FactoryGoWorkDatabase context) { if (model == null) { return null; } return new Machine() { Id = model.Id, Title = model.Title, Country = model.Country, ProductId = model.ProductId, Product = context.Products.FirstOrDefault(x => x.Id == model.ProductId)!, UserId = model.UserId }; } public static Machine Create(MachineViewModel model) { return new Machine { Id = model.Id, Title = model.Title, Country = model.Country, UserId = model.UserId }; } public void Update(MachineBindingModel model) { if (model == null) return; Title = model.Title; Country = model.Country; } public MachineViewModel GetViewModel => new() { Id = Id, Title = Title, Country = Country, UserId = UserId, ProductId = ProductId }; } }