CostItem create

This commit is contained in:
Илья Федотов 2024-05-27 22:05:47 +04:00
parent 12851df7fc
commit 1a5a000ef4
14 changed files with 121 additions and 14 deletions

View File

@ -95,7 +95,11 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
if (string.IsNullOrEmpty(model.Name)) { if (string.IsNullOrEmpty(model.Name)) {
throw new ArgumentNullException("Отсутствует название статьи затрат", nameof(model.Price)); throw new ArgumentNullException("Отсутствует название статьи затрат", nameof(model.Price));
} }
_logger.LogInformation($"CostItem. ID:{model.ID}.EmployeeID:{model.EmployeeID}.Name:{model.Name}.Price:{model.Price}"); if (model.CostNum <= 0) {
throw new ArgumentOutOfRangeException("Номер счета должен идти от 1");
}
_logger.LogInformation($"CostItem. ID:{model.ID}.EmployeeID:{model.EmployeeID}.Name:{model.Name}.Price:{model.Price}" +
$"CostNum:{model.CostNum}");
var element = _storage.GetElement(new CostItemSearchModel { ID = model.ID }); var element = _storage.GetElement(new CostItemSearchModel { ID = model.ID });
if (element != null && element.ID != model.EmployeeID) if (element != null && element.ID != model.EmployeeID)
{ {

View File

@ -14,5 +14,7 @@ namespace ElectronicsShopContracts.BindingModels {
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public double Price { get; set; } public double Price { get; set; }
public int CostNum { get; set; }
} }
} }

View File

@ -8,5 +8,6 @@ namespace ElectronicsShopContracts.SearchModels {
public class CostItemSearchModel { public class CostItemSearchModel {
public int? ID { get; set; } public int? ID { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public int? CostNum { get; set; }
} }
} }

View File

@ -18,5 +18,8 @@ namespace ElectronicsShopContracts.ViewModels {
[DisplayName("Затраты")] [DisplayName("Затраты")]
public double Price { get; set; } public double Price { get; set; }
[DisplayName("Номер счета")]
public int CostNum { get; set; }
} }
} }

View File

@ -10,7 +10,7 @@ namespace ElectronicsShopDataBaseImplement
optionsBuilder) optionsBuilder)
{ {
if (optionsBuilder.IsConfigured == false) { if (optionsBuilder.IsConfigured == false) {
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-E2VPEN3\SQLEXPRESS;Initial Catalog=ElectronicsShopDatabase11;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-O0N00SH\SQLEXPRESS;Initial Catalog=ElectronicsShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
} }
base.OnConfiguring(optionsBuilder); base.OnConfiguring(optionsBuilder);
} }

View File

