Настройка OrderStorage для работы с учётом появления клиента в БД.

This commit is contained in:
Programmist73 2023-03-27 10:28:20 +04:00
parent fadef43698
commit 47c32113a7
9 changed files with 288 additions and 42 deletions

View File

@ -15,10 +15,13 @@ namespace BlacksmithWorkshopContracts.ViewModels
[DisplayName("Номер")]
public int Id { get; set; }
public int ClientId { 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

@ -28,5 +28,7 @@ namespace BlacksmithWorkshopDatabaseImplement
public virtual DbSet<ManufactureWorkPiece> ManufactureWorkPieces { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Client> Clients { get; set; }
}
}

View File

@ -2,6 +2,8 @@
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -14,32 +16,115 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
{
public ClientViewModel? Delete(ClientBindingModel model)
{
throw new NotImplementedException();
}
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)
{
throw new NotImplementedException();
}
using var context = new BlacksmithWorkshopDatabase();
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
throw new NotImplementedException();
}
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()
{
throw new NotImplementedException();
}
using var context = new BlacksmithWorkshopDatabase();
return context.Clients
.Include(x => x.Orders)
.Select(x => x.GetViewModel)
.ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
throw new NotImplementedException();
}
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)
{
throw new NotImplementedException();
}
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

@ -49,7 +49,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
.Include(x => x.Client)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
@ -66,16 +67,31 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
{
return context.Orders
.Include(x => x.Manufacture)
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.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 context.Orders
.Include(x => x.Manufacture)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
return new();
}
public List<OrderViewModel> GetFullList()
@ -84,6 +100,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
@ -104,7 +121,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.FirstOrDefault(x => x.Id == newOrder.Id)
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
}
@ -123,7 +141,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
return context.Orders
.Include(x => x.Manufacture)
.FirstOrDefault(x => x.Id == model.Id)
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
}

View File

@ -0,0 +1,78 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopDatabaseImplement.Models
{
public class Client : IClientModel
{
public int Id { get; set; }
[Required]
public string ClientFIO { get; set; } = string.Empty;
[Required]
public string Email { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
//для реализации связи многие ко многим с заказами (так как клиенты могу сделать одинаковый заказ)
[ForeignKey("ClientId")]
public virtual List<Order> Orders { get; set; } = new();
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(ClientViewModel model)
{
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;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
ClientFIO = ClientFIO,
Email = Email,
Password = Password
};
}
}

View File

@ -19,7 +19,10 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
[Required]
public int ManufactureId { get; private set; }
[Required]
[Required]
public int ClientId { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
@ -36,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)
@ -47,6 +53,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
{
Id = model.Id,
ManufactureId = model.ManufactureId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -70,6 +77,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
{
Id = Id,
ManufactureId = ManufactureId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,
@ -77,5 +85,5 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
DateImplement = DateImplement,
ManufactureName = Manufacture.ManufactureName
};
}
}
}

View File

@ -34,8 +34,22 @@ namespace BlacksmithWorkshopFileImplement.Implements
.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)
@ -55,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

@ -39,11 +39,11 @@ namespace BlacksmithWorkshopListImplement.Implements
//получение отфильтрованного списка заказов
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
{
foreach (var order in _source.Orders)
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo)
{
@ -53,17 +53,33 @@ namespace BlacksmithWorkshopListImplement.Implements
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.Id == model.Id)
{
result.Add(GetViewModel(order));
}
}
foreach (var order in _source.Orders)
{
if (order.ClientId == model.ClientId)
{
result.Add(GetViewModel(order));
}
}
return result;
}
return result;
}
return new();
}
//получение элемента из списка заказов
public OrderViewModel? GetElement(OrderSearchModel model)
@ -99,7 +115,16 @@ namespace BlacksmithWorkshopListImplement.Implements
}
}
return viewModel;
foreach (var client in _source.Clients)
{
if (client.Id == order.ClientId)
{
viewModel.ClientFIO = client.ClientFIO;
break;
}
}
return viewModel;
}
//при создании заказа определяем для него новый id: ищем max id и прибавляем к нему 1

View File

@ -17,7 +17,9 @@ namespace BlacksmithWorkshopListImplement.Models
//методы set сделали приватными, чтобы исключить неразрешённые манипуляции
public int Id { get; private set; }
public int ManufactureId { 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,11 +68,12 @@ namespace BlacksmithWorkshopListImplement.Models
{
Id = Id,
ManufactureId = ManufactureId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}
}