Add Shop controller

This commit is contained in:
ShabOl 2024-05-14 23:43:30 +04:00
parent 30ba254e86
commit 6714d4f3ef
14 changed files with 509 additions and 50 deletions

View File

@ -0,0 +1,9 @@
namespace AutoWorkshopContracts.ViewModels
{
public class RepairCount
{
public RepairViewModel Repair { get; set; } = new();
public int Count { get; set; } = new();
}
}

View File

@ -0,0 +1,9 @@
namespace AutoWorkshopContracts.ViewModels
{
public class ShopRepairViewModel
{
public ShopViewModel Shop { get; set; } = new();
public Dictionary<int, RepairCount> ShopRepairs { get; set; } = new();
}
}

View File

@ -1,22 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace AutoWorkshopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class FixShopRepairrelationship : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View File

@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace AutoWorkshopDatabaseImplement.Migrations
{
[DbContext(typeof(AutoWorkshopDatabase))]
[Migration("20240505173724_Fix Shop-Repair relationship")]
partial class FixShopRepairrelationship
[Migration("20240514192226_Lab5_Hard")]
partial class Lab5_Hard
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -25,6 +25,31 @@ namespace AutoWorkshopDatabaseImplement.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Client", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ClientFIO")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Clients");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
@ -53,6 +78,9 @@ namespace AutoWorkshopDatabaseImplement.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ClientId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
@ -73,6 +101,8 @@ namespace AutoWorkshopDatabaseImplement.Migrations
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("RepairId");
b.ToTable("Orders");
@ -179,12 +209,20 @@ namespace AutoWorkshopDatabaseImplement.Migrations
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Order", b =>
{
b.HasOne("AutoWorkshopDatabaseImplement.Models.Client", "Client")
.WithMany("Orders")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair")
.WithMany("Orders")
.HasForeignKey("RepairId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Client");
b.Navigation("Repair");
});
@ -226,6 +264,11 @@ namespace AutoWorkshopDatabaseImplement.Migrations
b.Navigation("Shop");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Client", b =>
{
b.Navigation("Orders");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b =>
{
b.Navigation("RepairComponents");

View File

@ -0,0 +1,215 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace AutoWorkshopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class Lab5_Hard : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Clients",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ClientFIO = table.Column<string>(type: "text", nullable: false),
Email = table.Column<string>(type: "text", nullable: false),
Password = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Clients", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ComponentName = table.Column<string>(type: "text", nullable: false),
Cost = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Repairs",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
RepairName = table.Column<string>(type: "text", nullable: false),
Price = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Repairs", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ShopName = table.Column<string>(type: "text", nullable: false),
Address = table.Column<string>(type: "text", nullable: false),
OpeningDate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
RepairsMaxCount = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
RepairId = table.Column<int>(type: "integer", nullable: false),
ClientId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false),
Sum = table.Column<double>(type: "double precision", nullable: false),
Status = table.Column<int>(type: "integer", nullable: false),
DateCreate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
DateImplement = table.Column<DateTime>(type: "timestamp without time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Clients_ClientId",
column: x => x.ClientId,
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Orders_Repairs_RepairId",
column: x => x.RepairId,
principalTable: "Repairs",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "RepairComponents",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
RepairId = table.Column<int>(type: "integer", nullable: false),
ComponentId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_RepairComponents", x => x.Id);
table.ForeignKey(
name: "FK_RepairComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_RepairComponents_Repairs_RepairId",
column: x => x.RepairId,
principalTable: "Repairs",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopRepairs",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
RepairId = table.Column<int>(type: "integer", nullable: false),
ShopId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShopRepairs", x => x.Id);
table.ForeignKey(
name: "FK_ShopRepairs_Repairs_RepairId",
column: x => x.RepairId,
principalTable: "Repairs",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopRepairs_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_ClientId",
table: "Orders",
column: "ClientId");
migrationBuilder.CreateIndex(
name: "IX_Orders_RepairId",
table: "Orders",
column: "RepairId");
migrationBuilder.CreateIndex(
name: "IX_RepairComponents_ComponentId",
table: "RepairComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_RepairComponents_RepairId",
table: "RepairComponents",
column: "RepairId");
migrationBuilder.CreateIndex(
name: "IX_ShopRepairs_RepairId",
table: "ShopRepairs",
column: "RepairId");
migrationBuilder.CreateIndex(
name: "IX_ShopRepairs_ShopId",
table: "ShopRepairs",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "RepairComponents");
migrationBuilder.DropTable(
name: "ShopRepairs");
migrationBuilder.DropTable(
name: "Clients");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Repairs");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -22,6 +22,31 @@ namespace AutoWorkshopDatabaseImplement.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Client", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ClientFIO")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Clients");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
@ -50,6 +75,9 @@ namespace AutoWorkshopDatabaseImplement.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ClientId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
@ -70,6 +98,8 @@ namespace AutoWorkshopDatabaseImplement.Migrations
b.HasKey("Id");
b.HasIndex("ClientId");
b.HasIndex("RepairId");
b.ToTable("Orders");
@ -176,12 +206,20 @@ namespace AutoWorkshopDatabaseImplement.Migrations
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Order", b =>
{
b.HasOne("AutoWorkshopDatabaseImplement.Models.Client", "Client")
.WithMany("Orders")
.HasForeignKey("ClientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair")
.WithMany("Orders")
.HasForeignKey("RepairId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Client");
b.Navigation("Repair");
});
@ -223,6 +261,11 @@ namespace AutoWorkshopDatabaseImplement.Migrations
b.Navigation("Shop");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Client", b =>
{
b.Navigation("Orders");
});
modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b =>
{
b.Navigation("RepairComponents");

View File

@ -11,9 +11,6 @@ namespace AutoWorkshopFileImplement
private readonly string OrderFileName = "Order.xml";
private readonly string RepairFileName = "Repair.xml";
private readonly string ShopFileName = "Shop.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
private readonly string ClientFileName = "Client.xml";
public List<Component> Components { get; private set; }
@ -21,9 +18,9 @@ namespace AutoWorkshopFileImplement
public List<Order> Orders { get; private set; }
public List<Repair> Repairs { get; private set; }
public List<Shop> Shops { get; private set; }
public List<Client> Clients { get; private set; }
private DataFileSingleton()

View File

@ -0,0 +1,12 @@
namespace AutoWorkshopRestApi
{
public class ApiConfig
{
public static string? ShopPassword;
public static void LoadData(IConfiguration Configuration)
{
ShopPassword = Configuration["ShopApiPassword"];
}
}
}

View File

@ -0,0 +1,156 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.BusinessLogicsContracts;
using AutoWorkshopContracts.SearchModels;
using AutoWorkshopContracts.ViewModels;
using Microsoft.AspNetCore.Mvc;
namespace AutoWorkshopRestApi.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ShopController : Controller
{
private readonly ILogger _logger;
private readonly IShopLogic _shopLogic;
public ShopController(ILogger<ShopController> Logger, IShopLogic ShopLogic)
{
_logger = Logger;
_shopLogic = ShopLogic;
}
[HttpGet]
public bool Authentication(string Password)
{
return CheckPassword(Password);
}
[HttpGet]
public List<ShopViewModel>? GetShopList(string Password)
{
if (!CheckPassword(Password))
{
return null;
}
try
{
return _shopLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка магазинов");
throw;
}
}
[HttpGet]
public ShopRepairViewModel? GetShop(int ShopId, string Password)
{
if (!CheckPassword(Password))
{
return null;
}
try
{
var Shop = _shopLogic.ReadElement(new ShopSearchModel { Id = ShopId });
return new ShopRepairViewModel
{
Shop = Shop,
ShopRepairs = Shop.ShopRepairs.ToDictionary(x => x.Key, x => new RepairCount
{
Repair = new RepairViewModel()
{
Id = x.Value.Item1.Id,
RepairName = x.Value.Item1.RepairName,
RepairComponents = x.Value.Item1.RepairComponents,
Price = x.Value.Item1.Price,
},
Count = x.Value.Item2
})
};
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения магазина");
throw;
}
}
[HttpPost]
public void CreateShop(ShopBindingModel Model, string Password)
{
if (!CheckPassword(Password))
{
return;
}
try
{
_shopLogic.Create(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания магазина");
throw;
}
}
[HttpPost]
public void UpdateShop(ShopBindingModel Model, string Password)
{
if (!CheckPassword(Password))
{
return;
}
try
{
_shopLogic.Update(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка обновления магазина");
throw;
}
}
[HttpDelete]
public void DeleteShop(int ShopId, string Password)
{
if (!CheckPassword(Password))
{
return;
}
try
{
_shopLogic.Delete(new ShopBindingModel { Id = ShopId });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления магазина");
throw;
}
}
[HttpPost]
public void MakeSypply(SupplyBindingModel Model, string Password)
{
if (!CheckPassword(Password))
{
return;
}
try
{
_shopLogic.MakeSupply(Model);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания поставки в магазин");
throw;
}
}
private bool CheckPassword(string Password)
{
return ApiConfig.ShopPassword == Password;
}
}
}

View File

@ -1,7 +1,9 @@
using AutoWorkshopBusinessLogic.BusinessLogics;
using AutoWorkshopContracts.BusinessLogicContracts;
using AutoWorkshopContracts.BusinessLogicsContracts;
using AutoWorkshopContracts.StoragesContracts;
using AutoWorkshopDatabaseImplement.Implements;
using AutoWorkshopRestApi;
using Microsoft.OpenApi.Models;
var Builder = WebApplication.CreateBuilder(args);
@ -12,9 +14,13 @@ Builder.Logging.AddLog4Net("log4net.config");
Builder.Services.AddTransient<IClientStorage, ClientStorage>();
Builder.Services.AddTransient<IOrderStorage, OrderStorage>();
Builder.Services.AddTransient<IRepairStorage, RepairStorage>();
Builder.Services.AddTransient<IShopStorage, ShopStorage>();
Builder.Services.AddTransient<IOrderLogic, OrderLogic>();
Builder.Services.AddTransient<IClientLogic, ClientLogic>();
Builder.Services.AddTransient<IRepairLogic, RepairLogic>();
Builder.Services.AddTransient<IShopLogic, ShopLogic>();
Builder.Services.AddControllers();
Builder.Services.AddEndpointsApiExplorer();
@ -28,6 +34,7 @@ Builder.Services.AddSwaggerGen(c =>
});
var App = Builder.Build();
ApiConfig.LoadData(Builder.Configuration);
if (App.Environment.IsDevelopment())
{

View File

@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ShopApiPassword": "8841"
}

View File

@ -120,21 +120,14 @@
ReportsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { ReportRepairsToolStripMenuItem, ReportRepCompToolStripMenuItem, OrdersToolStripMenuItem, ReportShopsToolStripMenuItem, RepostBusyShopsToolStripMenuItem, ReportGroupOrdersToolStripMenuItem });
ReportsToolStripMenuItem.Name = "ReportsToolStripMenuItem";
ReportsToolStripMenuItem.Size = new Size(60, 20);
//
// ClientsToolStripMenuItem
//
ClientsToolStripMenuItem.Name = "ClientsToolStripMenuItem";
ReportsToolStripMenuItem.Text = "Отчёты";
//
// ClientsToolStripMenuItem
//
ClientsToolStripMenuItem.Name = "ClientsToolStripMenuItem";
ClientsToolStripMenuItem.Size = new Size(182, 26);
ClientsToolStripMenuItem.Text = "Клиенты";
ClientsToolStripMenuItem.Click += ClientsToolStripMenuItem_Click;
//
// ReportsToolStripMenuItem
//
ReportsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] {
ComponentsToolStripMenuItem1,
ComponentRepairToolStripMenuItem1,
OrdersToolStripMenuItem});
ReportsToolStripMenuItem.Text = "Отчёты";
//
// ReportRepairsToolStripMenuItem
//
@ -289,11 +282,9 @@
private ToolStripMenuItem ReportRepairsToolStripMenuItem;
private ToolStripMenuItem ReportRepCompToolStripMenuItem;
private ToolStripMenuItem OrdersToolStripMenuItem;
private ToolStripMenuItem OrdersToolStripMenuItem;
private ToolStripMenuItem ReportShopsToolStripMenuItem;
private ToolStripMenuItem ClientsToolStripMenuItem;
}
private ToolStripMenuItem RepostBusyShopsToolStripMenuItem;
private ToolStripMenuItem ReportGroupOrdersToolStripMenuItem;
private ToolStripMenuItem ClientsToolStripMenuItem;
private ToolStripMenuItem ClientsToolStripMenuItem;
}
}

