промежуточная фиксация

This commit is contained in:
Дамир Нугаев 2023-04-12 11:50:00 +04:00
parent 78bf4372db
commit b7bf53620a
24 changed files with 964 additions and 86 deletions

View File

@ -0,0 +1,160 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.BusinessLogicsContracts;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
{
public class ClientLogic : IClientLogic
{
private readonly ILogger _logger;
private readonly IClientStorage _clientStorage;
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage clientStorage)
{
_logger = logger;
_clientStorage = clientStorage;
}
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
{
_logger.LogInformation("ReadList. ClientFIO:{ClientFIO}. Id:{Id}", model?.ClientFIO, model?.Id);
//list хранит весь список в случае, если model пришло со значением null на вход метода
var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ClientViewModel? ReadElement(ClientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadList. ClientFIO:{ClientFIO}. Id:{Id}", model.ClientFIO, model?.Id);
var element = _clientStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ClientBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_clientStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
//проверка входного аргумента для методов Insert, Update и Delete
private void CheckModel(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
//так как при удалении передаём как параметр false
if (!withParams)
{
return;
}
//проверка на наличие ФИО
if (string.IsNullOrEmpty(model.ClientFIO))
{
throw new ArgumentNullException("Отсутствие ФИО в учётной записи", nameof(model.ClientFIO));
}
//проверка на наличие почты
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Отсутствие почты в учётной записи (логина)", nameof(model.Email));
}
//проверка на наличие пароля
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Отсутствие пароля в учётной записи", nameof(model.Password));
}
_logger.LogInformation("WorkPiece. ClientFIO:{ClientFIO}. Email:{Email}. Password:{Password}. Id:{Id}",
model.ClientFIO, model.Email, model.Password, model.Id);
//для проверка на наличие такого же аккаунта
var element = _clientStorage.GetElement(new ClientSearchModel
{
Email = model.Email,
});
//если элемент найден и его Id не совпадает с Id переданного объекта
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Аккаунт с таким логином уже есть");
}
}
}
}

View File

@ -122,7 +122,12 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic
throw new InvalidOperationException("Дата создания должна быть более ранней, нежели дата завершения");
}
_logger.LogInformation("Order. OrderId:{Id}. Sun:{Sum}. ManufactureId:{Id}", model.Id, model.Sum, model.ManufactureId);
if (model.ClientId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.ClientId));
}
_logger.LogInformation("Order. OrderId:{Id}. Sum:{Sum}. ClientId:{ClientId}. ManufactureId:{Id}", model.Id, model.Sum, model.ClientId, model.ManufactureId);
}
//обновление статуса заказа

View File

@ -0,0 +1,20 @@
using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopContracts.BindingModels
{
public class ClientBindingModel : IClientModel
{
public int Id { get; set; }
public string ClientFIO { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
}

View File

@ -13,6 +13,8 @@ namespace BlacksmithWorkshopContracts.BindingModels
{
public int Id { get; set; }
public int ClientId { get; set; }
public int ManufactureId { get; set; }
public int Count { get; set; }
@ -25,4 +27,4 @@ namespace BlacksmithWorkshopContracts.BindingModels
public DateTime? DateImplement { get; set; }
}
}
}

View File

@ -0,0 +1,24 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopContracts.BusinessLogicsContracts
{
public interface IClientLogic
{
List<ClientViewModel>? ReadList(ClientSearchModel? model);
ClientViewModel? ReadElement(ClientSearchModel model);
bool Create(ClientBindingModel model);
bool Update(ClientBindingModel model);
bool Delete(ClientBindingModel model);
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopContracts.SearchModels
{
public class ClientSearchModel
{
public int? Id { get; set; }
public string? ClientFIO { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
}
}

View File

@ -12,6 +12,9 @@ namespace BlacksmithWorkshopContracts.SearchModels
//для поиска по идентификатору
public int? Id { get; set; }
//для поиска по клиенту
public int? ClientId { get; set; }
//два поля для возможности производить выборку
public DateTime? DateFrom { get; set; }

View File

@ -0,0 +1,26 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopContracts.StoragesContracts
{
public interface IClientStorage
{
List<ClientViewModel> GetFullList();
List<ClientViewModel> GetFilteredList(ClientSearchModel model);
ClientViewModel? GetElement(ClientSearchModel model);
ClientViewModel? Insert(ClientBindingModel model);
ClientViewModel? Update(ClientBindingModel model);
ClientViewModel? Delete(ClientBindingModel model);
}
}

View File

@ -0,0 +1,25 @@
using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopContracts.ViewModels
{
public class ClientViewModel : IClientModel
{
public int Id { get; set; }
[DisplayName("ФИО клиента")]
public string ClientFIO { get; set; } = string.Empty;
[DisplayName("Логин (эл. почта)")]
public string Email { get; set; } = string.Empty;
[DisplayName("Пароль")]
public string Password { get; set; } = string.Empty;
}
}

View File

@ -15,8 +15,13 @@ namespace BlacksmithWorkshopContracts.ViewModels
[DisplayName("Номер")]
public int Id { get; set; }
public int ClientId { get; set; }
public int ManufactureId { get; set; }
[DisplayName("ФИО клиента")]
public string ClientFIO { get; set; } = string.Empty;
[DisplayName("Изделие")]
public string ManufactureName { get; set; } = string.Empty;

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopDataModels.Models
{
public interface IClientModel : IId
{
string ClientFIO { get; }
string Email { get; }
string Password { get; }
}
}

View File

@ -0,0 +1,130 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopDatabaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var element = context.Clients
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
// для отображения КОРРЕКТНОЙ ViewModel-и
var deletedElement = context.Clients
.Include(x => x.Orders)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Clients.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
using var context = new BlacksmithWorkshopDatabase();
if (model.Id.HasValue)
{
return context.Clients
.Include(x => x.Orders)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password))
{
return context.Clients
.Include(x => x.Orders)
.FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password))
?.GetViewModel;
}
return new();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.ClientFIO))
{
return new();
}
using var context = new BlacksmithWorkshopDatabase();
return context.Clients
.Include(x => x.Orders)
.Where(x => x.ClientFIO.Contains(model.ClientFIO))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> GetFullList()
{
using var context = new BlacksmithWorkshopDatabase();
return context.Clients
.Include(x => x.Orders)
.Select(x => x.GetViewModel)
.ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
using var context = new BlacksmithWorkshopDatabase();
context.Clients.Add(newClient);
context.SaveChanges();
return context.Clients
.Include(x => x.Orders)
.FirstOrDefault(x => x.Id == newClient.Id)
?.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var order = context.Clients
.Include(x => x.Orders)
.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Clients
.Include(x => x.Orders)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
}
}

View File

@ -17,15 +17,23 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var element = context.Orders
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
// для отображения КОРРЕКТНОЙ ViewModel-и
var deletedElement = context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
return deletedElement;
}
return null;
@ -41,24 +49,45 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
using var context = new BlacksmithWorkshopDatabase();
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new BlacksmithWorkshopDatabase();
return context.Orders
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
{
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.Id.HasValue)
{
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.ClientId.HasValue)
{
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public List<OrderViewModel> GetFullList()
@ -66,9 +95,10 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
using var context = new BlacksmithWorkshopDatabase();
return context.Orders
.Include(x => x.Manufacture)
.Select(x => x.GetViewModel)
.ToList();
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
@ -81,17 +111,24 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
}
using var context = new BlacksmithWorkshopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders.Include(x => x.Manufacture)
.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
var order = context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
@ -101,7 +138,11 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopDatabaseImplement.Models
{
internal class Client
{
}
}

View File

@ -8,6 +8,7 @@ using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace BlacksmithWorkshopDatabaseImplement.Models
{
@ -18,6 +19,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
[Required]
public int ManufactureId { get; private set; }
[Required]
public int ClientId { get; private set; }
[Required]
public int Count { get; private set; }
@ -35,6 +39,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
//для передачи названия изделия
public virtual Manufacture Manufacture { get; set; }
//для передачи имени клиента
public virtual Client Client { get; set; }
public static Order? Create(OrderBindingModel model)
{
if (model == null)
@ -46,6 +53,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
{
Id = model.Id,
ManufactureId = model.ManufactureId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -69,12 +77,14 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
{
Id = Id,
ManufactureId = ManufactureId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
ManufactureName = Manufacture.ManufactureName
ManufactureName = Manufacture.ManufactureName,
ClientFIO = Client.ClientFIO
};
}
}

View File

@ -0,0 +1,90 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopFileImplement.Implements
{
public class ClientStorage : IClientStorage
{
private readonly DataFileSingleton source;
public ClientStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ClientViewModel> GetFullList()
{
return source.Clients.Select(x => x.GetViewModel).ToList();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
return source.Clients.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return source.Clients.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ClientViewModel? Insert(ClientBindingModel model)
{
model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => x.Id) + 1 : 1;
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
source.Clients.Add(newClient);
source.SaveClients();
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
var client = source.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
source.SaveClients();
return client.GetViewModel;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
var element = source.Clients.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Clients.Remove(element);
source.SaveClients();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -28,12 +28,28 @@ namespace BlacksmithWorkshopFileImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
{
return new();
return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => GetViewModel(x))
.ToList();
}
else if (model.Id.HasValue)
{
return source.Orders
.Where(x => x.Id == model.Id)
.Select(x => GetViewModel(x))
.ToList();
}
else if (model.ClientId.HasValue)
{
return source.Orders
.Where(x => x.ClientId == model.ClientId)
.Select(x => GetViewModel(x))
.ToList();
}
return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList();
return new();
}
public OrderViewModel? GetElement(OrderSearchModel model)
@ -53,11 +69,18 @@ namespace BlacksmithWorkshopFileImplement.Implements
var manufacture = source.Manufactures.FirstOrDefault(x => x.Id == order.ManufactureId);
var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
if (manufacture != null)
{
viewModel.ManufactureName = manufacture.ManufactureName;
}
if (client != null)
{
viewModel.ClientFIO = client.ClientFIO;
}
return viewModel;
}

View File

@ -0,0 +1,84 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Enums;
using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace BlacksmithWorkshopFileImplement.Models
{
public class Client : IClientModel
{
public int Id { get; private set; }
public string ClientFIO { get; private set; } = string.Empty;
public string Email { get; private set; } = string.Empty;
public string Password { get; private set; } = string.Empty;
public static Client? Create(ClientBindingModel model)
{
if (model == null)
{
return null;
}
return new Client()
{
Id = model.Id,
ClientFIO = model.ClientFIO,
Email = model.Email,
Password = model.Password
};
}
public static Client? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Client()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ClientFIO = element.Element("ClientFIO")!.Value,
Email = element.Element("Email")!.Value,
Password = element.Element("Password")!.Value
};
}
public void Update(ClientBindingModel model)
{
if (model == null)
{
return;
}
ClientFIO = model.ClientFIO;
Email = model.Email;
Password = model.Password;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
ClientFIO = ClientFIO,
Email = Email,
Password = Password
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("ClientFIO", ClientFIO),
new XElement("Email", Email),
new XElement("Password", Password));
}
}

View File

@ -17,6 +17,8 @@ namespace BlacksmithWorkshopFileImplement.Models
{
public int Id { get; private set; }
public int ClientId { get; private set; }
public int ManufactureId { get; private set; }
public int Count { get; private set; }
@ -39,6 +41,7 @@ namespace BlacksmithWorkshopFileImplement.Models
return new Order()
{
Id = model.Id,
ClientId = model.ClientId,
ManufactureId = model.ManufactureId,
Count = model.Count,
Sum = model.Sum,
@ -58,6 +61,7 @@ namespace BlacksmithWorkshopFileImplement.Models
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ClientId = Convert.ToInt32(element.Attribute("Id")!.Value),
ManufactureId = Convert.ToInt32(element.Element("ManufactureId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
@ -82,6 +86,7 @@ namespace BlacksmithWorkshopFileImplement.Models
public OrderViewModel GetViewModel => new()
{
Id = Id,
ClientId = ClientId,
ManufactureId = ManufactureId,
Count = Count,
Sum = Sum,
@ -90,9 +95,9 @@ namespace BlacksmithWorkshopFileImplement.Models
DateImplement = DateImplement
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("ClientId", ClientId.ToString()),
new XElement("ManufactureId", ManufactureId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),

View File

@ -0,0 +1,130 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopListImplement.Implements
{
public class ClientStorage : IClientStorage
{
//поле для работы со списком изделий
private readonly DataListSingleton _source;
public ClientStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<ClientViewModel> GetFullList()
{
var result = new List<ClientViewModel>();
foreach (var client in _source.Clients)
{
result.Add(client.GetViewModel);
}
return result;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
var result = new List<ClientViewModel>();
if (string.IsNullOrEmpty(model.Email))
{
return result;
}
foreach (var client in _source.Clients)
{
if (client.Email.Contains(model.Email))
{
result.Add(client.GetViewModel);
}
}
return result;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
foreach (var client in _source.Clients)
{
if ((!string.IsNullOrEmpty(model.Email) && client.Email == model.Email) ||
(model.Id.HasValue && client.Id == model.Id))
{
return client.GetViewModel;
}
}
return null;
}
public ClientViewModel? Insert(ClientBindingModel model)
{
model.Id = 1;
foreach (var client in _source.Clients)
{
if (model.Id <= client.Id)
{
model.Id = client.Id + 1;
}
}
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
_source.Clients.Add(newClient);
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
foreach (var client in _source.Clients)
{
if (client.Id == model.Id)
{
client.Update(model);
return client.GetViewModel;
}
}
return null;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
for (int i = 0; i < _source.Clients.Count; ++i)
{
if (_source.Clients[i].Id == model.Id)
{
var element = _source.Clients[i];
_source.Clients.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}

View File

@ -39,22 +39,46 @@ namespace BlacksmithWorkshopListImplement.Implements
//получение отфильтрованного списка заказов
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
if (!model.Id.HasValue)
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo)
{
result.Add(GetViewModel(order));
}
}
return result;
}
else if (model.Id.HasValue)
{
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
{
return new() { GetViewModel(order) };
}
}
}
else if (model.ClientId.HasValue)
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
if (order.ClientId == model.ClientId)
{
result.Add(GetViewModel(order));
}
}
return result;
}
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
{
result.Add(GetViewModel(order));
}
}
return result;
return new();
}
//получение элемента из списка заказов
@ -91,6 +115,15 @@ namespace BlacksmithWorkshopListImplement.Implements
}
}
foreach (var client in _source.Clients)
{
if (client.Id == order.ClientId)
{
viewModel.ClientFIO = client.ClientFIO;
break;
}
}
return viewModel;
}

