DataImplement + (не полный)
This commit is contained in:
parent
2bccc600f5
commit
2c40048bc3
@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.9.34728.123
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiningRoomView", "DiningRoomView\DiningRoomView.csproj", "{611756F0-9DBA-41CB-ABD1-15A094CCAFFC}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiningRoomView", "DiningRoomView\DiningRoomView.csproj", "{611756F0-9DBA-41CB-ABD1-15A094CCAFFC}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiningRoomContracts", "DiningRoomContracts\DiningRoomContracts.csproj", "{432450B8-A672-40A6-98D5-FA0F3FD8BF78}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiningRoomContracts", "DiningRoomContracts\DiningRoomContracts.csproj", "{432450B8-A672-40A6-98D5-FA0F3FD8BF78}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiningRoomDataModels", "DiningRoomDataModels\DiningRoomDataModels.csproj", "{49474CED-88C8-440C-AAFA-5DCCF868D03F}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiningRoomDataModels", "DiningRoomDataModels\DiningRoomDataModels.csproj", "{49474CED-88C8-440C-AAFA-5DCCF868D03F}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiningRoomDatabaseImplement", "DiningRoomDatabaseImplement\DiningRoomDatabaseImplement.csproj", "{348BD2CC-C93D-42E8-A890-FC7807476625}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -27,6 +29,10 @@ Global
|
||||
{49474CED-88C8-440C-AAFA-5DCCF868D03F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{49474CED-88C8-440C-AAFA-5DCCF868D03F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{49474CED-88C8-440C-AAFA-5DCCF868D03F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{348BD2CC-C93D-42E8-A890-FC7807476625}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{348BD2CC-C93D-42E8-A890-FC7807476625}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{348BD2CC-C93D-42E8-A890-FC7807476625}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{348BD2CC-C93D-42E8-A890-FC7807476625}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -15,7 +15,5 @@ namespace DiningRoomContracts.BindingModels
|
||||
public int UserId { get; set; }
|
||||
|
||||
public string CardName { get; set; } = string.Empty;
|
||||
|
||||
public Dictionary<int, (IDrinkModel, int)> DrinkCard { get; set; } = new();
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,9 @@ namespace DiningRoomContracts.BindingModels
|
||||
public int Id { get; set; }
|
||||
|
||||
public int UserId { get; set; }
|
||||
public int CardId { get; set; }
|
||||
|
||||
public string DrinkName { get; set; } = string.Empty;
|
||||
public string DrinkName { get; set; } = string.Empty;
|
||||
|
||||
public double Cost { get; set; }
|
||||
|
||||
|
@ -13,13 +13,13 @@ namespace DiningRoomContracts.BindingModels
|
||||
public int Id { get; set; }
|
||||
|
||||
public int UserId { get; set; }
|
||||
public int ProductId { get; set; }
|
||||
|
||||
public DateTime DateCreate { get; set; } = DateTime.Now;
|
||||
|
||||
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
|
||||
public Dictionary<int, (IProductModel, int)> ProductOrder { get; set; } = new();
|
||||
public Dictionary<int, (IDrinkModel, int)> DrinkOrder { get; set; } = new();
|
||||
public DateTime DateCreate { get; set; } = DateTime.Now;
|
||||
public string ProductName { get; set; } = string.Empty;
|
||||
|
||||
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
|
||||
public int Count { get; set; }
|
||||
public double Sum { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
public int? Id { get; set; }
|
||||
|
||||
public int? UserId { get; set; }
|
||||
public int? CardId { get; set; }
|
||||
|
||||
public string? DrinkName { get; set; }
|
||||
}
|
||||
|
@ -15,10 +15,9 @@ namespace DiningRoomContracts.SearchModels
|
||||
|
||||
public DateTime? DateFrom { get; set; }
|
||||
public DateTime? DateTo { get; set; }
|
||||
public string? ProductName { get; set; }
|
||||
|
||||
public OrderStatus? Status { get; set; }
|
||||
public Dictionary<int, (IProductModel, int)> ProductOrder { get; set; } = new();
|
||||
public Dictionary<int, (IDrinkModel, int)> DrinkOrder { get; set; } = new();
|
||||
|
||||
public double? Sum { get; set; }
|
||||
}
|
||||
|
@ -8,8 +8,10 @@ namespace DiningRoomContracts.ViewModels
|
||||
public int Id { get; set; }
|
||||
|
||||
public int UserId { get; set; }
|
||||
public int CardId { get; set; }
|
||||
public string CardName { get; set; } = string.Empty;
|
||||
|
||||
[DisplayName("Название товара")]
|
||||
[DisplayName("Название товара")]
|
||||
public string DrinkName { get; set; } = string.Empty;
|
||||
|
||||
[DisplayName("Стоимость")]
|
||||
|
@ -13,15 +13,16 @@ namespace DiningRoomContracts.ViewModels
|
||||
{
|
||||
[DisplayName("Номер")]
|
||||
public int Id { get; set; }
|
||||
|
||||
public int ProductId { get; set; }
|
||||
public int UserId { get; set; }
|
||||
[DisplayName("Дата Создания")]
|
||||
public DateTime DateCreate { get; set; } = DateTime.Now;
|
||||
public Dictionary<int, (IProductModel, int)> ProductOrder { get; set; } = new();
|
||||
public Dictionary<int, (IDrinkModel, int)> DrinkOrder { get; set; } = new();
|
||||
[DisplayName("Статус")]
|
||||
[DisplayName("Блюдо")]
|
||||
public string ProductName { get; set; } = string.Empty;
|
||||
[DisplayName("Статус")]
|
||||
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
|
||||
|
||||
[DisplayName("Стоимость")]
|
||||
public int Count { get; set; }
|
||||
[DisplayName("Стоимость")]
|
||||
public double Sum { get; set; }
|
||||
|
||||
|
@ -11,13 +11,8 @@
|
||||
int UserId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Название напитка
|
||||
/// Название карты
|
||||
/// </summary>
|
||||
string CardName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Список комплектующих
|
||||
/// </summary>
|
||||
Dictionary<int, (IDrinkModel, int)> DrinkCard { get; }
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
/// Стоимость товара
|
||||
/// </summary>
|
||||
double Cost { get; }
|
||||
int CardId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Список комплектующих
|
||||
|
@ -16,16 +16,16 @@ namespace DiningRoomDataModels.Models
|
||||
/// </summary>
|
||||
DateTime DateCreate { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Статус заказа
|
||||
/// </summary>
|
||||
OrderStatus Status { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Стоимость заказа
|
||||
/// </summary>
|
||||
double Sum { get; }
|
||||
Dictionary<int, (IProductModel, int)> ProductOrder { get; }
|
||||
Dictionary<int, (IDrinkModel, int)> DrinkOrder { get; }
|
||||
int ProductId { get; }
|
||||
/// <summary>
|
||||
/// Статус заказа
|
||||
/// </summary>
|
||||
OrderStatus Status { get; }
|
||||
int Count { get; }
|
||||
/// <summary>
|
||||
/// Стоимость заказа
|
||||
/// </summary>
|
||||
double Sum { get; }
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
using DiningRoomDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DiningRoomDatabaseImplement
|
||||
{
|
||||
public class DiningRoomDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder)
|
||||
{
|
||||
if (!OptionsBuilder.IsConfigured)
|
||||
{
|
||||
OptionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=DiningRoom;Username=postgres;Password=postgres");
|
||||
}
|
||||
|
||||
base.OnConfiguring(OptionsBuilder);
|
||||
|
||||
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
||||
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
|
||||
}
|
||||
public virtual DbSet<User> Users { get; set; }
|
||||
|
||||
public virtual DbSet<Component> Components { get; set; }
|
||||
|
||||
public virtual DbSet<Drink> Drinks { get; set; }
|
||||
|
||||
public virtual DbSet<DrinkComponent> DrinkComponents { get; set; }
|
||||
|
||||
public virtual DbSet<Product> Products { get; set; }
|
||||
|
||||
public virtual DbSet<ProductComponent> ProductComponents { get; set; }
|
||||
|
||||
|
||||
public virtual DbSet<Order> Orders { get; set; }
|
||||
public virtual DbSet<Card> Cards { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.18" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.18">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="netcore-psql-util" Version="1.2.1" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DiningRoomContracts\DiningRoomContracts.csproj" />
|
||||
<ProjectReference Include="..\DiningRoomDataModels\DiningRoomDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,152 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.SearchModels;
|
||||
using DiningRoomContracts.StorageContracts;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Implements
|
||||
{
|
||||
//!!!ПОДОБИЕ component
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
|
||||
var element = context.Orders
|
||||
.Include(x => x.Product)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
|
||||
if (element != null)
|
||||
{
|
||||
context.Orders.Remove(element);
|
||||
context.SaveChanges();
|
||||
|
||||
return element.GetViewModel;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new DiningRoomDatabase();
|
||||
|
||||
return context.Orders
|
||||
.Include(x => x.Client)
|
||||
.Include(x => x.Implementer)
|
||||
.FirstOrDefault(x =>
|
||||
(model.Statuses == null || model.Statuses != null && model.Statuses.Contains(x.Status)) &&
|
||||
model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId ||
|
||||
model.Id.HasValue && x.Id == model.Id
|
||||
)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
if (model.Id.HasValue)
|
||||
{
|
||||
var result = GetElement(model);
|
||||
return result != null ? new() { result } : new();
|
||||
}
|
||||
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
IQueryable<Order>? queryWhere = null;
|
||||
|
||||
if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
||||
{
|
||||
queryWhere = context.Orders
|
||||
.Where(x => model.DateFrom <= x.DateCreate.Date &&
|
||||
x.DateCreate.Date <= model.DateTo);
|
||||
}
|
||||
|
||||
else if (model.Statuses != null)
|
||||
{
|
||||
queryWhere = context.Orders.Where(x => model.Statuses.Contains(x.Status));
|
||||
}
|
||||
|
||||
else if (model.ClientId.HasValue)
|
||||
{
|
||||
queryWhere = context.Orders.Where(x => x.ClientId == model.ClientId);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
return queryWhere
|
||||
.Include(x => x.Client)
|
||||
.Include(x => x.Implementer)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
|
||||
return context.Orders
|
||||
.Include(x => x.Wood)
|
||||
.Include(x => x.Client)
|
||||
.Include(x => x.Implementer)
|
||||
.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
var newOrder = Order.Create(model);
|
||||
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
|
||||
context.Orders.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
|
||||
return context.Orders
|
||||
.Include(x => x.Wood)
|
||||
.Include(x => x.Client)
|
||||
.Include(x => x.Implementer)
|
||||
.FirstOrDefault(x => x.Id == newOrder.Id)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
|
||||
var order = context.Orders
|
||||
.Include(x => x.Wood)
|
||||
.Include(x => x.Client)
|
||||
.Include(x => x.Implementer)
|
||||
.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
|
||||
return order.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
106
DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs
Normal file
106
DiningRoom/DiningRoomDatabaseImplement/Implements/UserStorage.cs
Normal file
@ -0,0 +1,106 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.SearchModels;
|
||||
using DiningRoomContracts.StorageContracts;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDatabaseImplement.Models;
|
||||
using DiningRoomDataModels.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Implements
|
||||
{
|
||||
//!!!МБ У USER ХРАНИТЬ СПИСКИ ВСЕХ СОЗДАННЫХ СУШНОСТЕЙ И ТОГДА ПРИ СОЗДАНИИ, УДАЛЕНИИ СУЩНОСТЕЙ ЕЩЁ СОЗДАВАТЬ И УДАЛЯТЬ ИЗ СПИСКА У ПОЛЬЗОВАТЕЛЯ
|
||||
public class UserStorage : IUserStorage
|
||||
{
|
||||
public List<UserViewModel> GetFullList()
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
return context.Users.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
//Фильтрация пользователей логически нужна только для роли (по остальным параметрам будет 1 или 0 пользователей)
|
||||
public List<UserViewModel> GetFilteredList(UserSearchModel model)
|
||||
{
|
||||
if (model.Role == null || model.Role == UserRole.Неизвестная)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new DiningRoomDatabase();
|
||||
return context.Users.Where(x => x.Role == model.Role).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
//!!!ПРОВЕРИТЬ
|
||||
//id, почта и логин уникальны, можно получать по ним
|
||||
public UserViewModel? GetElement(UserSearchModel model)
|
||||
{
|
||||
//!!!МБ ЭТУ ПРОВЕРКУ УБРАТЬ
|
||||
if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new DiningRoomDatabase();
|
||||
|
||||
//Поиск пользователя при входе в систему по логину, паролю и его роли (чтобы не могли войти в аккаунты другой роли)
|
||||
if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password) && model.Role.HasValue)
|
||||
{
|
||||
return context.Users.FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password && x.Role == model.Role)?.GetViewModel;
|
||||
}
|
||||
//!!!НИЖЕ МБ НЕ НАДО
|
||||
//Получение по логину (пользователей с таким логином будет 1 или 0)
|
||||
if (!string.IsNullOrEmpty(model.Login))
|
||||
{
|
||||
return context.Users.FirstOrDefault(x => x.Login == model.Login)?.GetViewModel;
|
||||
}
|
||||
//Получение по почте (пользователей с такой почтой будет 1 или 0)
|
||||
else if (!string.IsNullOrEmpty(model.Email))
|
||||
{
|
||||
return context.Users.FirstOrDefault(x => x.Email == model.Email)?.GetViewModel;
|
||||
}
|
||||
//Получение по id
|
||||
return context.Users.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public UserViewModel? Insert(UserBindingModel model)
|
||||
{
|
||||
var newUser = User.Create(model);
|
||||
if (newUser == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new DiningRoomDatabase();
|
||||
context.Users.Add(newUser);
|
||||
context.SaveChanges();
|
||||
return newUser.GetViewModel;
|
||||
}
|
||||
|
||||
public UserViewModel? Update(UserBindingModel model)
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
var user = context.Users.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (user == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
user.Update(model);
|
||||
context.SaveChanges();
|
||||
return user.GetViewModel;
|
||||
}
|
||||
|
||||
//!!!МБ И НЕ НУЖЕН
|
||||
public UserViewModel? Delete(UserBindingModel model)
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
var user = context.Users.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (user != null)
|
||||
{
|
||||
context.Users.Remove(user);
|
||||
context.SaveChanges();
|
||||
return user.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
40
DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs
Normal file
40
DiningRoom/DiningRoomDatabaseImplement/Models/Card.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Models
|
||||
{
|
||||
public class Card : ICardModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int UserId { get; set; }
|
||||
public string CardName { get; set; } = string.Empty;
|
||||
public static Card Create(DiningRoomDatabase context, CardBindingModel model)
|
||||
{
|
||||
return new Card()
|
||||
{
|
||||
Id = model.Id,
|
||||
CardName = model.CardName,
|
||||
UserId = model.UserId,
|
||||
};
|
||||
}
|
||||
public void Update(CardBindingModel model)
|
||||
{
|
||||
CardName = model.CardName;
|
||||
}
|
||||
public CardViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
CardName = CardName,
|
||||
UserId = UserId,
|
||||
};
|
||||
}
|
||||
}
|
57
DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs
Normal file
57
DiningRoom/DiningRoomDatabaseImplement/Models/Component.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Models
|
||||
{
|
||||
public class Component : IComponentModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public int UserId { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string ComponentName { get; private set; } = string.Empty;
|
||||
public string Unit { get; private set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Cost { get; private set; }
|
||||
|
||||
[ForeignKey("ComponentId")]
|
||||
public virtual List<DrinkComponent> DrinkComponents { get; set; } = new();
|
||||
[ForeignKey("ComponentId")]
|
||||
public virtual List<ProductComponent> ProductComponents { get; set; } = new();
|
||||
|
||||
public static Component Create(ComponentBindingModel Model)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Id = Model.Id,
|
||||
UserId = Model.UserId,
|
||||
ComponentName = Model.ComponentName,
|
||||
Cost = Model.Cost,
|
||||
Unit = Model.Unit,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(ComponentBindingModel Model)
|
||||
{
|
||||
ComponentName = Model.ComponentName;
|
||||
Cost = Model.Cost;
|
||||
Unit = Model.Unit;
|
||||
}
|
||||
|
||||
public ComponentViewModel ViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
UserId = UserId,
|
||||
ComponentName = ComponentName,
|
||||
Cost = Cost,
|
||||
Unit = Unit,
|
||||
|
||||
};
|
||||
}
|
||||
}
|
116
DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs
Normal file
116
DiningRoom/DiningRoomDatabaseImplement/Models/Drink.cs
Normal file
@ -0,0 +1,116 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Models
|
||||
{
|
||||
public class Drink : IDrinkModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public int UserId { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string DrinkName { get; private set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Cost { get; private set; }
|
||||
|
||||
public int CardId { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string Category { get; private set; } = string.Empty;
|
||||
|
||||
[ForeignKey("ComponentId")]
|
||||
public virtual List<DrinkComponent> Components { get; set; } = new();
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _drinkComponents;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> DrinkComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_drinkComponents == null)
|
||||
{
|
||||
_drinkComponents = Components.ToDictionary(
|
||||
DrkComp => DrkComp.ComponentId,
|
||||
DrkComp => (DrkComp.Component as IComponentModel, DrkComp.Count)
|
||||
);
|
||||
}
|
||||
|
||||
return _drinkComponents;
|
||||
}
|
||||
}
|
||||
|
||||
public static Drink Create(DiningRoomDatabase Context, DrinkBindingModel Model)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Id = Model.Id,
|
||||
UserId = Model.UserId,
|
||||
DrinkName = Model.DrinkName,
|
||||
Cost = Model.Cost,
|
||||
CardId = Model.CardId,
|
||||
Components = Model.DrinkComponents.Select(x => new DrinkComponent
|
||||
{
|
||||
Component = Context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList(),
|
||||
};
|
||||
}
|
||||
public void Update(DrinkBindingModel model)
|
||||
{
|
||||
DrinkName = model.DrinkName;
|
||||
Cost = model.Cost;
|
||||
}
|
||||
public DrinkViewModel GetViewModel
|
||||
{
|
||||
|
||||
get
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
return new DrinkViewModel
|
||||
{
|
||||
Id = Id,
|
||||
DrinkName = DrinkName,
|
||||
CardName = context.Cards.FirstOrDefault(x => x.Id == CardId)?.CardName ?? string.Empty,
|
||||
Cost = Cost,
|
||||
DrinkComponents = DrinkComponents
|
||||
};
|
||||
}
|
||||
}
|
||||
public void UpdateComponents(DiningRoomDatabase context, DrinkBindingModel model)
|
||||
{
|
||||
var drinkComponents = context.DrinkComponents.Where(rec => rec.DrinkId == model.Id).ToList();
|
||||
if (drinkComponents != null && drinkComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.DrinkComponents.RemoveRange(drinkComponents.Where(rec => !model.DrinkComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in drinkComponents)
|
||||
{
|
||||
updateComponent.Count = model.DrinkComponents[updateComponent.ComponentId].Item2;
|
||||
model.DrinkComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var drink = context.Drinks.First(x => x.Id == Id);
|
||||
foreach (var pc in model.DrinkComponents)
|
||||
{
|
||||
context.DrinkComponents.Add(new DrinkComponent
|
||||
{
|
||||
Drink = drink,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_drinkComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Models
|
||||
{
|
||||
public class DrinkComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int DrinkId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Drink Drink { get; set; } = new();
|
||||
|
||||
public virtual Component Component { get; set; } = new();
|
||||
}
|
||||
}
|
84
DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs
Normal file
84
DiningRoom/DiningRoomDatabaseImplement/Models/Order.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDataModels.Enums;
|
||||
using DiningRoomDataModels.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 DiningRoomDatabaseImplement.Models
|
||||
{
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[ForeignKey("ProductId")]
|
||||
public int ProductId { get; set; }
|
||||
[ForeignKey("UserId")]
|
||||
[Required]
|
||||
public int UserId { get; private set; }
|
||||
public User User { get; set; }
|
||||
public virtual Product Product { get; set; }
|
||||
[Required]
|
||||
public double Sum { get; private set; }
|
||||
[Required]
|
||||
public int Count { get; private set; }
|
||||
|
||||
[Required]
|
||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||
|
||||
[Required]
|
||||
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
|
||||
|
||||
public static Order? Create(OrderBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Order()
|
||||
{
|
||||
Id = model.Id,
|
||||
ProductId = model.ProductId,
|
||||
UserId = model.UserId,
|
||||
DateCreate = model.DateCreate,
|
||||
Status = model.Status,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(OrderBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
using var context = new DiningRoomDatabase();
|
||||
Status = model.Status;
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
public OrderViewModel GetViewModel
|
||||
{
|
||||
get
|
||||
{
|
||||
using var context = new DiningRoomDatabase();
|
||||
return new OrderViewModel
|
||||
{
|
||||
Id = Id,
|
||||
ProductId = ProductId,
|
||||
ProductName = context.Products.FirstOrDefault(x => x.Id == ProductId)?.ProductName ?? string.Empty,
|
||||
UserId = UserId,
|
||||
DateCreate = DateCreate,
|
||||
Status = Status,
|
||||
Count = Count,
|
||||
Sum = Sum
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
101
DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs
Normal file
101
DiningRoom/DiningRoomDatabaseImplement/Models/Product.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Models
|
||||
{
|
||||
public class Product : IProductModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int UserId { get; set; }
|
||||
|
||||
[Required]
|
||||
public string ProductName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Cost { get; set; }
|
||||
|
||||
[ForeignKey("ComponentId")]
|
||||
public virtual List<ProductComponent> Components { get; set; } = new();
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _productComponents;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> ProductComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_productComponents == null)
|
||||
{
|
||||
_productComponents = Components.ToDictionary(
|
||||
ProdComp => ProdComp.ComponentId,
|
||||
ProdComp => (ProdComp.Component as IComponentModel, ProdComp.Count)
|
||||
);
|
||||
}
|
||||
|
||||
return _productComponents;
|
||||
}
|
||||
}
|
||||
|
||||
public static Product Create(DiningRoomDatabase Context, ProductBindingModel Model)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Id = Model.Id,
|
||||
UserId = Model.UserId,
|
||||
ProductName = Model.ProductName,
|
||||
Cost = Model.Cost,
|
||||
Components = Model.ProductComponents.Select(x => new ProductComponent
|
||||
{
|
||||
Component = Context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList(),
|
||||
};
|
||||
}
|
||||
public void Update(ProductBindingModel model)
|
||||
{
|
||||
ProductName = model.ProductName;
|
||||
Cost = model.Cost;
|
||||
}
|
||||
public ProductViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ProductName = ProductName,
|
||||
Cost = Cost,
|
||||
ProductComponents = ProductComponents
|
||||
};
|
||||
public void UpdateComponents(DiningRoomDatabase context, ProductBindingModel model)
|
||||
{
|
||||
var productComponents = context.ProductComponents.Where(rec => rec.ProductId == model.Id).ToList();
|
||||
if (productComponents != null && productComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.ProductComponents.RemoveRange(productComponents.Where(rec => !model.ProductComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in productComponents)
|
||||
{
|
||||
updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2;
|
||||
model.ProductComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var product = context.Products.First(x => x.Id == Id);
|
||||
foreach (var pc in model.ProductComponents)
|
||||
{
|
||||
context.ProductComponents.Add(new ProductComponent
|
||||
{
|
||||
Product = product,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_productComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Models
|
||||
{
|
||||
public class ProductComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ProductId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Product Product { get; set; } = new();
|
||||
|
||||
public virtual Component Component { get; set; } = new();
|
||||
}
|
||||
}
|
62
DiningRoom/DiningRoomDatabaseImplement/Models/User.cs
Normal file
62
DiningRoom/DiningRoomDatabaseImplement/Models/User.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using DiningRoomContracts.BindingModels;
|
||||
using DiningRoomContracts.ViewModels;
|
||||
using DiningRoomDataModels.Enums;
|
||||
using DiningRoomDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DiningRoomDatabaseImplement.Models
|
||||
{
|
||||
public class User : IUserModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string Login { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
//!!!МБ ТУТ НУЖНА ДОП. АННОТАЦИЯ ПРОВЕРКИ ПОЧТЫ
|
||||
[Required]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
|
||||
|
||||
//!!!МБ ТУТ НАДО БУДЕТ СОЗДАТЬ 2 РАЗНЫХ МЕТОДА: СОЗДАНИЕ ИСПОЛНИТЕЛЯ и СОЗДАНИЕ ПОРУЧИТЕЛЯ (хотя мб где-то потом будем задавать роль в BindingModel)
|
||||
public static User Create(UserBindingModel model)
|
||||
{
|
||||
return new User
|
||||
{
|
||||
Id = model.Id,
|
||||
Login = model.Login,
|
||||
Password = model.Password,
|
||||
Email = model.Email
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(UserBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Login = model.Login;
|
||||
Password = model.Password;
|
||||
Email = model.Email;
|
||||
}
|
||||
|
||||
//!!!МБ ТУТ НЕ НАДО РОЛЬ
|
||||
public UserViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Login = Login,
|
||||
Password = Password,
|
||||
Email = Email,
|
||||
Role = Role
|
||||
};
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user