Доделал хранилище для клиента

This commit is contained in:
Leozillo73 2024-04-27 21:41:07 +04:00
parent a4a2923b51
commit 845f442d95
11 changed files with 253 additions and 21 deletions

View File

@ -14,10 +14,10 @@ namespace ZooContracts.BindingModels
public int ClientId { get; set; }
public string RouteName { get; set; } = string.Empty;
public double RoutePrice { get; set; }
public RouteStatus Status { get; set; } = RouteStatus.Неизвестен;
public RouteStatus Status { get; set; } = RouteStatus.Неоплачен;
public DateTime DateStart { get; set; }
public DateTime DateFinish { get; set; }
public Dictionary<int, (IPreserveModel, int)> Preserves
public Dictionary<int, (IPreserveModel, int)> RoutePreserves
{
get;
set;

View File

@ -10,9 +10,10 @@ namespace ZooContracts.SearchModels
public class RouteSearchModel
{
public int? Id { get; set; }
public int? ClientId { get; set; }
public string? RouteName { get; set; } = string.Empty;
public double? RoutePrice { get; set; }
public RouteStatus Status { get; set; } = RouteStatus.Неизвестен;
public RouteStatus Status { get; set; } = RouteStatus.Неоплачен;
public DateTime? DateStart { get; set; }
public DateTime? DateFinish { get; set; }
}

View File

@ -14,8 +14,8 @@ namespace ZooContracts.StorageContracts
List<ClientViewModel> GetFullList();
List<ClientViewModel> GetFilteredList(ClientSearchModel model);
ClientViewModel? GetElement(ClientSearchModel model);
ClientViewModel? Insert(ClientSearchModel model);
ClientViewModel? Update(ClientSearchModel model);
ClientViewModel? Delete(ClientSearchModel model);
ClientViewModel? Insert(ClientBindingModel model);
ClientViewModel? Update(ClientBindingModel model);
ClientViewModel? Delete(ClientBindingModel model);
}
}

View File

@ -14,8 +14,8 @@ namespace ZooContracts.StorageContracts
List<RouteViewModel> GetFullList();
List<RouteViewModel> GetFilteredList(RouteSearchModel model);
RouteViewModel? GetElement(RouteSearchModel model);
RouteViewModel? Insert(RouteSearchModel model);
RouteViewModel? Update(RouteSearchModel model);
RouteViewModel? Delete(RouteSearchModel model);
RouteViewModel? Insert(RouteBindingModel model);
RouteViewModel? Update(RouteBindingModel model);
RouteViewModel? Delete(RouteBindingModel model);
}
}

View File