@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ElectronicsShopDataBaseImplement.Migrations namespace ElectronicsShopDataBaseImplement.Migrations
{ {
[DbContext(typeof(Database))] [DbContext(typeof(Database))]
[Migration("20240527144429_test")] [Migration("20240527175902_InitMigration")]
partial class test partial class InitMigration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -58,6 +58,9 @@ namespace ElectronicsShopDataBaseImplement.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID")); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("CostNum")
.HasColumnType("int");
b.Property<int>("EmployeeID") b.Property<int>("EmployeeID")
.HasColumnType("int"); .HasColumnType("int");

View File

@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace ElectronicsShopDataBaseImplement.Migrations namespace ElectronicsShopDataBaseImplement.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class test : Migration public partial class InitMigration : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
@ -34,7 +34,8 @@ namespace ElectronicsShopDataBaseImplement.Migrations
.Annotation("SqlServer:Identity", "1, 1"), .Annotation("SqlServer:Identity", "1, 1"),
EmployeeID = table.Column<int>(type: "int", nullable: false), EmployeeID = table.Column<int>(type: "int", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false), Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false) Price = table.Column<double>(type: "float", nullable: false),
CostNum = table.Column<int>(type: "int", nullable: false)
}, },
constraints: table => constraints: table =>
{ {

View File

@ -55,6 +55,9 @@ namespace ElectronicsShopDataBaseImplement.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID")); SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
b.Property<int>("CostNum")
.HasColumnType("int");
b.Property<int>("EmployeeID") b.Property<int>("EmployeeID")
.HasColumnType("int"); .HasColumnType("int");

View File

@ -24,6 +24,9 @@ namespace ElectronicsShopDataBaseImplement.Models
[Required] [Required]
public double Price { get; set; } public double Price { get; set; }
[Required]
public int CostNum { get; set; }
public static CostItem? Create(CostItemBindingModel? model) public static CostItem? Create(CostItemBindingModel? model)
{ {
if (model == null) if (model == null)
@ -36,6 +39,7 @@ namespace ElectronicsShopDataBaseImplement.Models
EmployeeID = model.EmployeeID, EmployeeID = model.EmployeeID,
Name = model.Name, Name = model.Name,
Price = model.Price, Price = model.Price,
CostNum = model.CostNum,
}; };
} }
public void Update(CostItemBindingModel? model) public void Update(CostItemBindingModel? model)
@ -47,6 +51,7 @@ namespace ElectronicsShopDataBaseImplement.Models
EmployeeID = model.EmployeeID; EmployeeID = model.EmployeeID;
Name = model.Name; Name = model.Name;
Price = model.Price; Price = model.Price;
CostNum = model.CostNum;
} }
public CostItemViewModel GetViewModel => new() public CostItemViewModel GetViewModel => new()
@ -55,7 +60,7 @@ namespace ElectronicsShopDataBaseImplement.Models
EmployeeID = EmployeeID, EmployeeID = EmployeeID,
Name = Name, Name = Name,
Price = Price, Price = Price,
CostNum = CostNum,
}; };
} }
} }

View File

@ -9,5 +9,6 @@ namespace ElectronicsShopDataModels.Models {
int EmployeeID { get; } int EmployeeID { get; }
string Name { get; } string Name { get; }
double Price { get; } double Price { get; }
int CostNum { get; }
} }
} }

View File

@ -89,6 +89,27 @@ namespace ElectronicsShopEmployeeApp.Controllers {
return; return;
} }
[HttpGet]
public IActionResult CreateCostItem() {
return View();
}
[HttpPost]
public void CreateCostItem(string name, double price, int costNum) {
if (APIEmployee.Employee == null) {
throw new Exception("Ňîëüęî äë˙ ŕâňîđčçîâŕíűő");
}
if (price <= 0) {
throw new Exception("Ńóěěŕ çŕňđŕň äîëćíŕ áűňü áîëüřĺ 0");
}
APIEmployee.PostRequest("api/main/createcostitem", new CostItemBindingModel {
EmployeeID = APIEmployee.Employee.ID,
Name = name,
Price = price,
CostNum = costNum
});
}
[HttpGet] [HttpGet]
public IActionResult Create() { public IActionResult Create() {
ViewBag.Orders = APIEmployee.GetRequset<List<ProductViewModel>>("api/main/getproductlist"); ViewBag.Orders = APIEmployee.GetRequset<List<ProductViewModel>>("api/main/getproductlist");
@ -96,7 +117,7 @@ namespace ElectronicsShopEmployeeApp.Controllers {
} }
[HttpPost] [HttpPost]
public void Create(int price, int costItem, string productName) { public void Create(int price, int costNum, string productName) {
if (APIEmployee.Employee == null) { if (APIEmployee.Employee == null) {
throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ"); throw new Exception("Òîëüêî äëÿ àâòîðèçîâàííûõ");
} }
@ -104,9 +125,9 @@ namespace ElectronicsShopEmployeeApp.Controllers {
throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0"); throw new Exception("Ñòîèìîñòü òîâàðà äîëæíà áûòü áîëüøå 0");
} }
APIEmployee.PostRequest("api/main/createproduct", new ProductBindingModel { APIEmployee.PostRequest("api/main/createproduct", new ProductBindingModel {
CostItemID = costItem, CostItemID = costNum,
ProductName = productName, ProductName = productName,
Price = Calc(price, costItem) Price = Calc(price, costNum)
}); });
Response.Redirect("Index"); Response.Redirect("Index");
} }

View File

@ -0,0 +1,47 @@
@{
ViewData["Title"] = "CreateCostItem";
}
<div class="text-center">
<h2 class="display-4">Создание статьи затрат</h2>
</div>
<form method="post">
<div class="row">
<div class="col-4">Название статьи затрат:</div>
<div class="col-8">
<input id="name" type="text" name="name" />
</div>
</div>
<div class="row">
<div class="col-4">Номер счета:</div>
<div class="col-8">
<input id="costnum" type="text" name="costnum" />
</div>
</div>
<div class="row">
<div class="col-4">Затраты:</div>
<div class="col-8">
<input id="price" type="text" name="price" />
</div>
</div>
<div class="row">
<div class="col-4"></div>
<div class="col-8">
<input type="submit" value="Создать" class="btn btn-outline-primary"/>
</div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
</form>
<script>
$('#price').on('price', function () {
check();
}
$('#costnum').on('costnum', function () {
check();
}
function check() {
var price = $('#price').val();
var costnum = $('#costnum').val();
}
</script>

View File

@ -14,11 +14,14 @@ namespace ElectronicsShopRestAPI.Controllers {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IProductLogic _product; private readonly IProductLogic _product;
private readonly IOrderLogic _order; private readonly IOrderLogic _order;
private readonly ICostItemLogic _costItem;
public MainController(ILogger<MainController> logger, IProductLogic product, IOrderLogic orderLogic) { public MainController(ILogger<MainController> logger, IProductLogic product,
IOrderLogic orderLogic, ICostItemLogic costItemLogic) {
_logger = logger; _logger = logger;
_product = product; _product = product;
_order = orderLogic; _order = orderLogic;
_costItem = costItemLogic;
} }
[HttpGet] [HttpGet]
@ -72,6 +75,17 @@ namespace ElectronicsShopRestAPI.Controllers {
return; return;
} }
//Мейби нужны будут удаления, обновления, выбор конкретного заказа или продукта. Короче потом.... [HttpPost]
public void CreateCostItem(CostItemBindingModel model) {
try {
_costItem.Create(model);
}
catch (Exception ex) {
_logger.LogError(ex, "Ошибка создания заказа");
throw;
}
}
//Мейби нужны будут удаления, обновления, выбор конкретного заказа или продукта. Короче потом...
//А также для статей затрат.
} }
} }

View File

@ -14,11 +14,13 @@ builder.Services.AddTransient<IClientStorage, ClientStorage>();
builder.Services.AddTransient<IEmployeeStorage, EmployeeStorage>(); builder.Services.AddTransient<IEmployeeStorage, EmployeeStorage>();
builder.Services.AddTransient<IProductStorage, ProductStorage>(); builder.Services.AddTransient<IProductStorage, ProductStorage>();
builder.Services.AddTransient<IOrderStorage, OrderStorage>(); builder.Services.AddTransient<IOrderStorage, OrderStorage>();
builder.Services.AddTransient<ICostItemStorage, CostItemStorage>();
builder.Services.AddTransient<IClientLogic, ClientLogic>(); builder.Services.AddTransient<IClientLogic, ClientLogic>();
builder.Services.AddTransient<IEmployeeLogic, EmployeeLogic>(); builder.Services.AddTransient<IEmployeeLogic, EmployeeLogic>();
builder.Services.AddTransient<IProductLogic, ProductLogic>(); builder.Services.AddTransient<IProductLogic, ProductLogic>();
builder.Services.AddTransient<IOrderLogic, OrderLogic>(); builder.Services.AddTransient<IOrderLogic, OrderLogic>();
builder.Services.AddTransient<ICostItemLogic, CostItemLogic>();
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle