Выполненная лаб. работа.

This commit is contained in:
Anastasia 2023-04-02 17:16:00 +04:00
parent f163242d36
commit c691996cbc
11 changed files with 281 additions and 30 deletions

View File

@ -101,6 +101,7 @@
this.Controls.Add(this.labelName);
this.Name = "FormComponent";
this.Text = "Компонент";
this.Load += new System.EventHandler(this.FormComponent_Load);
this.ResumeLayout(false);
this.PerformLayout();

View File

@ -106,6 +106,7 @@
this.Controls.Add(this.dataGridView);
this.Name = "FormComponents";
this.Text = "Компоненты";
this.Load += new System.EventHandler(this.FormComponents_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);

View File

@ -207,6 +207,7 @@
this.Controls.Add(this.groupBox);
this.Name = "FormJewel";
this.Text = "Изделие";
this.Load += new System.EventHandler(this.FormJewel_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.groupBox.ResumeLayout(false);
this.ResumeLayout(false);

View File

@ -100,6 +100,7 @@
this.Controls.Add(this.dataGridView);
this.Name = "FormJewels";
this.Text = "Изделия";
this.Load += new System.EventHandler(this.FormJewels_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.ResumeLayout(false);

View File

@ -156,6 +156,7 @@
this.MainMenuStrip = this.menuStrip;
this.Name = "FormMain";
this.Text = "Ювелирная лавка";
this.Load += new System.EventHandler(this.FormMain_Load);
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();

View File

@ -1,6 +1,6 @@
using JewelryStoreContracts.BusinessLogicsContracts;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreFileImplement.Implements;
using JewelryStoreDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;

View File

@ -0,0 +1,80 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JewelryStoreDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new JewelryStoreDatabase();
return context.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new JewelryStoreDatabase();
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 JewelryStoreDatabase();
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 JewelryStoreDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new JewelryStoreDatabase();
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 JewelryStoreDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,94 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JewelryStoreDatabaseImplement.Implements
{
public class JewelStorage : IJewelStorage
{
public List<JewelViewModel> GetFullList()
{
using var context = new JewelryStoreDatabase();
return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
}
public List<JewelViewModel> GetFilteredList(JewelSearchModel model)
{
if (string.IsNullOrEmpty(model.JewelName))
{
return new();
}
using var context = new JewelryStoreDatabase();
return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component)
.Where(x => x.JewelName.Contains(model.JewelName)).ToList().Select(x => x.GetViewModel).ToList();
}
public JewelViewModel? GetElement(JewelSearchModel model)
{
if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue)
{
return null;
}
using var context = new JewelryStoreDatabase();
return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.JewelName) &&
x.JewelName == model.JewelName) ||(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public JewelViewModel? Insert(JewelBindingModel model)
{
using var context = new JewelryStoreDatabase();
var newProduct = Jewel.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Jewels.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public JewelViewModel? Update(JewelBindingModel model)
{
using var context = new JewelryStoreDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Jewels.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateComponents(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public JewelViewModel? Delete(JewelBindingModel model)
{
using var context = new JewelryStoreDatabase();
var element = context.Jewels
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Jewels.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,90 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JewelryStoreDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new JewelryStoreDatabase();
return context.Orders
.Select(x => AccessJewelStorage(x.GetViewModel, context))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new JewelryStoreDatabase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessJewelStorage(x.GetViewModel, context))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new JewelryStoreDatabase();
return AccessJewelStorage(context.Orders
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel, context);
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new JewelryStoreDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessJewelStorage(newOrder.GetViewModel, context);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new JewelryStoreDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return AccessJewelStorage(order.GetViewModel, context);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new JewelryStoreDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessJewelStorage(element.GetViewModel, context);
}
return null;
}
static OrderViewModel AccessJewelStorage(OrderViewModel model, JewelryStoreDatabase context)
{
if (model == null) return model;
string? jewelName = context.Jewels.FirstOrDefault(x => x.Id == model.JewelId)?.JewelName;
if (jewelName != null) model.JewelName = jewelName;
return model;
}
}
}

View File

@ -15,7 +15,7 @@ namespace JewelryStoreDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Initial Catalog=JewelryStoreDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=JewelryStoreDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}

View File

@ -5,6 +5,7 @@ using JewelryStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -15,19 +16,17 @@ namespace JewelryStoreDatabaseImplement.Models
{
public int Id { get; private set; }
[Required]
public int JewelId { get; private set; }
public int JewelId { get; set; }
[Required]
public int Count { get; private set; }
public int Count { get; set; }
[Required]
public double Sum { get; private set; }
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public OrderStatus Status { get; set; }
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public virtual Jewel Jewel { get; set; }
public DateTime DateCreate { get; set; }
public DateTime? DateImplement { get; set; }
public Jewel Jewel { get; set; }
public static Order? Create(OrderBindingModel? model)
{
@ -46,36 +45,19 @@ namespace JewelryStoreDatabaseImplement.Models
DateImplement = model.DateImplement
};
}
public static Order Create(OrderViewModel model)
{
return new Order
{
Id = model.Id,
JewelId = model.JewelId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel model)
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
if (model.DateImplement.HasValue) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
JewelId = JewelId,
JewelName = Jewel.JewelName,
Count = Count,
Sum = Sum,
Status = Status,