@ -12,16 +12,22 @@ namespace ZooContracts.ViewModels
{
public class RouteViewModel
{
[DisplayName("Номер")]
public int Id { get; set; }
public int ClientId { get; set; }
[DisplayName("ФИО клиента")]
public string ClientFIO { get; set; } = string.Empty;
[DisplayName("Название маршрута")]
public string RouteName { get; set; } = string.Empty;
[DisplayName("Стоимость маршрута")]
public double RoutePrice { get; set; }
[DisplayName("Статус")]
public RouteStatus Status { get; set; } = RouteStatus.Неизвестен;
public RouteStatus Status { get; set; } = RouteStatus.Неоплачен;
[DisplayName("Дата начала")]
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime DateStart { get; set; }
[DisplayName("Дата окончания")]
public DateTime DateFinish { get; set; }
public Dictionary<int, (IPreserveModel, int)> Preserves
{
get;

View File

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

View File

@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.StorageContracts;
using ZooContracts.ViewModels;
using Microsoft.EntityFrameworkCore;
using ZooContracts.SearchModels;
using ZooContracts.BindingModels;
using System.Threading.Channels;
using ZooDataBaseImplement.Models;
namespace ZooDataBaseImplement.Implements
{
public class RouteStorage : IRouteStorage
{
public List<RouteViewModel> GetFullList()
{
using var context = new ZooDatabase();
return context.Routes
.Include(x => x.Preserves)
.ThenInclude(x => x.Preserve)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<RouteViewModel> GetFilteredList(RouteSearchModel model)
{
if (string.IsNullOrEmpty(model.RouteName))
{
return new();
}
using var context = new ZooDatabase();
return context.Routes
.Include(x => x.Preserves)
.ThenInclude(x => x.Preserve)
.Where(x => x.RouteName.Contains(model.RouteName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public RouteViewModel? GetElement(RouteSearchModel model)
{
if (string.IsNullOrEmpty(model.RouteName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ZooDatabase();
return context.Routes
.Include(x => x.Preserves)
.ThenInclude(x => x.Preserve)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.RouteName) &&
x.RouteName == model.RouteName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public RouteViewModel? Insert(RouteBindingModel model)
{
using var context = new ZooDatabase();
var newCanned = Route.Create(context, model);
if (newCanned == null)
{
return null;
}
context.Routes.Add(newCanned);
context.SaveChanges();
return newCanned.GetViewModel;
}
public RouteViewModel? Update(RouteBindingModel model)
{
using var context = new ZooDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var route = context.Routes.FirstOrDefault(rec =>
rec.Id == model.Id);
if (route == null)
{
return null;
}
route.Update(model);
context.SaveChanges();
transaction.Commit();
return route.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public RouteViewModel? Delete(RouteBindingModel model)
{
using var context = new ZooDatabase();
var element = context.Routes
.Include(x => x.Preserves)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Routes.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -1,6 +1,7 @@
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;
@ -17,7 +18,10 @@ namespace ZooDataBaseImplement.Models
public string PreserveName { get; set; } = string.Empty;
[Required]
public double PreservePrice { get; set; }
public static Preserve? Create(PreserveBindingModel model)
[ForeignKey("PreserveId")]
public virtual List<RoutePreserve> RoutePreserves{ get; set; } =
new();
public static Preserve? Create(PreserveBindingModel model)
{
if (model == null)
{

View File

@ -19,9 +19,9 @@ namespace ZooDataBaseImplement.Models
[Required]
public double RoutePrice { get; set; }
[Required]
public RouteStatus Status { get; private set; } = RouteStatus.Неизвестен;
public RouteStatus Status { get; private set; } = RouteStatus.Неоплачен;
public DateTime DateStart { get; private set; }
public DateTime? DateFinish { get; private set; }
public DateTime DateFinish { get; private set; }
private Dictionary<int, (IPreserveModel, int)>? _routepreserves =
null;
@ -41,6 +41,7 @@ namespace ZooDataBaseImplement.Models
}
[ForeignKey("PreserveID")]
public virtual List<RoutePreserve> Preserves { get; set; } = new();
public static Route Create(ZooDatabase context, RouteBindingModel model)
{
return new Route()
@ -48,19 +49,39 @@ namespace ZooDataBaseImplement.Models
Id = model.Id,
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
RouteName = model.RouteName,
RoutePrice = model.RoutePrice,
Status = model.Status,
DateStart = model.DateStart,
DateFinish = model.DateFinish,
Preserves = model.RoutePreserves.Select(x => new RoutePreserve
{
Preserve = context.Preserves.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PreserveBindingModel model)
public void Update(RouteBindingModel model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateStart = model.DateStart;
DateFinish = model.DateFinish;
}
public PreserveViewModel GetViewModel => new()
public RouteViewModel GetViewModel => new()
{
Id = Id,
ClientId = ClientId,
ClientFIO = Client.ClientFIO,
RouteName = RouteName,
RoutePrice = RoutePrice,
Status = Status,
DateStart= DateStart,
DateFinish=DateFinish,
};
}
}

View File

@ -8,8 +8,8 @@ namespace ZooDataModels.Enums
{
public enum RouteStatus
{
Неизвестен = -1,
Принят = 0,
Завершен =1
Неоплачен = -1,
Неполностью = 0,
Оплачен = 1
}
}

View File

@ -15,6 +15,6 @@ namespace ZooDataModels.Models
RouteStatus Status { get; }
DateTime DateStart { get; }
DateTime DateFinish { get; }
Dictionary<int, (IPreserveModel, int)> Preserves{ get; }
Dictionary<int, (IPreserveModel, int)> RoutePreserves{ get; }
}
}