This commit is contained in:
bulatova_karina 2024-03-25 21:23:46 +04:00
parent 2b581d2ad8
commit c32c65f4a5

View File

@ -16,8 +16,7 @@ namespace ComputersShopFileImplement.Models
public string ComputerName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _computerComponents =
null;
private Dictionary<int, (IComponentModel, int)>? _computerComponents = null;
public Dictionary<int, (IComponentModel, int)> ComputerComponents
{
get
@ -43,8 +42,7 @@ namespace ComputersShopFileImplement.Models
Id = model.Id,
ComputerName = model.ComputerName,
Price = model.Price,
Components = model.ComputerComponents.ToDictionary(x => x.Key, x
=> x.Value.Item2)
Components = model.ComputerComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Computer? Create(XElement element)
@ -58,8 +56,11 @@ namespace ComputersShopFileImplement.Models
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComputerName = element.Element("ComputerName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("ComputerComponents")!.Elements("ComputerComponent")
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
Components =
element.Element("ComputerComponents")!.Elements("ComputerComponent")
.ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ComputerBindingModel model)
@ -70,8 +71,7 @@ namespace ComputersShopFileImplement.Models
}
ComputerName = model.ComputerName;
Price = model.Price;
Components = model.ComputerComponents.ToDictionary(x => x.Key, x =>
x.Value.Item2);
Components = model.ComputerComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_computerComponents = null;
}
public ComputerViewModel GetViewModel => new()
@ -82,17 +82,12 @@ namespace ComputersShopFileImplement.Models
ComputerComponents = ComputerComponents
};
public XElement GetXElement => new("Computer",
new XAttribute("Id", Id),
new XElement("ComputerName", ComputerName),
new XElement("Price", Price.ToString()),
new XElement("ComputerComponents", Components.Select(x =>
new XElement("ComputerComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value)))
.ToArray()));
new XAttribute("Id", Id),
new XElement("ComputerName", ComputerName),
new XElement("Price", Price.ToString()),
new XElement("ComputerComponents", Components.Select(x =>
new XElement("ComputerComponent", new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}