fix database

This commit is contained in:
Zakharov_Rostislav 2024-05-05 21:21:25 +04:00
parent 687e4a8e55
commit 22f762b60b
14 changed files with 74 additions and 174 deletions

View File

@ -9,7 +9,6 @@ namespace CarShowroomDataModels.Models
public interface IClient : IId public interface IClient : IId
{ {
string Name { get; } string Name { get; }
string Email { get; } string PhoneNumber { get; }
string Password { get; }
} }
} }

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="SearchModel\" />
</ItemGroup>
</Project> </Project>

View File

@ -11,14 +11,12 @@ namespace CarShowroomContracts.Dtos
{ {
public int Id { get; set; } public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Email { get; set; } public string PhoneNumber { get; set; }
public string Password { get; set; }
public ClientDto(IClient model) public ClientDto(IClient model)
{ {
Id = model.Id; Id = model.Id;
Name = model.Name; Name = model.Name;
Email = model.Email; PhoneNumber = model.PhoneNumber;
Password = model.Password;
} }
} }
} }

View File

@ -14,15 +14,12 @@ namespace CarShowroomContracts.Views
[DisplayName("ФИО")] [DisplayName("ФИО")]
public string Name { get; set; } public string Name { get; set; }
[DisplayName("Email")] [DisplayName("Email")]
public string Email { get; set; } public string PhoneNumber { get; set; }
[DisplayName("Пароль")]
public string Password { get; set; }
public ClientView(IClient model) public ClientView(IClient model)
{ {
Id = model.Id; Id = model.Id;
Name = model.Name; Name = model.Name;
Email = model.Email; PhoneNumber = model.PhoneNumber;
Password = model.Password;
} }
} }
} }

View File

@ -30,7 +30,6 @@ namespace CarShowroomDatabaseStorage
modelBuilder.Entity<SaleService>().HasKey(sc => new { sc.SaleId, sc.ServiceId }); modelBuilder.Entity<SaleService>().HasKey(sc => new { sc.SaleId, sc.ServiceId });
//default values //default values
modelBuilder.Entity<Client>().Property(c => c.Name).HasDefaultValue("empty_string"); modelBuilder.Entity<Client>().Property(c => c.Name).HasDefaultValue("empty_string");
modelBuilder.Entity<Client>().Property(c => c.Password).HasDefaultValue("empty_string");
modelBuilder.Entity<Employee>().Property(e => e.Name).HasDefaultValue("empty_string"); modelBuilder.Entity<Employee>().Property(e => e.Name).HasDefaultValue("empty_string");
modelBuilder.Entity<Employee>().Property(e => e.Password).HasDefaultValue("empty_string"); modelBuilder.Entity<Employee>().Property(e => e.Password).HasDefaultValue("empty_string");
modelBuilder.Entity<Car>().Property(c => c.Color).HasDefaultValue("empty_string"); modelBuilder.Entity<Car>().Property(c => c.Color).HasDefaultValue("empty_string");

View File

