Реализация хранилища клиентов в БД + фильтрация заказов по клиентам

This commit is contained in:
Данила Мочалов 2023-03-25 17:16:17 +04:00
parent 300d108c70
commit eeb454ce8a
13 changed files with 219 additions and 8 deletions

View File

@ -11,6 +11,7 @@ namespace LawFirmContracts.BindingModels
public class OrderBindingModel : IOrderModel
{
public int DocumentId { get; set; }
public int ClientId { get; set; }
public int Count { get; set; }

View File

@ -11,5 +11,6 @@ namespace LawFirmContracts.SearchModels
public int? Id { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public int? ClientId { get; set; }
}
}

View File

@ -11,13 +11,16 @@ namespace LawFirmContracts.ViewModels
{
public class OrderViewModel : IOrderModel
{
public int DocumentId { get; set; }
[DisplayName("Номер")]
public int Id { get; set; }
public int DocumentId { get; set; }
[DisplayName("Документ")]
public string DocumentName { get; set; } = string.Empty;
public int ClientId { get; set; }
[DisplayName("ФИО клиента")]
public string ClientFIO { get; set; } = string.Empty;
[DisplayName("Количество")]
public int Count { get; set; }

View File

@ -10,6 +10,7 @@ namespace LawFirmDataModels.Models
public interface IOrderModel : IId
{
int DocumentId { get; }
int ClientId { get; }
int Count { get; }
double Sum { get; }
OrderStatus Status { get; }

View File

@ -0,0 +1,85 @@
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StorageContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmDatabaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new LawFirmDatabase();
return context.Clients.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new LawFirmDatabase();
return context.Clients
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> GetFullList()
{
using var context = new LawFirmDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
using var context = new LawFirmDatabase();
context.Clients.Add(newClient);
context.SaveChanges();
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new LawFirmDatabase();
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new LawFirmDatabase();
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
context.Clients.Remove(client);
context.SaveChanges();
return client.GetViewModel;
}
}
}

View File

@ -26,11 +26,19 @@ namespace LawFirmDatabaseImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue)
{
return new();
}
using var context = new LawFirmDatabase();
if (model.ClientId.HasValue)
{
return context.Orders
.Include(x => x.Client)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Orders
.Include(x => x.Document)
.Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo)

View File

@ -23,6 +23,7 @@ namespace LawFirmDatabaseImplement
public virtual DbSet<Document> Documents { set; get; }
public virtual DbSet<DocumentBlank> DocumentBlanks { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Client> Clients { set; get; }
}
}

View File

@ -0,0 +1,64 @@
using LawFirmContracts.BindingModels;
using LawFirmContracts.ViewModels;
using LawFirmDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmDatabaseImplement.Models
{
public class Client : IClientModel
{
public int Id { get; private set; }
[Required]
public string ClientFIO { get; private set; } = string.Empty;
[Required]
public string Email { get; private set; } = string.Empty;
[Required]
public string Password { get; private 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()
{
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

@ -17,6 +17,8 @@ namespace LawFirmDatabaseImplement.Models
[Required]
public int DocumentId { get; private set; }
[Required]
public int ClientId { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
@ -27,6 +29,7 @@ namespace LawFirmDatabaseImplement.Models
public DateTime? DateImplement { get; private set; }
public virtual Document Document { get; set; }
public virtual Client Client { get; set; }
public static Order? Create(OrderBindingModel? model)
{
@ -37,6 +40,7 @@ namespace LawFirmDatabaseImplement.Models
return new Order
{
DocumentId = model.DocumentId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -59,13 +63,15 @@ namespace LawFirmDatabaseImplement.Models
public OrderViewModel GetViewModel => new()
{
DocumentId = DocumentId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
DocumentName = Document.DocumentName
DocumentName = Document.DocumentName,
ClientFIO = Client.ClientFIO,
};
}
}

View File

@ -32,10 +32,17 @@ namespace LawFirmFileImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue)
{
return new();
}
if (model.ClientId.HasValue)
{
return source.Orders
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
return source.Orders
.Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
@ -91,6 +98,11 @@ namespace LawFirmFileImplement.Implements
{
viewModel.DocumentName = document.DocumentName;
}
var client = source.Clients.FirstOrDefault(x => x.Id!= order.ClientId);
if (client != null)
{
viewModel.ClientFIO = client.ClientFIO;
}
return viewModel;
}

View File

@ -14,6 +14,7 @@ namespace LawFirmFileImplement.Models
public class Order : IOrderModel
{
public int DocumentId { get; private set; }
public int ClientId { get; set; }
public int Count { get; private set; }
@ -36,6 +37,7 @@ namespace LawFirmFileImplement.Models
return new Order
{
DocumentId = model.DocumentId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -55,6 +57,7 @@ namespace LawFirmFileImplement.Models
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DocumentId = Convert.ToInt32(element.Element("DocumentId")!.Value),
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value),
@ -76,6 +79,7 @@ namespace LawFirmFileImplement.Models
public OrderViewModel GetViewModel => new()
{
DocumentId = DocumentId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
@ -88,6 +92,7 @@ namespace LawFirmFileImplement.Models
"Order",
new XAttribute("Id", Id),
new XElement("DocumentId", DocumentId.ToString()),
new XElement("ClientId", ClientId),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),

View File

@ -39,11 +39,24 @@ namespace LawFirmListImplements.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
if (model.DateFrom.HasValue && model.DateTo.HasValue)
{
if (order.Id == model.Id || model.DateFrom <= order.DateCreate && order.DateCreate <= model.DateTo)
foreach (var order in _source.Orders)
{
result.Add(GetViewModel(order));
if (order.Id == model.Id || model.DateFrom <= order.DateCreate && order.DateCreate <= model.DateTo)
{
result.Add(GetViewModel(order));
}
}
}
if (model.ClientId.HasValue)
{
foreach(var order in _source.Orders)
{
if (order.ClientId == model.ClientId)
{
result.Add(GetViewModel(order));
}
}
}
return result;
@ -115,6 +128,14 @@ namespace LawFirmListImplements.Implements
break;
}
}
foreach (var client in _source.Clients)
{
if (client.Id == order.ClientId)
{
viewModel.ClientFIO = client.ClientFIO;
break;
}
}
return viewModel;
}

View File

@ -13,6 +13,7 @@ namespace LawFirmListImplements.Models
public class Order : IOrderModel
{
public int DocumentId { get; private set; }
public int ClientId { get; private set; }
public int Count { get; private set; }
@ -35,6 +36,7 @@ namespace LawFirmListImplements.Models
return new Order
{
DocumentId = model.DocumentId,
ClientId = model.ClientId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -57,6 +59,7 @@ namespace LawFirmListImplements.Models
public OrderViewModel GetViewModel => new()
{
DocumentId = DocumentId,
ClientId = ClientId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,