Upload files to 'ShipyardListImplement'
This commit is contained in:
parent
d6113d4a1b
commit
c1bf24fdac
50
ShipyardListImplement/Ship.cs
Normal file
50
ShipyardListImplement/Ship.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using ShipyardContracts.ViewModels;
|
||||
using ShipyardDataModels.Models;
|
||||
using ShipyardContracts.BindingModels;
|
||||
|
||||
|
||||
namespace ShipyardListImplement.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, (IComponentModel, int)> ShipComponents
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
} = new Dictionary<int, (IComponentModel, int)>();
|
||||
public static Ship? Create(ShipBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Ship()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShipName = model.ShipName,
|
||||
Price = model.Price,
|
||||
ShipComponents = model.ShipComponents
|
||||
};
|
||||
}
|
||||
public void Update(ShipBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ShipName = model.ShipName;
|
||||
Price = model.Price;
|
||||
ShipComponents = model.ShipComponents;
|
||||
}
|
||||
public ShipViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShipName = ShipName,
|
||||
Price = Price,
|
||||
ShipComponents = ShipComponents
|
||||
};
|
||||
}
|
||||
}
|
107
ShipyardListImplement/ShipStorage.cs
Normal file
107
ShipyardListImplement/ShipStorage.cs
Normal file
@ -0,0 +1,107 @@
|
||||
using ShipyardContracts.ViewModels;
|
||||
using ShipyardContracts.SearchModels;
|
||||
using ShipyardContracts.BindingModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ShipyardContracts.StoragesContracts;
|
||||
using ShipyardListImplement.Models;
|
||||
|
||||
namespace ShipyardListImplement
|
||||
{
|
||||
public class ShipStorage : IShipStorage
|
||||
{
|
||||
private readonly DataListSingleton _source;
|
||||
public ShipStorage()
|
||||
{
|
||||
_source = DataListSingleton.GetInstance();
|
||||
}
|
||||
public List<ShipViewModel> GetFullList()
|
||||
{
|
||||
var result = new List<ShipViewModel>();
|
||||
foreach (var Ship in _source.Ships)
|
||||
{
|
||||
result.Add(Ship.GetViewModel);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<ShipViewModel> GetFilteredList(ShipSearchModel model)
|
||||
{
|
||||
var result = new List<ShipViewModel>();
|
||||
if (string.IsNullOrEmpty(model.ShipName))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
foreach (var Ship in _source.Ships)
|
||||
{
|
||||
if (Ship.ShipName.Contains(model.ShipName))
|
||||
{
|
||||
result.Add(Ship.GetViewModel);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public ShipViewModel? GetElement(ShipSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShipName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
foreach (var Ship in _source.Ships)
|
||||
{
|
||||
if ((!string.IsNullOrEmpty(model.ShipName) &&
|
||||
Ship.ShipName == model.ShipName) ||
|
||||
(model.Id.HasValue && Ship.Id == model.Id))
|
||||
{
|
||||
return Ship.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public ShipViewModel? Insert(ShipBindingModel model)
|
||||
{
|
||||
model.Id = 1;
|
||||
foreach (var Ship in _source.Ships)
|
||||
{
|
||||
if (model.Id <= Ship.Id)
|
||||
{
|
||||
model.Id = Ship.Id + 1;
|
||||
}
|
||||
}
|
||||
var newShip = Ship.Create(model);
|
||||
if (newShip == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
_source.Ships.Add(newShip);
|
||||
return newShip.GetViewModel;
|
||||
}
|
||||
public ShipViewModel? Update(ShipBindingModel model)
|
||||
{
|
||||
foreach (var Ship in _source.Ships)
|
||||
{
|
||||
if (Ship.Id == model.Id)
|
||||
{
|
||||
Ship.Update(model);
|
||||
return Ship.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public ShipViewModel? Delete(ShipBindingModel model)
|
||||
{
|
||||
for (int i = 0; i < _source.Ships.Count; ++i)
|
||||
{
|
||||
if (_source.Ships[i].Id == model.Id)
|
||||
{
|
||||
var element = _source.Ships[i];
|
||||
_source.Ships.RemoveAt(i);
|
||||
return element.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
19
ShipyardListImplement/ShipyardListImplement.csproj
Normal file
19
ShipyardListImplement/ShipyardListImplement.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NLog" Version="5.2.8" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\AbstractShopContracts\AbstractShopContracts\AbstractShopContracts.csproj" />
|
||||
<ProjectReference Include="..\..\AbstractShopDataModels\AbstractShopDataModels\ShipyardDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user