@ -13,7 +13,7 @@ using System.Threading.Tasks;
namespace CarShowroomDatabaseStorage.Entities namespace CarShowroomDatabaseStorage.Entities
{ {
[Table("client")] [Table("client")]
[Index(nameof(Email), IsUnique = true)] [Index(nameof(PhoneNumber), IsUnique = true)]
public class Client : IClient public class Client : IClient
{ {
[Column("client_id")] [Column("client_id")]
@ -23,13 +23,9 @@ namespace CarShowroomDatabaseStorage.Entities
[MaxLength(50)] [MaxLength(50)]
public string Name { get; private set; } = string.Empty; public string Name { get; private set; } = string.Empty;
[Required] [Required]
[Column("client_email")] [Column("client_phonenumber")]
[MaxLength(40)] [MaxLength(25)]
public string Email { get; private set; } = string.Empty; public string PhoneNumber { get; private set; } = string.Empty;
[Required]
[Column("client_password")]
[MaxLength(32)]
public string Password { get; private set; } = string.Empty;
public virtual List<Sale> Sales { get; set; } = new(); public virtual List<Sale> Sales { get; set; } = new();
private Client() { } private Client() { }
@ -38,8 +34,7 @@ namespace CarShowroomDatabaseStorage.Entities
{ {
Id = client.Id; Id = client.Id;
Name = client.Name; Name = client.Name;
Email = client.Email; PhoneNumber = client.PhoneNumber;
Password = client.Password;
} }
public static Client? Create(IClient client) public static Client? Create(IClient client)
@ -54,8 +49,7 @@ namespace CarShowroomDatabaseStorage.Entities
if (client == null) if (client == null)
return; return;
Name = client.Name; Name = client.Name;
Email = client.Email; PhoneNumber = client.PhoneNumber;
Password = client.Password;
} }
public ClientView GetClientView() public ClientView GetClientView()

View File

@ -28,24 +28,22 @@ namespace CarShowroomDatabaseStorage.Entities
[Column("sale_employee_id")] [Column("sale_employee_id")]
public int? EmployeeId { get; private set; } public int? EmployeeId { get; private set; }
public virtual Employee? Employee { get; set; } public virtual Employee? Employee { get; set; }
public virtual List<Car> Cars { get; set; } = new();
public virtual List<SaleCar> SaleCars { get; set; } = new(); public virtual List<SaleCar> SaleCars { get; set; } = new();
[NotMapped] [NotMapped]
public List<int> CarIds public List<int> CarIds
{ {
get get
{ {
return Cars.Select(c => c.Id).ToList(); return SaleCars.Select(c => c.CarId).ToList();
} }
} }
public virtual List<Service> Services { get; set; } = new();
public virtual List<SaleService> SaleServices { get; set; } = new(); public virtual List<SaleService> SaleServices { get; set; } = new();
[NotMapped] [NotMapped]
public List<int> ServiceIds public List<int> ServiceIds
{ {
get get
{ {
return Services.Select(s => s.Id).ToList(); return SaleServices.Select(s => s.ServiceId).ToList();
} }
} }
@ -86,8 +84,8 @@ namespace CarShowroomDatabaseStorage.Entities
SaleView sale = new SaleView(this); SaleView sale = new SaleView(this);
sale.ClientName = Client?.Name ?? string.Empty; sale.ClientName = Client?.Name ?? string.Empty;
sale.EmployeeName = Employee?.Name ?? string.Empty; sale.EmployeeName = Employee?.Name ?? string.Empty;
sale.Services = Services.Select(s => s.GetServiceView()).ToList(); sale.Services = SaleServices.Select(s => s.Service.GetServiceView()).ToList();
sale.Cars = Cars.Select(c => c.GetCarView()).ToList(); sale.Cars = SaleCars.Select(c => c.Car.GetCarView()).ToList();
return sale; return sale;
} }
} }

View File

@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace CarShowroomDatabaseStorage.Migrations namespace CarShowroomDatabaseStorage.Migrations
{ {
[DbContext(typeof(CarShowroomDatabase))] [DbContext(typeof(CarShowroomDatabase))]
[Migration("20240505145552_InitialCreate")] [Migration("20240505165914_InitialCreate")]
partial class InitialCreate partial class InitialCreate
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -50,15 +50,10 @@ namespace CarShowroomDatabaseStorage.Migrations
.HasColumnType("timestamp with time zone") .HasColumnType("timestamp with time zone")
.HasColumnName("car_releasedate"); .HasColumnName("car_releasedate");
b.Property<int?>("SaleId")
.HasColumnType("integer");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("ModelId"); b.HasIndex("ModelId");
b.HasIndex("SaleId");
b.ToTable("car"); b.ToTable("car");
}); });
@ -71,12 +66,6 @@ namespace CarShowroomDatabaseStorage.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id")); NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(40)
.HasColumnType("character varying(40)")
.HasColumnName("client_email");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
@ -85,17 +74,15 @@ namespace CarShowroomDatabaseStorage.Migrations
.HasDefaultValue("empty_string") .HasDefaultValue("empty_string")
.HasColumnName("client_name"); .HasColumnName("client_name");
b.Property<string>("Password") b.Property<string>("PhoneNumber")
.IsRequired() .IsRequired()
.ValueGeneratedOnAdd() .HasMaxLength(25)
.HasMaxLength(32) .HasColumnType("character varying(25)")
.HasColumnType("character varying(32)") .HasColumnName("client_phonenumber");
.HasDefaultValue("empty_string")
.HasColumnName("client_password");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("Email") b.HasIndex("PhoneNumber")
.IsUnique(); .IsUnique();
b.ToTable("client"); b.ToTable("client");
@ -289,16 +276,11 @@ namespace CarShowroomDatabaseStorage.Migrations
.HasColumnType("character varying(50)") .HasColumnType("character varying(50)")
.HasColumnName("service_name"); .HasColumnName("service_name");
b.Property<int?>("SaleId")
.HasColumnType("integer");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("Name") b.HasIndex("Name")
.IsUnique(); .IsUnique();
b.HasIndex("SaleId");
b.ToTable("service"); b.ToTable("service");
}); });
@ -310,10 +292,6 @@ namespace CarShowroomDatabaseStorage.Migrations
.OnDelete(DeleteBehavior.Restrict) .OnDelete(DeleteBehavior.Restrict)
.IsRequired(); .IsRequired();
b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", null)
.WithMany("Cars")
.HasForeignKey("SaleId");
b.Navigation("Model"); b.Navigation("Model");
}); });
@ -383,13 +361,6 @@ namespace CarShowroomDatabaseStorage.Migrations
b.Navigation("Service"); b.Navigation("Service");
}); });
modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b =>
{
b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", null)
.WithMany("Services")
.HasForeignKey("SaleId");
});
modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b => modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b =>
{ {
b.Navigation("SaleCars"); b.Navigation("SaleCars");
@ -417,13 +388,9 @@ namespace CarShowroomDatabaseStorage.Migrations
modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b => modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b =>
{ {
b.Navigation("Cars");
b.Navigation("SaleCars"); b.Navigation("SaleCars");
b.Navigation("SaleServices"); b.Navigation("SaleServices");
b.Navigation("Services");
}); });
modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b => modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b =>

View File

@ -19,8 +19,7 @@ namespace CarShowroomDatabaseStorage.Migrations
client_id = table.Column<int>(type: "integer", nullable: false) client_id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
client_name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "empty_string"), client_name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "empty_string"),
client_email = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false), client_phonenumber = table.Column<string>(type: "character varying(25)", maxLength: 25, nullable: false)
client_password = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false, defaultValue: "empty_string")
}, },
constraints: table => constraints: table =>
{ {
@ -55,6 +54,20 @@ namespace CarShowroomDatabaseStorage.Migrations
table.PrimaryKey("PK_make", x => x.make_id); table.PrimaryKey("PK_make", x => x.make_id);
}); });
migrationBuilder.CreateTable(
name: "service",
columns: table => new
{
service_id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
service_name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
service_cost = table.Column<int>(type: "integer", nullable: false, defaultValue: -1)
},
constraints: table =>
{
table.PrimaryKey("PK_service", x => x.service_id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "sale", name: "sale",
columns: table => new columns: table => new
@ -104,53 +117,6 @@ namespace CarShowroomDatabaseStorage.Migrations
onDelete: ReferentialAction.Restrict); onDelete: ReferentialAction.Restrict);
}); });
migrationBuilder.CreateTable(
name: "service",
columns: table => new
{
service_id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
service_name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
service_cost = table.Column<int>(type: "integer", nullable: false, defaultValue: -1),
SaleId = table.Column<int>(type: "integer", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_service", x => x.service_id);
table.ForeignKey(
name: "FK_service_sale_SaleId",
column: x => x.SaleId,
principalTable: "sale",
principalColumn: "sale_id");
});
migrationBuilder.CreateTable(
name: "car",
columns: table => new
{
car_id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
car_color = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "empty_string"),
car_releasedate = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
car_model_id = table.Column<int>(type: "integer", nullable: false),
SaleId = table.Column<int>(type: "integer", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_car", x => x.car_id);
table.ForeignKey(
name: "FK_car_model_car_model_id",
column: x => x.car_model_id,
principalTable: "model",
principalColumn: "model_id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_car_sale_SaleId",
column: x => x.SaleId,
principalTable: "sale",
principalColumn: "sale_id");
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "sale_service", name: "sale_service",
columns: table => new columns: table => new
@ -175,6 +141,27 @@ namespace CarShowroomDatabaseStorage.Migrations
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable(
name: "car",
columns: table => new
{
car_id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
car_color = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "empty_string"),
car_releasedate = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
car_model_id = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_car", x => x.car_id);
table.ForeignKey(
name: "FK_car_model_car_model_id",
column: x => x.car_model_id,
principalTable: "model",
principalColumn: "model_id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "sale_car", name: "sale_car",
columns: table => new columns: table => new
@ -205,14 +192,9 @@ namespace CarShowroomDatabaseStorage.Migrations
column: "car_model_id"); column: "car_model_id");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_car_SaleId", name: "IX_client_client_phonenumber",
table: "car",
column: "SaleId");
migrationBuilder.CreateIndex(
name: "IX_client_client_email",
table: "client", table: "client",
column: "client_email", column: "client_phonenumber",
unique: true); unique: true);
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
@ -258,11 +240,6 @@ namespace CarShowroomDatabaseStorage.Migrations
table: "sale_service", table: "sale_service",
column: "service_id"); column: "service_id");
migrationBuilder.CreateIndex(
name: "IX_service_SaleId",
table: "service",
column: "SaleId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_service_service_name", name: "IX_service_service_name",
table: "service", table: "service",
@ -282,23 +259,23 @@ namespace CarShowroomDatabaseStorage.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "car"); name: "car");
migrationBuilder.DropTable(
name: "sale");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "service"); name: "service");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "model"); name: "model");
migrationBuilder.DropTable(
name: "sale");
migrationBuilder.DropTable(
name: "make");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "client"); name: "client");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "employee"); name: "employee");
migrationBuilder.DropTable(
name: "make");
} }
} }
} }

View File

@ -47,15 +47,10 @@ namespace CarShowroomDatabaseStorage.Migrations
.HasColumnType("timestamp with time zone") .HasColumnType("timestamp with time zone")
.HasColumnName("car_releasedate"); .HasColumnName("car_releasedate");
b.Property<int?>("SaleId")
.HasColumnType("integer");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("ModelId"); b.HasIndex("ModelId");
b.HasIndex("SaleId");
b.ToTable("car"); b.ToTable("car");
}); });
@ -68,12 +63,6 @@ namespace CarShowroomDatabaseStorage.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id")); NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(40)
.HasColumnType("character varying(40)")
.HasColumnName("client_email");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
@ -82,17 +71,15 @@ namespace CarShowroomDatabaseStorage.Migrations
.HasDefaultValue("empty_string") .HasDefaultValue("empty_string")
.HasColumnName("client_name"); .HasColumnName("client_name");
b.Property<string>("Password") b.Property<string>("PhoneNumber")
.IsRequired() .IsRequired()
.ValueGeneratedOnAdd() .HasMaxLength(25)
.HasMaxLength(32) .HasColumnType("character varying(25)")
.HasColumnType("character varying(32)") .HasColumnName("client_phonenumber");
.HasDefaultValue("empty_string")
.HasColumnName("client_password");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("Email") b.HasIndex("PhoneNumber")
.IsUnique(); .IsUnique();
b.ToTable("client"); b.ToTable("client");
@ -286,16 +273,11 @@ namespace CarShowroomDatabaseStorage.Migrations
.HasColumnType("character varying(50)") .HasColumnType("character varying(50)")
.HasColumnName("service_name"); .HasColumnName("service_name");
b.Property<int?>("SaleId")
.HasColumnType("integer");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("Name") b.HasIndex("Name")
.IsUnique(); .IsUnique();
b.HasIndex("SaleId");
b.ToTable("service"); b.ToTable("service");
}); });
@ -307,10 +289,6 @@ namespace CarShowroomDatabaseStorage.Migrations
.OnDelete(DeleteBehavior.Restrict) .OnDelete(DeleteBehavior.Restrict)
.IsRequired(); .IsRequired();
b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", null)
.WithMany("Cars")
.HasForeignKey("SaleId");
b.Navigation("Model"); b.Navigation("Model");
}); });
@ -380,13 +358,6 @@ namespace CarShowroomDatabaseStorage.Migrations
b.Navigation("Service"); b.Navigation("Service");
}); });
modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b =>
{
b.HasOne("CarShowroomDatabaseStorage.Entities.Sale", null)
.WithMany("Services")
.HasForeignKey("SaleId");
});
modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b => modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Car", b =>
{ {
b.Navigation("SaleCars"); b.Navigation("SaleCars");
@ -414,13 +385,9 @@ namespace CarShowroomDatabaseStorage.Migrations
modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b => modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Sale", b =>
{ {
b.Navigation("Cars");
b.Navigation("SaleCars"); b.Navigation("SaleCars");
b.Navigation("SaleServices"); b.Navigation("SaleServices");
b.Navigation("Services");
}); });
modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b => modelBuilder.Entity("CarShowroomDatabaseStorage.Entities.Service", b =>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.