View File

@ -261,7 +261,6 @@ namespace AutoWorkshopView
Form.ShowDialog();
}
}
}
private void ClientsToolStripMenuItem_Click(object sender, EventArgs e)
{

View File

@ -42,14 +42,14 @@ namespace AutoWorkshopView
Services.AddTransient<IOrderStorage, OrderStorage>();
Services.AddTransient<IRepairStorage, RepairStorage>();
Services.AddTransient<IShopStorage, ShopStorage>();
Services.AddTransient<IClientStorage, ClientStorage>();
Services.AddTransient<IComponentLogic, ComponentLogic>();
Services.AddTransient<IOrderLogic, OrderLogic>();
Services.AddTransient<IRepairLogic, RepairLogic>();
Services.AddTransient<IReportLogic, ReportLogic>();
Services.AddTransient<IShopLogic, ShopLogic>();
Services.AddTransient<IClientLogic, ClientLogic>();
Services.AddTransient<IClientStorage, ClientStorage>();
Services.AddTransient<IClientLogic, ClientLogic>();
Services.AddTransient<AbstractSaveToWord, SaveToWord>();
Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
@ -70,7 +70,6 @@ namespace AutoWorkshopView
Services.AddTransient<FormReportOrders>();
Services.AddTransient<FormReportShop>();
Services.AddTransient<FormReportGroupedOrders>();
}
Services.AddTransient<FormClients>();
}
}