database implement

This commit is contained in:
ShabOl 2024-04-11 00:47:41 +04:00
parent e65f441c79
commit edfe10dd44
5 changed files with 181 additions and 6 deletions

View File

@ -25,5 +25,7 @@ namespace AutoWorkshopDatabaseImplement
public virtual DbSet<RepairComponent> RepairComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Client> Clients { set; get; }
}
}

View File

@ -0,0 +1,94 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.SearchModels;
using AutoWorkshopContracts.StoragesContracts;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDatabaseImplement.Models;
namespace AutoWorkshopDatabaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public List<ClientViewModel> GetFullList()
{
using var Context = new AutoWorkshopDatabase();
return Context.Clients
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel Model)
{
if (string.IsNullOrEmpty(Model.ClientFIO) && string.IsNullOrEmpty(Model.Email) && string.IsNullOrEmpty(Model.Password))
return new();
using var Context = new AutoWorkshopDatabase();
return Context.Clients
.Where(x => (string.IsNullOrEmpty(Model.ClientFIO) || x.ClientFIO.Contains(Model.ClientFIO) &&
(string.IsNullOrEmpty(Model.Email) || x.Email.Contains(Model.Email)) &&
(string.IsNullOrEmpty(Model.Password) || x.Password.Contains(Model.Password))))
.Select(x => x.GetViewModel)
.ToList();
}
public ClientViewModel? GetElement(ClientSearchModel Model)
{
if (string.IsNullOrEmpty(Model.ClientFIO) && string.IsNullOrEmpty(Model.Email) && string.IsNullOrEmpty(Model.Password) &&
!Model.Id.HasValue)
{
return null;
}
using var Context = new AutoWorkshopDatabase();
return Context.Clients
.FirstOrDefault(x => (string.IsNullOrEmpty(Model.ClientFIO) || x.ClientFIO == Model.ClientFIO) &&
(!Model.Id.HasValue || x.Id == Model.Id) && (string.IsNullOrEmpty(Model.Email) || x.Email == Model.Email) &&
(string.IsNullOrEmpty(Model.Password) || x.Password == Model.Password))?
.GetViewModel;
}
public ClientViewModel? Insert(ClientBindingModel Model)
{
var NewClient = Client.Create(Model);
if (NewClient == null)
return null;
using var Context = new AutoWorkshopDatabase();
Context.Clients.Add(NewClient);
Context.SaveChanges();
return NewClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel Model)
{
using var Context = new AutoWorkshopDatabase();
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 AutoWorkshopDatabase();
var Element = Context.Clients.FirstOrDefault(rec => rec.Id == Model.Id);
if (Element == null)
return null;
Context.Clients.Remove(Element);
Context.SaveChanges();
return Element.GetViewModel;
}
}
}

View File

@ -4,7 +4,6 @@ using AutoWorkshopContracts.StoragesContracts;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
namespace AutoWorkshopDatabaseImplement.Implements
{
@ -16,6 +15,7 @@ namespace AutoWorkshopDatabaseImplement.Implements
return Context.Orders
.Include(x => x.Repair)
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
@ -31,14 +31,16 @@ namespace AutoWorkshopDatabaseImplement.Implements
{
return Context.Orders
.Include(x => x.Repair)
.Where(x => x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo)
.Include(x => x.Client)
.Where(x => x.DateCreate >= Model.DateFrom && x.DateCreate <= Model.DateTo && (!Model.ClientId.HasValue || x.ClientId == Model.ClientId))
.Select(x => x.GetViewModel)
.ToList();
}
return Context.Orders
.Include(x => x.Repair)
.Where(x => x.Id == Model.Id)
.Include(x => x.Client)
.Where(x => x.Id == Model.Id && (!Model.ClientId.HasValue || x.ClientId == Model.ClientId))
.Select(x => x.GetViewModel)
.ToList();
}
@ -52,6 +54,7 @@ namespace AutoWorkshopDatabaseImplement.Implements
return Context.Orders
.Include(x => x.Repair)
.Include(x => x.Client)
.FirstOrDefault(x => Model.Id.HasValue && x.Id == Model.Id)?
.GetViewModel;
}
@ -70,7 +73,7 @@ namespace AutoWorkshopDatabaseImplement.Implements
Context.Orders.Add(NewOrder);
Context.SaveChanges();
return Context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == NewOrder.Id)?.GetViewModel;
return Context.Orders.Include(x => x.Repair).Include(x => x.Client).FirstOrDefault(x => x.Id == NewOrder.Id)?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel Model)
@ -84,13 +87,13 @@ namespace AutoWorkshopDatabaseImplement.Implements
Order.Update(Model);
Context.SaveChanges();
return Context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel;
return Context.Orders.Include(x => x.Repair).Include(x => x.Client).FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel Model)
{
using var Context = new AutoWorkshopDatabase();
var Order = Context.Orders.FirstOrDefault(rec => rec.Id == Model.Id);
var Order = Context.Orders.Include(x => x.Repair).Include(x => x.Client).FirstOrDefault(rec => rec.Id == Model.Id);
if (Order == null)
return null;

View File

@ -0,0 +1,68 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AutoWorkshopDatabaseImplement.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; 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

@ -14,7 +14,12 @@ namespace AutoWorkshopDatabaseImplement.Models
public int RepairId { get; private set; }
public virtual Repair Repair { get; set; }
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; set; }
[Required]
public int Count { get; private set; }
@ -38,6 +43,7 @@ namespace AutoWorkshopDatabaseImplement.Models
{
Id = Model.Id,
RepairId = Model.RepairId,
ClientId = Model.ClientId,
Count = Model.Count,
Sum = Model.Sum,
Status = Model.Status,
@ -59,7 +65,9 @@ namespace AutoWorkshopDatabaseImplement.Models
{
Id = Id,
RepairId = RepairId,
ClientId = ClientId,
RepairName = Repair.RepairName,
ClientFIO = Client.ClientFIO,
Count = Count,
Sum = Sum,
Status = Status,