diff --git a/TIAM.Database.Test/AdminDalTest.cs b/TIAM.Database.Test/AdminDalTest.cs index 3430e11f..a4e8e85b 100644 --- a/TIAM.Database.Test/AdminDalTest.cs +++ b/TIAM.Database.Test/AdminDalTest.cs @@ -146,7 +146,7 @@ namespace TIAM.Database.Test Assert.IsNotNull(userProductMapping.User.Profile.Address, "User.Profile.Address is null"); Assert.IsNotNull(userProductMapping.Product.Profile.Address, "Product.Profile.Address is null"); - Assert.IsTrue(userProductMapping.Transfers?.Count != 0, "Transfers?.Count == 0"); + //Assert.IsTrue(userProductMapping.Transfers?.Count != 0, "Transfers?.Count == 0"); Assert.IsTrue(userProductMapping.Id == userProductMappingId, "userProductMapping.Id != userProductMappingId"); } @@ -175,7 +175,7 @@ namespace TIAM.Database.Test Assert.IsNotNull(userProductMapping.JsonDetailModel, "User is null"); Assert.IsNotNull(userProductMapping.JsonDetailModel.Cars, "Product is null"); - Assert.IsTrue(userProductMapping.Transfers?.Count != 0); + //Assert.IsTrue(userProductMapping.Transfers?.Count != 0); Assert.IsTrue(userProductMapping.JsonDetailModel.Cars.Count == 1); Assert.IsTrue(userProductMapping.JsonDetailModel.Cars[0].UserProductCarId == userProductCarId, "userProductMapping.JsonDetailModel.Cars[0].UserProductCarId == userProductCarId"); } @@ -304,7 +304,13 @@ namespace TIAM.Database.Test var transfer = Dal.GetTransferById(transferId); Assert.IsNotNull(transfer); - Assert.IsNotNull(transfer.UserProductMapping); + //Assert.IsNotNull(transfer.UserProductMapping); + Assert.IsNotNull(transfer.TransferToDrivers); + + Assert.IsTrue(transfer.TransferToDrivers.Count > 0); + Assert.IsNotNull(transfer.TransferToDrivers[0].Car); + + Assert.AreEqual(transfer.OrderId, 1); Assert.IsTrue(transfer.Id == transferId, "transfer.Id != transferId"); } @@ -343,12 +349,13 @@ namespace TIAM.Database.Test } [DataTestMethod] - [DataRow(["8f38f8e3-a92c-4979-88b1-dc812a82245f", "814b5495-c2e9-4f1d-a73f-37cd5d353078", "71392CFD-FB9C-45C1-9540-7BE3782CF26A"])] - public async Task TransferCrudTest(string[] transferIdProductIdUserProductIdStrings) + [DataRow(["8f38f8e3-a92c-4979-88b1-dc812a82245f", "814b5495-c2e9-4f1d-a73f-37cd5d353078", "71392CFD-FB9C-45C1-9540-7BE3782CF26A", "b3f51770-c821-4207-be2a-e622fed2a731"])] + public async Task TransferCrudTest(string[] transferIdProductIdUserProductIdCarIdStrings) { - var transferId = Guid.Parse(transferIdProductIdUserProductIdStrings[0]); - var productId = Guid.Parse(transferIdProductIdUserProductIdStrings[1]); - var userProductMappingId = Guid.Parse(transferIdProductIdUserProductIdStrings[2]); + var transferId = Guid.Parse(transferIdProductIdUserProductIdCarIdStrings[0]); + var productId = Guid.Parse(transferIdProductIdUserProductIdCarIdStrings[1]); + var userProductMappingId = Guid.Parse(transferIdProductIdUserProductIdCarIdStrings[2]); + var carId = Guid.Parse(transferIdProductIdUserProductIdCarIdStrings[3]); var fromAddress = "Budapest, Liszt Ferenc tér"; var toAddress = "1211 Budapest, Kossuth Lajos utca 145"; @@ -376,23 +383,36 @@ namespace TIAM.Database.Test transfer = Dal.GetTransferById(transferId); Assert.IsNotNull(transfer); - Assert.IsNull(transfer.UserProductMapping); + //Assert.IsNull(transfer.UserProductMapping); + Assert.IsTrue(transfer.OrderId > 0); + + var car = Dal.GetCarById(carId); + Assert.IsNotNull(car); + Assert.IsTrue(car.UserProductMappingId == userProductMappingId); + + var transferToDriver = new TransferToDriver(Guid.NewGuid(), transferId, userProductMappingId, carId, car.LicencePlate, 12000); transfer.Payed = true; - transfer.UserProductToCarId = userProductToCarId; - transfer.UserProductMappingId = userProductMappingId; + //transfer.UserProductToCarId = userProductToCarId; + //transfer.UserProductMappingId = userProductMappingId; transfer.TransferStatusType = TransferStatusType.AssignedToDriver; - - Assert.IsTrue(await Dal.UpdateTransferAsync(transfer)); + Assert.IsTrue(await Dal.UpdateTransferAsync(transfer, transferToDriver)); transfer = Dal.GetTransferById(transferId); Assert.IsNotNull(transfer); - Assert.IsNotNull(transfer.UserProductMapping); + //Assert.IsNotNull(transfer.UserProductMapping); + Assert.IsNotNull(transfer.TransferToDrivers); + + Assert.IsTrue(transfer.TransferToDrivers.Count > 0); + + car = transfer.TransferToDrivers[0].Car; + Assert.IsNotNull(car); + Assert.IsTrue(car.Id == carId); Assert.IsTrue(transfer.Payed); - Assert.IsTrue(transfer.UserProductToCarId == userProductToCarId); + //Assert.IsTrue(transfer.UserProductToCarId == userProductToCarId); Assert.IsTrue(transfer.TransferStatusType == TransferStatusType.AssignedToDriver); Assert.IsTrue(transfer.Id == transferId, "transfer.Id != transferId"); diff --git a/TIAM.Database/DataLayers/Admins/AdminDal.cs b/TIAM.Database/DataLayers/Admins/AdminDal.cs index 146c91b7..ca34f611 100644 --- a/TIAM.Database/DataLayers/Admins/AdminDal.cs +++ b/TIAM.Database/DataLayers/Admins/AdminDal.cs @@ -27,18 +27,34 @@ namespace TIAM.Database.DataLayers.Admins { } + #region Car + public Car? GetCarById(Guid carId) => Session(ctx => ctx.Cars.FirstOrDefault(x => x.Id == carId)); + public List GetCarByUserProductMappingId(Guid userProductMappingId) => Session(ctx => ctx.Cars.Where(x => x.UserProductMappingId == userProductMappingId).ToList()); + #endregion Car + #region Transfer - public Transfer? GetTransferById(Guid transferId, bool autoInclude = false) => Session(ctx => ctx.GetTransferById(transferId, autoInclude)); - public string? GetTransferJsonById(Guid transferId, bool autoInclude = false) => Session(ctx => ctx.GetTransferById(transferId, autoInclude)?.ToJson()); + public Transfer? GetTransferById(Guid transferId) => Session(ctx => ctx.GetTransferById(transferId)); + public string? GetTransferJsonById(Guid transferId) => Session(ctx => ctx.GetTransferById(transferId)?.ToJson()); public Task AddTransferAsync(Transfer transfer) => TransactionAsync(ctx => ctx.AddTransfer(transfer)); public Task UpdateTransferAsync(Transfer transfer) => TransactionAsync(ctx => ctx.UpdateTransfer(transfer)); + + public Task UpdateTransferAsync(Transfer transfer, TransferToDriver transferToDriver) => UpdateTransferAsync(transfer, [transferToDriver]); + public Task UpdateTransferAsync(Transfer transfer, List transferToDrivers) + => TransactionAsync(ctx => + { + ctx.AddRange(transferToDrivers); + ctx.SaveChanges(); + + return ctx.UpdateTransfer(transfer); + }); + public Task RemoveTransferAsync(Transfer transfer) => TransactionAsync(ctx => ctx.RemoveTransfer(transfer)); public Task RemoveTransferAsync(Guid transferId) => TransactionAsync(ctx => ctx.RemoveTransfer(transferId)); #endregion Transfer #region TransferDestination - public TransferDestination? GetTransferDestinationById(Guid transferDestinationId, bool autoInclude = false) => Session(ctx=>ctx.GetTransferDestinationById(transferDestinationId)); + public TransferDestination? GetTransferDestinationById(Guid transferDestinationId) => Session(ctx=>ctx.GetTransferDestinationById(transferDestinationId)); public string? GetTransferDestinationJsonById(Guid transferDestinationId) => Session(ctx => ctx.GetTransferDestinationById(transferDestinationId)?.ToJson()); public Task AddTransferDestinationAsync(TransferDestination transferDestination) => TransactionAsync(ctx => ctx.AddTransferDestination(transferDestination)); @@ -47,6 +63,12 @@ namespace TIAM.Database.DataLayers.Admins public Task RemoveTransferDestinationAsync(Guid transferDestinationId, bool removeAddress = false) => TransactionAsync(ctx => ctx.RemoveTransferDestination(transferDestinationId, removeAddress)); #endregion TransferDestination + #region TransferToDriver + public Task AddTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.TransferToDrivers.Add(transferToDriver).State == EntityState.Added); + public Task UpdateTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.TransferToDrivers.Update(transferToDriver).State == EntityState.Modified); + public Task RemoveTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.TransferToDrivers.Remove(transferToDriver).State == EntityState.Deleted); + #endregion TransferToDriver + #region TransferDestinationToProduct public TransferDestinationToProduct? GetTransferDestinationToProductById(Guid transferDestinationToProductId) => Session(ctx=>ctx.GetTransferDestinationToProductById(transferDestinationToProductId)); public string? GetTransferDestinationToProductJsonById(Guid transferDestinationToProductId) => Session(ctx => ctx.GetTransferDestinationToProductById(transferDestinationToProductId)?.ToJson()); diff --git a/TIAM.Database/DbContexts/Admins/AdminDbContext.cs b/TIAM.Database/DbContexts/Admins/AdminDbContext.cs index 03c9932b..eb24e122 100644 --- a/TIAM.Database/DbContexts/Admins/AdminDbContext.cs +++ b/TIAM.Database/DbContexts/Admins/AdminDbContext.cs @@ -27,6 +27,8 @@ namespace TIAM.Database.DbContexts.Admins public DbSet TransferDestinations { get; set; } public DbSet TransferDestinationToProducts { get; set; } public DbSet Transfers { get; set; } + public DbSet TransferToDrivers { get; set; } + public DbSet Cars { get; set; } public DbSet ServiceProviders { get; set; } public DbSet Products { get; set; } @@ -69,6 +71,8 @@ namespace TIAM.Database.DbContexts.Admins new TransferEntityTypeDefaultConfigurations().Configure(modelBuilder.Entity()); modelBuilder.Entity().Navigation(e => e.Address).AutoInclude(true); + + modelBuilder.Entity().Navigation(e => e.Car).AutoInclude(true); modelBuilder.Entity().HasOne(e => e.TransferDestination); modelBuilder.Entity().Navigation(e => e.TransferDestination).AutoInclude(true); diff --git a/TIAM.Database/DbSets/Transfers/TransferDbSetExtensions.cs b/TIAM.Database/DbSets/Transfers/TransferDbSetExtensions.cs index e745cd2d..d8622d2d 100644 --- a/TIAM.Database/DbSets/Transfers/TransferDbSetExtensions.cs +++ b/TIAM.Database/DbSets/Transfers/TransferDbSetExtensions.cs @@ -18,13 +18,13 @@ public static class TransferDbSetExtensions public static bool RemoveTransfer(this ITransferDbSet ctx, Guid transferId) { - var transfer = ctx.GetTransferById(transferId, false); + var transfer = ctx.GetTransferById(transferId); return transfer == null || ctx.RemoveTransfer(transfer); } #endregion Add, Update, Remove - public static Transfer? GetTransferById(this ITransferDbSet ctx, Guid transferId, bool autoInclude = false) + public static Transfer? GetTransferById(this ITransferDbSet ctx, Guid transferId) => ctx.Transfers.FirstOrDefault(x => x.Id == transferId); public static IQueryable GetTransfers(this ITransferDbSet ctx) diff --git a/TIAM.Database/ModelBuilders/Transfers/TransferEntityTypeBuilderExtensions.cs b/TIAM.Database/ModelBuilders/Transfers/TransferEntityTypeBuilderExtensions.cs index 46310f81..6dab7ab9 100644 --- a/TIAM.Database/ModelBuilders/Transfers/TransferEntityTypeBuilderExtensions.cs +++ b/TIAM.Database/ModelBuilders/Transfers/TransferEntityTypeBuilderExtensions.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; using TIAM.Database.ModelBuilders.Users; using TIAM.Entities.Transfers; using TIAM.Entities.Users; @@ -8,16 +9,17 @@ namespace TIAM.Database.ModelBuilders.Transfers; public static class TransferEntityTypeBuilderExtensions { /// - /// Mapping Transfer to UserProductMapping relation + /// Mapping Transfer to Drivers relation /// - public static void BuildTransferToProductMappingToRelation(this EntityTypeBuilder modelBuilder, bool autoInclude) + public static void BuildTransferToDriversMappingToRelation(this EntityTypeBuilder modelBuilder, bool autoInclude) { modelBuilder - .HasOne(x => x.UserProductMapping) - .WithMany(x => x.Transfers) - .HasForeignKey(x => x.UserProductMappingId); + .HasMany(x => x.TransferToDrivers) + .WithOne(x => x.Transfer) + .HasForeignKey(x=>x.TransferId) + .OnDelete(DeleteBehavior.Cascade); - modelBuilder.Navigation(e => e.UserProductMapping).AutoInclude(autoInclude); + modelBuilder.Navigation(e => e.TransferToDrivers).AutoInclude(autoInclude); } - + } \ No newline at end of file diff --git a/TIAM.Database/ModelBuilders/Transfers/TransferEntityTypeDefaultConfigurations.cs b/TIAM.Database/ModelBuilders/Transfers/TransferEntityTypeDefaultConfigurations.cs index ec2f60c1..87553cef 100644 --- a/TIAM.Database/ModelBuilders/Transfers/TransferEntityTypeDefaultConfigurations.cs +++ b/TIAM.Database/ModelBuilders/Transfers/TransferEntityTypeDefaultConfigurations.cs @@ -1,4 +1,5 @@ using AyCode.Database.DbContexts; +using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Builders; using TIAM.Entities.Products; using TIAM.Entities.Transfers; @@ -9,6 +10,9 @@ public class TransferEntityTypeDefaultConfigurations: IAcEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { - builder.BuildTransferToProductMappingToRelation(true); + builder.Property(e => e.OrderId).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore); + + //builder.BuildTransferToProductMappingToRelation(true); + builder.BuildTransferToDriversMappingToRelation(true); } } \ No newline at end of file diff --git a/TIAM.Database/ModelBuilders/Users/UserEntityTypeBuilderExtensions.cs b/TIAM.Database/ModelBuilders/Users/UserEntityTypeBuilderExtensions.cs index 1eac95e9..2bf7fbf9 100644 --- a/TIAM.Database/ModelBuilders/Users/UserEntityTypeBuilderExtensions.cs +++ b/TIAM.Database/ModelBuilders/Users/UserEntityTypeBuilderExtensions.cs @@ -73,11 +73,11 @@ public static class UserEntityTypeBuilderExtensions /// public static void BuildTransferToUserProductMappingRelation(this EntityTypeBuilder modelBuilder, bool autoInclude) { - modelBuilder - .HasMany(e => e.Transfers) - .WithOne(e => e.UserProductMapping).HasForeignKey(x => x.UserProductMappingId); + //modelBuilder + // .HasMany(e => e.Transfers) + // .WithOne(e => e.UserProductMapping).HasForeignKey(x => x.UserProductMappingId); - modelBuilder.Navigation(e => e.Transfers).AutoInclude(autoInclude); + //modelBuilder.Navigation(e => e.Transfers).AutoInclude(autoInclude); } #endregion UserProductMapping diff --git a/TIAM.Database/ModelBuilders/Users/UserEntityTypeConfigurations.cs b/TIAM.Database/ModelBuilders/Users/UserEntityTypeConfigurations.cs index a600ad0a..5c04b666 100644 --- a/TIAM.Database/ModelBuilders/Users/UserEntityTypeConfigurations.cs +++ b/TIAM.Database/ModelBuilders/Users/UserEntityTypeConfigurations.cs @@ -16,7 +16,7 @@ public class UserProductMappingEntityTypeDefaultConfiguration : IAcEntityTypeCon public void Configure(EntityTypeBuilder builder) { builder.BuildUserProductMappingToRelations(false); - builder.BuildTransferToUserProductMappingRelation(true); + //builder.BuildTransferToUserProductMappingRelation(true); } } diff --git a/TIAM.Entities/Transfers/Transfer.cs b/TIAM.Entities/Transfers/Transfer.cs index 68266e93..33e21de5 100644 --- a/TIAM.Entities/Transfers/Transfer.cs +++ b/TIAM.Entities/Transfers/Transfer.cs @@ -2,6 +2,7 @@ using AyCode.Interfaces.TimeStampInfo; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Dynamic; using TIAM.Core.Enums; using TIAM.Entities.Products; using TIAM.Entities.Users; @@ -9,20 +10,21 @@ using TIAM.Entities.Users; namespace TIAM.Entities.Transfers; [Table(nameof(Transfer))] -public class Transfer: IEntityGuid, ITimeStampInfo, IProductForeignKey, IUserProductMappingRelation, IUserForeignKey +public class Transfer: IEntityGuid, ITimeStampInfo, IProductForeignKey, IUserForeignKey { [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] public Guid Id { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int OrderId { get; } + public int OrderId { get; private set; } public Guid UserId { get; set; } public Guid ProductId { get; set; } - public Guid? UserProductMappingId { get; set; } - public Guid? UserProductToCarId { get; set; } + //public Guid? UserProductMappingId { get; set; } + //public Guid? UserProductToCarId { get; set; } - public virtual UserProductMapping? UserProductMapping { get; set; } + //public virtual UserProductMapping? UserProductMapping { get; set; } + public virtual List TransferToDrivers { get; set; } [Required] public TransferStatusType TransferStatusType { get; set; } = TransferStatusType.OrderSubmitted; diff --git a/TIAM.Entities/Transfers/TransferToDriver.cs b/TIAM.Entities/Transfers/TransferToDriver.cs index 6fb948f9..007b9c96 100644 --- a/TIAM.Entities/Transfers/TransferToDriver.cs +++ b/TIAM.Entities/Transfers/TransferToDriver.cs @@ -2,6 +2,7 @@ using System.ComponentModel.DataAnnotations.Schema; using AyCode.Interfaces.Entities; using AyCode.Interfaces.TimeStampInfo; +using TIAM.Entities.Drivers; using TIAM.Entities.Users; namespace TIAM.Entities.Transfers; @@ -16,9 +17,24 @@ public class TransferToDriver : IEntityGuid, ITimeStampInfo, IUserProductMapping public Guid UserProductMappingId { get; set; } public Guid CarId { get; set; } + public virtual Car Car { get; set; } + public virtual Transfer Transfer { get; set; } + public string LicencePlate { get; set; } public double Price { get; set; } public DateTime Created { get; set; } public DateTime Modified { get; set; } + + public TransferToDriver() {} + + public TransferToDriver(Guid id, Guid transferId, Guid userProductMappingId, Guid carId, string licencePlate, double price) + { + Id = id; + TransferId = transferId; + UserProductMappingId = userProductMappingId; + CarId = carId; + LicencePlate = licencePlate; + Price = price; + } } \ No newline at end of file diff --git a/TIAM.Entities/Users/UserProductMapping.cs b/TIAM.Entities/Users/UserProductMapping.cs index e1942db6..04539f0b 100644 --- a/TIAM.Entities/Users/UserProductMapping.cs +++ b/TIAM.Entities/Users/UserProductMapping.cs @@ -21,7 +21,7 @@ public class UserProductMapping : IEntityGuid, IUserRelation, IProductRelation, public virtual User User { get; set; } public virtual Product Product { get; set; } - public virtual List? Transfers { get; set; } + //public virtual List? Transfers { get; set; } public int Permissions { get; set; } = 1; public bool IsAdmin { get; set; } diff --git a/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs b/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs index c84367ce..3090035f 100644 --- a/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs +++ b/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs @@ -48,7 +48,7 @@ namespace TIAMWebApp.Server.Controllers _adminDal = adminDal; } - public double GetTransferPrice(Guid productId, Guid fromTransferDestinationId, Guid toTranferDestinationId) + public double GetTransferPrice(Guid productId, Guid fromTransferDestinationId, Guid toTranferDestinationId, in byte seatNumber) { throw new NotImplementedException(); } @@ -82,13 +82,14 @@ namespace TIAMWebApp.Server.Controllers public double GetSeatNumberPrice(in double price, in double? price2, in double? price3, in byte seatNumber) { - if (seatNumber >= (byte)SeatNumberPriceType.Price3SeatNum) - return price3 ?? price; //TODO: *seatnum percent - J. - - if (seatNumber >= (byte)SeatNumberPriceType.Price2SeatNum) - return price2 ?? price; //TODO: *seatnum percent - J. - - return price; + return seatNumber switch + { + >= (byte)SeatNumberPriceType.Price3SeatNum => price3 ?? price //TODO: price * seatnum percent - J. + , + >= (byte)SeatNumberPriceType.Price2SeatNum => price2 ?? price //TODO: price * seatnum percent - J. + , + _ => price + }; } //public double GetExtraPrice(bool? extraPriceType){}