View File

@ -0,0 +1,62 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopListImplement.Models
{
public class Client : IClientModel
{
//методы set делаем приватным, чтобы исключить неразрешённые манипуляции
public int Id { get; private set; }
public string ClientFIO { get; private set; } = string.Empty;
public string Email { get; private set; } = string.Empty;
public string Password { get; private set; } = string.Empty;
//метод для создания объекта от класса-компонента на основе класса-BindingModel
public static Client? Create(ClientBindingModel? model)
{
if (model == null)
{
return null;
}
return new Client()
{
Id = model.Id,
ClientFIO = model.ClientFIO,
Email = model.Email,
Password = model.Password
};
}
//метод изменения существующего объекта
public void Update(ClientBindingModel? model)
{
if (model == null)
{
return;
}
ClientFIO = model.ClientFIO;
Email = model.Email;
Password = model.Password;
}
//метод для создания объекта класса ViewModel на основе данных объекта класса-компонента
public ClientViewModel GetViewModel => new()
{
Id = Id,
ClientFIO = ClientFIO,
Email = Email,
Password = Password
};
}
}

View File

@ -17,6 +17,8 @@ namespace BlacksmithWorkshopListImplement.Models
//методы set сделали приватными, чтобы исключить неразрешённые манипуляции
public int Id { get; private set; }
public int ClientId { get; private set; }
public int ManufactureId { get; private set; }
public int Count { get; private set; }
@ -40,6 +42,7 @@ namespace BlacksmithWorkshopListImplement.Models
{
Id = model.Id,
ManufactureId = model.ManufactureId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -55,6 +58,7 @@ namespace BlacksmithWorkshopListImplement.Models
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
@ -64,6 +68,7 @@ namespace BlacksmithWorkshopListImplement.Models
{
Id = Id,
ManufactureId = ManufactureId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,

View File

@ -1,53 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<Body>
<ReportItems>
<Textbox Name="Textbox1">
<CanGrow>true</CanGrow>
<KeepTogether>true</KeepTogether>
<Paragraphs>
<Paragraph>
<TextRuns>
<TextRun>
<Value>Заказы</Value>
<Style />
</TextRun>
</TextRuns>
<Style>
<TextAlign>Center</TextAlign>
</Style>
</Paragraph>
</Paragraphs>
<rd:DefaultName>Textbox1</rd:DefaultName>
<Height>0.95137cm</Height>
<Width>16.51cm</Width>
<Style>
<Border>
<Style>None</Style>
</Border>
<VerticalAlign>Middle</VerticalAlign>
<PaddingLeft>2pt</PaddingLeft>
<PaddingRight>2pt</PaddingRight>
<PaddingTop>2pt</PaddingTop>
<PaddingBottom>2pt</PaddingBottom>
</Style>
</Textbox>
</ReportItems>
<Height>2in</Height>
<Style />
</Body>
<Width>6.5in</Width>
<Page>
<PageHeight>29.7cm</PageHeight>
<PageWidth>21cm</PageWidth>
<LeftMargin>2cm</LeftMargin>
<RightMargin>2cm</RightMargin>
<TopMargin>2cm</TopMargin>
<BottomMargin>2cm</BottomMargin>
<ColumnSpacing>0.13cm</ColumnSpacing>
<Style />
</Page>
<AutoRefresh>0</AutoRefresh>
<rd:ReportUnitType>Cm</rd:ReportUnitType>
<rd:ReportID>5a21f7f6-f6f5-4e1a-a42c-cf0e50086cd7</rd:ReportID>
</Report>