Заполнение FurnitureFactoryDataBaseImplement Implementы

This commit is contained in:
FLARJ 2023-04-07 17:42:34 +04:00
parent 1d680a34ff
commit b6f8cf61d3
10 changed files with 334 additions and 2 deletions

View File

@ -4,6 +4,7 @@ namespace FurnitureContracts.BindingModels
{
public class OrdersBindingModel : IOrdersModel
{
public string Title { get; set; } = string.Empty;
public DateTime Date { get; set; } = DateTime.Now;
public string Status { get; set; } = string.Empty;
public int Id { get; set; }

View File

@ -9,5 +9,9 @@ namespace FurnitureContracts.SearchModels
public class OrderSearchModel
{
public int? Id { get; set; }
public int? ManagerId { get; set; }
public string? Title { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}
}

View File

@ -11,5 +11,7 @@ namespace FurnitureContracts.SearchModels
public string? Name { get; set; }
public int? Id { get; set; }
public int? ManagerId { get; set; }
}
}

View File

@ -10,6 +10,10 @@ namespace FurnitureContracts.ViewModel
{
public class OrdersViewModel : IOrdersModel
{
[DisplayName("Название")]
public string Title { get; set; } = string.Empty;
[DisplayName("Дата заказа")]
public DateTime Date { get; set; }

View File

@ -63,8 +63,6 @@ namespace FurnitureFactoryDataBaseImplements.Implements
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public HeadsetViewModel? GetElement(HeadsetSearchModel model)

View File

@ -0,0 +1,105 @@
using Microsoft.EntityFrameworkCore;
using FurnitureContracts.BindingModels;
using FurnitureContracts.SearchModels;
using FurnitureContracts.StoragesContracts;
using FurnitureContracts.ViewModel;
using FurnitureFactoryDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureFactoryDataBaseImplements.Implements
{
public class ManagerStorage : IManagerStorage
{
public ManagerViewModel? Delete(ManagerBindingModel model)
{
using var context = new FurnitureDataBase();
var element = context.Managers
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Managers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ManagerViewModel? GetElement(ManagerSearchModel model)
{
if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue)
{
return null;
}
using var context = new FurnitureDataBase();
return context.Managers
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<ManagerViewModel> GetFilteredList(ManagerSearchModel model)
{
if (string.IsNullOrEmpty(model.UserName))
{
return new();
}
using var context = new FurnitureDataBase();
return context.Managers
.Where(x => x.Name.Contains(model.UserName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ManagerViewModel> GetFullList()
{
using var context = new FurnitureDataBase();
return context.Managers
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ManagerViewModel? Insert(ManagerBindingModel model)
{
using var context = new FurnitureDataBase();
var newManager = Manager.Create(context, model);
if (newManager == null)
{
return null;
}
context.Managers.Add(newManager);
context.SaveChanges();
return newManager.GetViewModel;
}
public ManagerViewModel? Update(ManagerBindingModel model)
{
using var context = new FurnitureDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var manager = context.Managers.FirstOrDefault(rec => rec.Id == model.Id);
if (manager == null)
{
return null;
}
manager.Update(model);
context.SaveChanges();
transaction.Commit();
return manager.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,123 @@
using Microsoft.EntityFrameworkCore;
using FurnitureContracts.BindingModels;
using FurnitureContracts.SearchModels;
using FurnitureContracts.StoragesContracts;
using FurnitureContracts.ViewModel;
using FurnitureFactoryDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureFactoryDataBaseImplements.Implements
{
public class OrderStorage : IOrdersStorage
{
public List<OrdersViewModel> GetFullList()
{
using var context = new FurnitureDataBase();
return context.Orders
.Include(x => x.SalesSalons)
.ThenInclude(x => x.SalesSalon)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrdersViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.ManagerId.HasValue)
{
return new();
}
using var context = new FurnitureDataBase();
if (model.ManagerId.HasValue && model.DateFrom.HasValue && model.DateTo != null)
{
return context.Orders
.Include(x => x.SalesSalons)
.ThenInclude(x => x.SalesSalon)
.Where(x => x.Date >= model.DateFrom && x.Date <= model.DateTo && x.ManagerId == model.ManagerId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Orders
.Include(x => x.SalesSalons)
.ThenInclude(x => x.SalesSalon)
.Where(x => x.ManagerId == model.ManagerId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public OrdersViewModel? GetElement(OrderSearchModel model)
{
if ((string.IsNullOrEmpty(model.Title) && !model.Id.HasValue) || !model.ManagerId.HasValue)
{
return null;
}
using var context = new FurnitureDataBase();
return context.Orders
.Include(x => x.SalesSalons)
.ThenInclude(x => x.SalesSalon)
.FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Title) && x.Title == model.Title) ||
(model.Id.HasValue && x.Id == model.Id)) && x.ManagerId == model.ManagerId)
?.GetViewModel;
}
public OrdersViewModel? Insert(OrdersBindingModel model)
{
using var context = new FurnitureDataBase();
var newOrder = Order.Create(context, model);
if (newOrder == null)
{
return null;
}
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrdersViewModel? Update(OrdersBindingModel model)
{
using var context = new FurnitureDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
order.UpdateInterests(context, model);
transaction.Commit();
return order.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public OrdersViewModel? Delete(OrdersBindingModel model)
{
using var context = new FurnitureDataBase();
var element = context.Orders
.Include(x => x.SalesSalons)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.SalesSalons.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,90 @@
using FurnitureContracts.BindingModels;
using FurnitureContracts.SearchModels;
using FurnitureContracts.StoragesContracts;
using FurnitureContracts.ViewModel;
using FurnitureFactoryDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureFactoryDataBaseImplements.Implements
{
public class SalesSalonStorage : ISalesSalonsStorage
{
public List<SalesSalonsViewModel> GetFullList()
{
using var context = new FurnitureDataBase();
return context.SalesSalons
.Select(x => x.GetViewModel)
.ToList();
}
public List<SalesSalonsViewModel> GetFilteredList(SalesSalonsSearchModel model)
{
if (!model.ManagerId.HasValue)
{
return new();
}
using var context = new FurnitureDataBase();
return context.Interests
.Where(x => x.StudentId == model.ManagerId)
.Select(x => x.GetViewModel)
.ToList();
}
public SalesSalonsViewModel? GetElement(SalesSalonsSearchModel model)
{
if ((string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) || !model.ManagerId.HasValue)
{
return null;
}
using var context = new FurnitureDataBase();
return context.SalesSalons
.FirstOrDefault(x => ((!string.IsNullOrEmpty(model.Name) && x.Title == model.Name) ||
(model.Id.HasValue && x.Id == model.Id)) && x.ManagerId == model.ManagerId)
?.GetViewModel;
}
public SalesSalonsViewModel? Insert(SalesSalonsBindingModel model)
{
var newSalesSalon = SalesSalons.Create(model);
if (newSalesSalon == null)
{
return null;
}
using var context = new FurnitureDataBase();
context.SalesSalons.Add(newSalesSalon);
context.SaveChanges();
return newSalesSalon.GetViewModel;
}
public SalesSalonsViewModel? Update(SalesSalonsBindingModel model)
{
using var context = new FurnitureDataBase();
var salessalon = context.SalesSalons.FirstOrDefault(x => x.Id == model.Id);
if (salessalon == null)
{
return null;
}
salessalon.Update(model);
context.SaveChanges();
return salessalon.GetViewModel;
}
public SalesSalonsViewModel? Delete(SalesSalonsBindingModel model)
{
using var context = new FurnitureDataBase();
var element = context.SalesSalons.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.SalesSalons.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -12,6 +12,8 @@ namespace FurnitureFactoryDataBaseImplement.Models
{
public class Orders : IOrdersModel
{
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string Status { get; set; } = string.Empty;
@ -46,6 +48,7 @@ namespace FurnitureFactoryDataBaseImplement.Models
return new Orders()
{
Id = model.Id,
Title = model.Title,
Status = model.Status,
Date = model.Date,
ManagerId = model.ManagerId,
@ -65,6 +68,7 @@ namespace FurnitureFactoryDataBaseImplement.Models
public OrdersViewModel GetViewModel => new()
{
Id = Id,
Title = Title,
Status = Status,
Date = Date,
ManagerId = ManagerId,

View File

@ -8,6 +8,7 @@ namespace FurnitureFactoryDataModels.Models
{
public interface IOrdersModel : IId
{
string Title { get; }
DateTime Date { get; }
string Status { get; }
public Dictionary<int, ISalesSalonsModel> OrdersSalesSalons { get; }