Upload files to 'ShipyardFileImplement'

This commit is contained in:
Ivan_Starostin 2024-04-27 19:10:30 +04:00
parent eb47697811
commit 496710f2ac
3 changed files with 207 additions and 0 deletions

View File

@ -0,0 +1,95 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ShipyardFileImplement.Models
{
public class Ship : IShipModel
{
public int Id { get; private set; }
public string ShipName { 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)>? _ShipComponents = null;
public Dictionary<int, (IComponentModel, int)> ShipComponents
{
get
{
if (_ShipComponents == null)
{
var source = DataFileSingleton.GetInstance();
_ShipComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
y.Value));
}
return _ShipComponents;
}
}
public static Ship? Create(ShipBindingModel model)
{
if (model == null)
{
return null;
}
return new Ship()
{
Id = model.Id,
ShipName = model.ShipName,
Price = model.Price,
Components = model.ShipComponents.ToDictionary(x => x.Key, x
=> x.Value.Item2)
};
}
public static Ship? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Ship()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShipName = element.Element("ShipName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components =
element.Element("ShipComponents")!.Elements("ShipComponent")
.ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ShipBindingModel model)
{
if (model == null)
{
return;
}
ShipName = model.ShipName;
Price = model.Price;
Components = model.ShipComponents.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_ShipComponents = null;
}
public ShipViewModel GetViewModel => new()
{
Id = Id,
ShipName = ShipName,
Price = Price,
ShipComponents = ShipComponents
};
public XElement GetXElement => new("Ship",
new XAttribute("Id", Id),
new XElement("ShipName", ShipName),
new XElement("Price", Price.ToString()),
new XElement("ShipComponents", Components.Select(x =>
new XElement("ShipComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -0,0 +1,89 @@
using ShipyardContracts.BindingModels;
using ShipyardContracts.SearchModels;
using ShipyardContracts.StoragesContracts;
using ShipyardContracts.ViewModels;
using ShipyardFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShipyardFileImplement.Implements
{
public class ShipStorage : IShipStorage
{
private readonly DataFileSingleton source;
public ShipStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ShipViewModel> GetFullList()
{
return source.Ships
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShipViewModel> GetFilteredList(ShipSearchModel model)
{
if (string.IsNullOrEmpty(model.ShipName))
{
return new();
}
return source.Ships
.Where(x => x.ShipName.Contains(model.ShipName))
.Select(x => x.GetViewModel)
.ToList();
}
public ShipViewModel? GetElement(ShipSearchModel model)
{
if (string.IsNullOrEmpty(model.ShipName) && !model.Id.HasValue)
{
return null;
}
return source.Ships
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ShipName) && x.ShipName ==
model.ShipName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ShipViewModel? Insert(ShipBindingModel model)
{
model.Id = source.Ships.Count > 0 ? source.Ships.Max(x => x.Id) + 1 : 1;
var newShip = Ship.Create(model);
if (newShip == null)
{
return null;
}
source.Ships.Add(newShip);
source.SaveShips();
return newShip.GetViewModel;
}
public ShipViewModel? Update(ShipBindingModel model)
{
var Ship = source.Ships.FirstOrDefault(x => x.Id ==
model.Id);
if (Ship == null)
{
return null;
}
Ship.Update(model);
source.SaveShips();
return Ship.GetViewModel;
}
public ShipViewModel? Delete(ShipBindingModel model)
{
var Ship = source.Ships.FirstOrDefault(x => x.Id ==
model.Id);
if (Ship != null)
{
source.Ships.Remove(Ship);
source.SaveShips();
return Ship.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AbstractShopContracts\AbstractShopContracts\AbstractShopContracts.csproj" />
<ProjectReference Include="..\..\AbstractShopDataModels\AbstractShopDataModels\ShipyardDataModels.csproj" />
</ItemGroup>
</Project>