Лабораторная работа №2
This commit is contained in:
parent
1598bebe06
commit
4896424633
@ -19,7 +19,6 @@ public class Product
|
||||
get;
|
||||
private set;
|
||||
} = [];
|
||||
|
||||
public static Product CreateEntity(int id, string nameOfShoes, ManufacturingCompany manufacturingCompany, int price)
|
||||
{
|
||||
return new Product
|
||||
|
@ -10,14 +10,10 @@ namespace ShoeStore.Entities;
|
||||
public class Sale
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
//public int ProductId { get; private set; }
|
||||
//public int StorageProductId { get; private set; }
|
||||
public DateTime DateOfSale { get; private set; }
|
||||
//public ProductType ProductType { get; private set; }
|
||||
|
||||
public int SalesNumber { get; private set; }
|
||||
//public int StorageSize { get; private set; }
|
||||
public IEnumerable<ProductSale> productSales
|
||||
public IEnumerable<ProductSale> Product
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
@ -28,8 +24,6 @@ public class Sale
|
||||
return new Sale
|
||||
{
|
||||
Id = id,
|
||||
//ProductType = productType,
|
||||
//StorageProductId = storageProductId,
|
||||
DateOfSale = DateTime.Now,
|
||||
SalesNumber = salesNumber,
|
||||
//StorageSize = storageSize
|
||||
|
@ -8,13 +8,15 @@ namespace ShoeStore.Entities;
|
||||
|
||||
public class Storage
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public int Size { get; private set; }
|
||||
public int QuantityInStock { get; private set; }
|
||||
public int NumberStorage { get; private set; }
|
||||
public static Storage CreateEntity(int size, int quantityInStock, int numberStorage)
|
||||
public static Storage CreateEntity(int id, int size, int quantityInStock, int numberStorage)
|
||||
{
|
||||
return new Storage
|
||||
{
|
||||
Id = id,
|
||||
Size = size,
|
||||
QuantityInStock = quantityInStock,
|
||||
NumberStorage = numberStorage
|
||||
|
26
ShoeStore/Forms/FormSale.Designer.cs
generated
26
ShoeStore/Forms/FormSale.Designer.cs
generated
@ -36,6 +36,8 @@
|
||||
dataGridView1 = new DataGridView();
|
||||
ColumnProduct = new DataGridViewComboBoxColumn();
|
||||
ColumnAmount = new DataGridViewTextBoxColumn();
|
||||
label1 = new Label();
|
||||
dateTimePickerDate = new DateTimePicker();
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownStorageSize).BeginInit();
|
||||
panel1.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit();
|
||||
@ -44,7 +46,7 @@
|
||||
// label5
|
||||
//
|
||||
label5.AutoSize = true;
|
||||
label5.Location = new Point(38, 82);
|
||||
label5.Location = new Point(38, 103);
|
||||
label5.Name = "label5";
|
||||
label5.Size = new Size(155, 15);
|
||||
label5.TabIndex = 4;
|
||||
@ -52,7 +54,7 @@
|
||||
//
|
||||
// numericUpDownStorageSize
|
||||
//
|
||||
numericUpDownStorageSize.Location = new Point(207, 80);
|
||||
numericUpDownStorageSize.Location = new Point(207, 101);
|
||||
numericUpDownStorageSize.Name = "numericUpDownStorageSize";
|
||||
numericUpDownStorageSize.Size = new Size(120, 23);
|
||||
numericUpDownStorageSize.TabIndex = 10;
|
||||
@ -105,11 +107,29 @@
|
||||
ColumnAmount.HeaderText = "Кол-во";
|
||||
ColumnAmount.Name = "ColumnAmount";
|
||||
//
|
||||
// label1
|
||||
//
|
||||
label1.AutoSize = true;
|
||||
label1.Location = new Point(38, 39);
|
||||
label1.Name = "label1";
|
||||
label1.Size = new Size(35, 15);
|
||||
label1.TabIndex = 16;
|
||||
label1.Text = "Дата:";
|
||||
//
|
||||
// dateTimePickerDate
|
||||
//
|
||||
dateTimePickerDate.Location = new Point(207, 33);
|
||||
dateTimePickerDate.Name = "dateTimePickerDate";
|
||||
dateTimePickerDate.Size = new Size(132, 23);
|
||||
dateTimePickerDate.TabIndex = 15;
|
||||
//
|
||||
// FormSale
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(377, 493);
|
||||
Controls.Add(label1);
|
||||
Controls.Add(dateTimePickerDate);
|
||||
Controls.Add(panel1);
|
||||
Controls.Add(buttonCancel);
|
||||
Controls.Add(buttonSave);
|
||||
@ -133,5 +153,7 @@
|
||||
private DataGridView dataGridView1;
|
||||
private DataGridViewComboBoxColumn ColumnProduct;
|
||||
private DataGridViewTextBoxColumn ColumnAmount;
|
||||
private Label label1;
|
||||
private DateTimePicker dateTimePickerDate;
|
||||
}
|
||||
}
|
@ -68,14 +68,13 @@ namespace ShoeStore.Forms
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!checkBoxNalStorage.Checked || numericUpDownSize.Value <= 0 || numericUpDownNumStorage.Value <= 0)
|
||||
if (numericUpDownSize.Value <= 0 || numericUpDownNumStorage.Value <= 0)
|
||||
{
|
||||
throw new Exception("Имеются незаполненные поля или товар отсутствует на складе");
|
||||
throw new Exception("Имеются незаполненные поля ");
|
||||
}
|
||||
_storageRepository.CreateStorage(Storage.CreateEntity(
|
||||
checkBoxNalStorage.Checked ? 1 : 0, // 1 - есть на складе, 0 - нет
|
||||
|
||||
_storageRepository.CreateStorage(Storage.CreateEntity(0,
|
||||
Convert.ToInt32(numericUpDownSize.Value),
|
||||
checkBoxNalStorage.Checked ? 1 : 0, // 1 - есть на складе, 0 - нет
|
||||
Convert.ToInt32(numericUpDownNumStorage.Value)));
|
||||
Close();
|
||||
}
|
||||
|
@ -46,6 +46,20 @@ public class ReceiptRepository : IReceiptRepository
|
||||
|
||||
public IEnumerable<Receipt> ReadReceipts(DateTime? dateForm = null, DateTime? dateTo = null, int? storageProductId = null, int? storageSize = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Receipts";
|
||||
var receipt = connection.Query<Receipt>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(receipt));
|
||||
return receipt;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ShoeStore.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ShoeStore.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,16 +13,89 @@ namespace ShoeStore.Repositories.Implementations;
|
||||
|
||||
public class SaleRepository : ISaleRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<SaleRepository> _logger;
|
||||
|
||||
|
||||
public SaleRepository(IConnectionString connectionString, ILogger<SaleRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateSale(Sale sale)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(sale));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Sales (SalesNumber, Date)
|
||||
VALUES (@SalesNumber, @Date);
|
||||
SELECT MAX(Id) FROM Sales";
|
||||
var visitingId = connection.QueryFirst<int>(queryInsert, sale, transaction);
|
||||
var querySubInsert = @"
|
||||
INSERT INTO Product_Sales (Amount, SalesNumber)
|
||||
VALUES (@Amount,@SalesNumber)";
|
||||
foreach (var elem in sale.Product)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
elem.ProductId,
|
||||
elem.SaleId,
|
||||
elem.Amount
|
||||
}, transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteSale(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"DELETE FROM Sales
|
||||
WHERE ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Sale> ReadSales(DateTime? dateForm = null, DateTime? dateTo = null, int? productId = null, int? storageSize = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT * FROM Sales";
|
||||
var sale =
|
||||
connection.Query<Sale>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(sale));
|
||||
return sale;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using ShoeStore.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ShoeStore.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -9,25 +13,112 @@ namespace ShoeStore.Repositories.Implementations;
|
||||
|
||||
public class StorageRepository : IStorageRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<StorageRepository> _logger;
|
||||
|
||||
|
||||
public StorageRepository(IConnectionString connectionString, ILogger<StorageRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateStorage(Storage storage)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(storage));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"INSERT INTO Storages (Size, NumberStorage, QuantityInStock)
|
||||
VALUES (@Size, @NumberStorage, @QuantityInStock)";
|
||||
connection.Execute(queryInsert, storage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteStorage(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"DELETE FROM Storages WHERE ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Storage ReadStorageById(int id)
|
||||
{
|
||||
return Storage.CreateEntity(0, 0, 0);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT * FROM Storages WHERE ID=@id";
|
||||
var storage = connection.QueryFirst<Storage>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(storage));
|
||||
return storage;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Storage> ReadStorages()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Storages";
|
||||
var storage = connection.Query<Storage>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(storage));
|
||||
return storage;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateStorage(Storage storage)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(storage));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"UPDATE Storages SET
|
||||
Size=@Size,
|
||||
NumberStorage=@NumberStorage,
|
||||
QuantityInStock=@QuantityInStock
|
||||
WHERE ID = @id";
|
||||
connection.Execute(queryUpdate, storage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user