diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs index c39f8bb..c84e7bf 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -56,7 +56,7 @@ namespace ConfectioneryBusinessLogic.BusinessLogics return false; } model.Status = newStatus; - if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now; + if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); else { model.DateImplement = viewModel.DateImplement; diff --git a/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs index 7859c0f..895fb3f 100644 --- a/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs +++ b/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs @@ -20,7 +20,7 @@ namespace ConfectioneryContracts.BindingModels public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 478c0ee..aa6da7b 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -18,9 +18,9 @@ namespace ConfectioneryDatabaseImplement optionsBuilder.UseNpgsql(@" Host=localhost; Port=5432; - Database=RPP; + Database=ConfectioneryDatabase; Username=postgres; - Password=12345678"); + Password=postgres"); } base.OnConfiguring(optionsBuilder); } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..b8fd402 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,86 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..deea67e --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,105 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new ConfectioneryDatabase(); + + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new ConfectioneryDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + + using var context = new ConfectioneryDatabase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new ConfectioneryDatabase(); + var element = context.Pastries.FirstOrDefault(x => x.Id == order.PastryId); + viewModel.PastryName = element.PastryName; + return viewModel; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs new file mode 100644 index 0000000..8207d5d --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/PastryStorage.cs @@ -0,0 +1,111 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class PastryStorage : IPastryStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && + !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName) && + x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public PastryViewModel? Insert(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newPastry = Pastry.Create(context, model); + if (newPastry == null) + { + return null; + } + context.Pastries.Add(newPastry); + context.SaveChanges(); + return newPastry.GetViewModel; + } + public PastryViewModel? Update(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var pastry = context.Pastries.FirstOrDefault(rec => + rec.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + context.SaveChanges(); + pastry.UpdateComponents(context, model); + transaction.Commit(); + return pastry.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public PastryViewModel? Delete(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Pastries + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pastries.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.Designer.cs similarity index 98% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.Designer.cs index 05c4d0b..dadc731 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.Designer.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.Designer.cs @@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace ConfectioneryDatabaseImplement.Migrations { [DbContext(typeof(ConfectioneryDatabase))] - [Migration("20230313183606_InitMigration")] - partial class InitMigration + [Migration("20230314061048_InitMigrate")] + partial class InitMigrate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.cs similarity index 98% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.cs index 8b670d7..1771d1b 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313183606_InitMigration.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230314061048_InitMigrate.cs @@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace ConfectioneryDatabaseImplement.Migrations { /// - public partial class InitMigration : Migration + public partial class InitMigrate : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) diff --git a/Confectionery/ConfectioneryView/Program.cs b/Confectionery/ConfectioneryView/Program.cs index 2d29ab3..8b7ae81 100644 --- a/Confectionery/ConfectioneryView/Program.cs +++ b/Confectionery/ConfectioneryView/Program.cs @@ -1,7 +1,7 @@ using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContracts; -using ConfectioneryFileImplement.Implements; +using ConfectioneryDatabaseImplement.Implements; using ConfectioneryView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging;