Надеюсь, что БД будет работать.

This commit is contained in:
Programmist73 2023-04-13 01:35:46 +04:00
parent e05d5c950b
commit 8b27ac16f3
10 changed files with 890 additions and 21 deletions

View File

@ -142,9 +142,9 @@ namespace TransportCompanyBusinessLogic.BusinessLogic
}
//проверка на наличие телефонного номера
if (string.IsNullOrEmpty(model.TelephoneNumber))
if (string.IsNullOrEmpty(model.Telephone))
{
throw new ArgumentNullException("Нет телефонного номера клиента", nameof(model.TelephoneNumber));
throw new ArgumentNullException("Нет телефонного номера клиента", nameof(model.Telephone));
}
//проверка на наличие почты
@ -155,7 +155,7 @@ namespace TransportCompanyBusinessLogic.BusinessLogic
_logger.LogInformation("Client. Name:{Name}. Surname:{Surname}. Patronymic:{Patronymic}. " +
"TelephoneNumber:{TelephoneNumber}. Email:{Email}. Id:{Id}",
model.Name, model.Surname, model.Patronymic, model.TelephoneNumber, model.Email, model.Id);
model.Name, model.Surname, model.Patronymic, model.Telephone, model.Email, model.Id);
//проверка на наличие такой же почты в списке
var element = _clientStorage.GetElement(new ClientSearchModel

View File

@ -101,7 +101,7 @@ public partial class ElegevContext : DbContext
entity.Property(e => e.DateStart).HasColumnName("date_start");
entity.Property(e => e.Price).HasColumnName("price");
entity.Property(e => e.TransportId).HasColumnName("transport_id");
entity.Property(e => e.TypeTransportationId).HasColumnName("type_transportation_id");
entity.Property(e => e.TransportationId).HasColumnName("transportation_id");
entity.HasOne(d => d.Cargo).WithMany(p => p.Truckings)
.HasForeignKey(d => d.CargoId)
@ -119,7 +119,7 @@ public partial class ElegevContext : DbContext
.HasConstraintName("transport_id");
entity.HasOne(d => d.TypeTransportation).WithMany(p => p.Truckings)
.HasForeignKey(d => d.TypeTransportationId)
.HasForeignKey(d => d.TransportationId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("type_transportation_id");
});

View File

@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.SearchModels;
using TransportCompanyContracts.StoragesContracts;
using TransportCompanyContracts.ViewModels;
using TransportCompanyDatabaseImplements.Models;
namespace TransportCompanyDatabaseImplements.Implements
{
public class CargoStorage : ICargoStorage
{
public CargoViewModel? Delete(CargoBindingModel model)
{
using var context = new ElegevContext();
var element = context.Cargos
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cargos.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public CargoViewModel? GetElement(CargoSearchModel model)
{
if (string.IsNullOrEmpty(model.TypeCargo) && !model.Id.HasValue)
{
return null;
}
using var context = new ElegevContext();
return context.Cargos
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<CargoViewModel> GetFilteredList(CargoSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ElegevContext();
return context.Cargos
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CargoViewModel> GetFullList()
{
using var context = new ElegevContext();
return context.Cargos
.Select(x => x.GetViewModel)
.ToList();
}
public CargoViewModel? Insert(CargoBindingModel model)
{
using var context = new ElegevContext();
model.Id = context.Cargos.Count() > 0 ? context.Cargos.Max(x => x.Id) + 1 : 1;
var newCargo = Cargo.Create(model);
if (newCargo == null)
{
return null;
}
context.Cargos.Add(newCargo);
context.SaveChanges();
return newCargo.GetViewModel;
}
public CargoViewModel? Update(CargoBindingModel model)
{
using var context = new ElegevContext();
using var transaction = context.Database.BeginTransaction();
try
{
var cargo = context.Cargos.FirstOrDefault(rec => rec.Id == model.Id);
if (cargo == null)
{
return null;
}
cargo.Update(model);
context.SaveChanges();
transaction.Commit();
return cargo.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -7,6 +7,7 @@ using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.SearchModels;
using TransportCompanyContracts.StoragesContracts;
using TransportCompanyContracts.ViewModels;
using TransportCompanyDatabaseImplements.Models;
namespace TransportCompanyDatabaseImplements.Implements
{
@ -16,32 +17,102 @@ namespace TransportCompanyDatabaseImplements.Implements
{
using var context = new ElegevContext();
return context.Clients.ToList();
return context.Clients
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
throw new NotImplementedException();
if (!model.Id.HasValue)
{
return new();
}
using var context = new ElegevContext();
return context.Clients
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
throw new NotImplementedException();
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new ElegevContext();
return context.Clients
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ClientViewModel? Insert(ClientBindingModel model)
{
throw new NotImplementedException();
using var context = new ElegevContext();
model.Id = context.Clients.Count() > 0 ? context.Clients.Max(x => x.Id) + 1 : 1;
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
context.Clients.Add(newClient);
context.SaveChanges();
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
throw new NotImplementedException();
using var context = new ElegevContext();
using var transaction = context.Database.BeginTransaction();
try
{
var client = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
transaction.Commit();
return client.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ClientViewModel? Delete(ClientBindingModel model)
{
throw new NotImplementedException();
using var context = new ElegevContext();
var element = context.Clients
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clients.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.SearchModels;
using TransportCompanyContracts.StoragesContracts;
using TransportCompanyContracts.ViewModels;
using TransportCompanyDatabaseImplements.Models;
namespace TransportCompanyDatabaseImplements.Implements
{
public class TranportationStorage : ITransportationStorage
{
public TransportationViewModel? Delete(TransportationBindingModel model)
{
using var context = new ElegevContext();
var element = context.TypeTransportations
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.TypeTransportations.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public TransportationViewModel? GetElement(TransportationSearchModel model)
{
if (string.IsNullOrEmpty(model.TransportationType) && !model.Id.HasValue)
{
return null;
}
using var context = new ElegevContext();
return context.TypeTransportations
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<TransportationViewModel> GetFilteredList(TransportationSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ElegevContext();
return context.TypeTransportations
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<TransportationViewModel> GetFullList()
{
using var context = new ElegevContext();
return context.TypeTransportations
.Select(x => x.GetViewModel)
.ToList();
}
public TransportationViewModel? Insert(TransportationBindingModel model)
{
using var context = new ElegevContext();
model.Id = context.TypeTransportations.Count() > 0 ? context.TypeTransportations.Max(x => x.Id) + 1 : 1;
var newTypeTransportation = TypeTransportation.Create(model);
if (newTypeTransportation == null)
{
return null;
}
context.TypeTransportations.Add(newTypeTransportation);
context.SaveChanges();
return newTypeTransportation.GetViewModel;
}
public TransportationViewModel? Update(TransportationBindingModel model)
{
using var context = new ElegevContext();
using var transaction = context.Database.BeginTransaction();
try
{
var typeTransportations = context.TypeTransportations.FirstOrDefault(rec => rec.Id == model.Id);
if (typeTransportations == null)
{
return null;
}
typeTransportations.Update(model);
context.SaveChanges();
transaction.Commit();
return typeTransportations.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.SearchModels;
using TransportCompanyContracts.StoragesContracts;
using TransportCompanyContracts.ViewModels;
using TransportCompanyDatabaseImplements.Models;
namespace TransportCompanyDatabaseImplements.Implements
{
public class TransportStorage : ITransportStorage
{
public TransportViewModel? Delete(TransportBindingModel model)
{
using var context = new ElegevContext();
var element = context.Transports
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Transports.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public TransportViewModel? GetElement(TransportSearchModel model)
{
if (string.IsNullOrEmpty(model.Tranport) && !model.Id.HasValue)
{
return null;
}
using var context = new ElegevContext();
return context.Transports
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<TransportViewModel> GetFilteredList(TransportSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ElegevContext();
return context.Transports
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<TransportViewModel> GetFullList()
{
using var context = new ElegevContext();
return context.Transports
.Select(x => x.GetViewModel)
.ToList();
}
public TransportViewModel? Insert(TransportBindingModel model)
{
using var context = new ElegevContext();
model.Id = context.Transports.Count() > 0 ? context.Transports.Max(x => x.Id) + 1 : 1;
var newTransport = Transport.Create(model);
if (newTransport == null)
{
return null;
}
context.Transports.Add(newTransport);
context.SaveChanges();
return newTransport.GetViewModel;
}
public TransportViewModel? Update(TransportBindingModel model)
{
using var context = new ElegevContext();
using var transaction = context.Database.BeginTransaction();
try
{
var transport = context.Transports.FirstOrDefault(rec => rec.Id == model.Id);
if (transport == null)
{
return null;
}
transport.Update(model);
context.SaveChanges();
transaction.Commit();
return transport.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.SearchModels;
using TransportCompanyContracts.StoragesContracts;
using TransportCompanyContracts.ViewModels;
using TransportCompanyDatabaseImplements.Models;
namespace TransportCompanyDatabaseImplements.Implements
{
public class TruckingStorage : ITruckingStorage
{
public TruckingViewModel? Delete(TruckingBindingModel model)
{
using var context = new ElegevContext();
var element = context.Truckings
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Truckings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public TruckingViewModel? GetElement(TruckingSearchModel model)
{
if (!model.Id.HasValue || !model.ClientId.HasValue)
{
return null;
}
using var context = new ElegevContext();
return context.Truckings
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<TruckingViewModel> GetFilteredList(TruckingSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ElegevContext();
return context.Truckings
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<TruckingViewModel> GetFullList()
{
using var context = new ElegevContext();
return context.Truckings
.Select(x => x.GetViewModel)
.ToList();
}
public TruckingViewModel? Insert(TruckingBindingModel model)
{
using var context = new ElegevContext();
model.Id = context.Truckings.Count() > 0 ? context.Truckings.Max(x => x.Id) + 1 : 1;
var newTrucking = Trucking.Create(model);
if (newTrucking == null)
{
return null;
}
context.Truckings.Add(newTrucking);
context.SaveChanges();
return newTrucking.GetViewModel;
}
public TruckingViewModel? Update(TruckingBindingModel model)
{
using var context = new ElegevContext();
using var transaction = context.Database.BeginTransaction();
try
{
var trucking = context.Truckings.FirstOrDefault(rec => rec.Id == model.Id);
if (trucking == null)
{
return null;
}
trucking.Update(model);
context.SaveChanges();
transaction.Commit();
return trucking.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,239 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using TransportCompanyDatabaseImplements;
#nullable disable
namespace TransportCompanyDatabaseImplements.Migrations
{
[DbContext(typeof(ElegevContext))]
[Migration("20230412213501_FirstMigra")]
partial class FirstMigra
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.HasSequence("seq_cargo");
modelBuilder.HasSequence("seq_client");
modelBuilder.HasSequence("seq_trucking");
modelBuilder.HasSequence("seq_type_transport");
modelBuilder.HasSequence("seq_type_transportation");
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.Cargo", b =>
{
b.Property<int>("Id")
.HasColumnType("integer")
.HasColumnName("id");
b.Property<string>("TypeCargo")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)")
.HasColumnName("type_cargo");
b.HasKey("Id")
.HasName("cargo_pkey");
b.ToTable("cargo", (string)null);
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.Client", b =>
{
b.Property<int>("Id")
.HasColumnType("integer")
.HasColumnName("id");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)")
.HasColumnName("email");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)")
.HasColumnName("name");
b.Property<string>("Patronymic")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)")
.HasColumnName("patronymic");
b.Property<string>("Surname")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)")
.HasColumnName("surname");
b.Property<string>("Telephone")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)")
.HasColumnName("telephone");
b.HasKey("Id")
.HasName("client_pkey");
b.ToTable("client", (string)null);
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.Transport", b =>
{
b.Property<int>("Id")
.HasColumnType("integer")
.HasColumnName("id");
b.Property<string>("TransportType")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)")
.HasColumnName("transport_type");
b.HasKey("Id")
.HasName("transport_pkey");
b.ToTable("transport", (string)null);
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.Trucking", b =>
{
b.Property<int>("Id")
.HasColumnType("integer")
.HasColumnName("id");
b.Property<int>("CargoId")
.HasColumnType("integer")
.HasColumnName("cargo_id");
b.Property<int>("ClientId")
.HasColumnType("integer")
.HasColumnName("client_id");
b.Property<DateTime>("DateEnd")
.HasColumnType("timestamp with time zone")
.HasColumnName("date_end");
b.Property<DateTime>("DateStart")
.HasColumnType("timestamp with time zone")
.HasColumnName("date_start");
b.Property<double>("Price")
.HasColumnType("double precision")
.HasColumnName("price");
b.Property<int>("TransportId")
.HasColumnType("integer")
.HasColumnName("transport_id");
b.Property<int>("TransportationId")
.HasColumnType("integer")
.HasColumnName("transportation_id");
b.HasKey("Id")
.HasName("trucking_pkey");
b.HasIndex("CargoId");
b.HasIndex("ClientId");
b.HasIndex("TransportId");
b.HasIndex("TransportationId");
b.ToTable("trucking", (string)null);
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.TypeTransportation", b =>
{
b.Property<int>("Id")
.HasColumnType("integer")
.HasColumnName("id");
b.Property<string>("TransportationType")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("character varying(255)")
.HasColumnName("transportation_type");
b.HasKey("Id")
.HasName("type_transportation_pkey");
b.ToTable("type_transportation", (string)null);
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.Trucking", b =>
{
b.HasOne("TransportCompanyDatabaseImplements.Models.Cargo", "Cargo")
.WithMany("Truckings")
.HasForeignKey("CargoId")
.IsRequired()
.HasConstraintName("cargo_id");
b.HasOne("TransportCompanyDatabaseImplements.Models.Client", "Client")
.WithMany("Truckings")
.HasForeignKey("ClientId")
.IsRequired()
.HasConstraintName("client_id");
b.HasOne("TransportCompanyDatabaseImplements.Models.Transport", "Transport")
.WithMany("Truckings")
.HasForeignKey("TransportId")
.IsRequired()
.HasConstraintName("transport_id");
b.HasOne("TransportCompanyDatabaseImplements.Models.TypeTransportation", "TypeTransportation")
.WithMany("Truckings")
.HasForeignKey("TransportationId")
.IsRequired()
.HasConstraintName("type_transportation_id");
b.Navigation("Cargo");
b.Navigation("Client");
b.Navigation("Transport");
b.Navigation("TypeTransportation");
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.Cargo", b =>
{
b.Navigation("Truckings");
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.Client", b =>
{
b.Navigation("Truckings");
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.Transport", b =>
{
b.Navigation("Truckings");
});
modelBuilder.Entity("TransportCompanyDatabaseImplements.Models.TypeTransportation", b =>
{
b.Navigation("Truckings");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,87 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace TransportCompanyDatabaseImplements.Migrations
{
/// <inheritdoc />
public partial class FirstMigra : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "type_transportation_id",
table: "trucking",
newName: "transportation_id");
migrationBuilder.RenameIndex(
name: "IX_trucking_type_transportation_id",
table: "trucking",
newName: "IX_trucking_transportation_id");
migrationBuilder.AlterColumn<double>(
name: "price",
table: "trucking",
type: "double precision",
nullable: false,
oldClrType: typeof(int),
oldType: "integer");
migrationBuilder.AlterColumn<DateTime>(
name: "date_start",
table: "trucking",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateOnly),
oldType: "date");
migrationBuilder.AlterColumn<DateTime>(
name: "date_end",
table: "trucking",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateOnly),
oldType: "date");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "transportation_id",
table: "trucking",
newName: "type_transportation_id");
migrationBuilder.RenameIndex(
name: "IX_trucking_transportation_id",
table: "trucking",
newName: "IX_trucking_type_transportation_id");
migrationBuilder.AlterColumn<int>(
name: "price",
table: "trucking",
type: "integer",
nullable: false,
oldClrType: typeof(double),
oldType: "double precision");
migrationBuilder.AlterColumn<DateOnly>(
name: "date_start",
table: "trucking",
type: "date",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
migrationBuilder.AlterColumn<DateOnly>(
name: "date_end",
table: "trucking",
type: "date",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
}
}
}

View File

@ -124,25 +124,25 @@ namespace TransportCompanyDatabaseImplements.Migrations
.HasColumnType("integer")
.HasColumnName("client_id");
b.Property<DateOnly>("DateEnd")
.HasColumnType("date")
b.Property<DateTime>("DateEnd")
.HasColumnType("timestamp with time zone")
.HasColumnName("date_end");
b.Property<DateOnly>("DateStart")
.HasColumnType("date")
b.Property<DateTime>("DateStart")
.HasColumnType("timestamp with time zone")
.HasColumnName("date_start");
b.Property<int>("Price")
.HasColumnType("integer")
b.Property<double>("Price")
.HasColumnType("double precision")
.HasColumnName("price");
b.Property<int>("TransportId")
.HasColumnType("integer")
.HasColumnName("transport_id");
b.Property<int>("TypeTransportationId")
b.Property<int>("TransportationId")
.HasColumnType("integer")
.HasColumnName("type_transportation_id");
.HasColumnName("transportation_id");
b.HasKey("Id")
.HasName("trucking_pkey");
@ -153,7 +153,7 @@ namespace TransportCompanyDatabaseImplements.Migrations
b.HasIndex("TransportId");
b.HasIndex("TypeTransportationId");
b.HasIndex("TransportationId");
b.ToTable("trucking", (string)null);
});
@ -198,7 +198,7 @@ namespace TransportCompanyDatabaseImplements.Migrations
b.HasOne("TransportCompanyDatabaseImplements.Models.TypeTransportation", "TypeTransportation")
.WithMany("Truckings")
.HasForeignKey("TypeTransportationId")
.HasForeignKey("TransportationId")
.IsRequired()
.HasConstraintName("type_transportation_id");