Romanov E.V. SecuritySystem Lab_Work_1 #1
102
SecuritySystemListImplement/Implements/SecureStorage.cs
Normal file
102
SecuritySystemListImplement/Implements/SecureStorage.cs
Normal file
@ -0,0 +1,102 @@
|
||||
using SecuritySystemListImplement.Models;
|
||||
using SecuritySystemContracts.BindingModels;
|
||||
using SecuritySystemContracts.SearchModels;
|
||||
using SecuritySystemContracts.StoragesContracts;
|
||||
using SecuritySystemContracts.ViewModels;
|
||||
|
||||
|
||||
namespace SecuritySystemListImplement.Implements
|
||||
{
|
||||
public class SecureStorage : IManufactureStorage
|
||||
{
|
||||
private readonly DataListSingleton _source;
|
||||
public SecureStorage()
|
||||
{
|
||||
_source = DataListSingleton.GetInstance();
|
||||
}
|
||||
public List<ManufactureViewModel> GetFullList()
|
||||
{
|
||||
var result = new List<ManufactureViewModel>();
|
||||
foreach (var Manufacture in _source.Manufactures)
|
||||
{
|
||||
result.Add(Manufacture.GetViewModel);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<ManufactureViewModel> GetFilteredList(ManufactureSearchModel model)
|
||||
{
|
||||
var result = new List<ManufactureViewModel>();
|
||||
if (string.IsNullOrEmpty(model.ManufactureName))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
foreach (var Manufacture in _source.Manufactures)
|
||||
{
|
||||
if (Manufacture.ManufactureName.Contains(model.ManufactureName))
|
||||
{
|
||||
result.Add(Manufacture.GetViewModel);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public ManufactureViewModel? GetElement(ManufactureSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ManufactureName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
foreach (var Manufacture in _source.Manufactures)
|
||||
{
|
||||
if ((!string.IsNullOrEmpty(model.ManufactureName) && Manufacture.ManufactureName == model.ManufactureName) ||
|
||||
(model.Id.HasValue && Manufacture.Id == model.Id))
|
||||
{
|
||||
return Manufacture.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public ManufactureViewModel? Insert(ManufactureBindingModel model)
|
||||
{
|
||||
model.Id = 1;
|
||||
foreach (var Manufacture in _source.Manufactures)
|
||||
{
|
||||
if (model.Id <= Manufacture.Id)
|
||||
{
|
||||
model.Id = Manufacture.Id + 1;
|
||||
}
|
||||
}
|
||||
var newManufacture = Secure.Create(model);
|
||||
if (newManufacture == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
_source.Manufactures.Add(newManufacture);
|
||||
return newManufacture.GetViewModel;
|
||||
}
|
||||
public ManufactureViewModel? Update(ManufactureBindingModel model)
|
||||
{
|
||||
foreach (var Manufacture in _source.Manufactures)
|
||||
{
|
||||
if (Manufacture.Id == model.Id)
|
||||
{
|
||||
Manufacture.Update(model);
|
||||
return Manufacture.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public ManufactureViewModel? Delete(ManufactureBindingModel model)
|
||||
{
|
||||
for (int i = 0; i < _source.Manufactures.Count; ++i)
|
||||
{
|
||||
if (_source.Manufactures[i].Id == model.Id)
|
||||
{
|
||||
var element = _source.Manufactures[i];
|
||||
_source.Manufactures.RemoveAt(i);
|
||||
return element.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
55
SecuritySystemListImplement/Models/Secure.cs
Normal file
55
SecuritySystemListImplement/Models/Secure.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using SecuritySystemContracts.BindingModels;
|
||||
using SecuritySystemContracts.ViewModels;
|
||||
using SecuritySystemDataModels.Models;
|
||||
|
||||
namespace SecuritySystemListImplement.Models
|
||||
{
|
||||
public class Secure : IManufactureModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public string ManufactureName { get; private set; } = string.Empty;
|
||||
public double Price { get; private set; }
|
||||
public Dictionary<int, (IComponentModel, int)> ManufactureComponents
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
} = new Dictionary<int, (IComponentModel, int)>();
|
||||
public static Secure? Create(ManufactureBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Secure()
|
||||
{
|
||||
Id = model.Id,
|
||||
ManufactureName = model.ManufactureName,
|
||||
Price = model.Price,
|
||||
ManufactureComponents = model.ManufactureComponents
|
||||
};
|
||||
}
|
||||
public void Update(ManufactureBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ManufactureName = model.ManufactureName;
|
||||
Price = model.Price;
|
||||
ManufactureComponents = model.ManufactureComponents;
|
||||
}
|
||||
public ManufactureViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ManufactureName = ManufactureName,
|
||||
Price = Price,
|
||||
ManufactureComponents = ManufactureComponents
|
||||
};
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user