Transfer, TransferToDriver, Car, UserProductMapping relations, etc

This commit is contained in:
jozsef.b@aycode.com 2024-03-28 18:54:32 +01:00
parent 7e1903bbea
commit 8c0f89395b
12 changed files with 119 additions and 48 deletions

View File

@ -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");

View File

@ -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<Car> 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<bool> AddTransferAsync(Transfer transfer) => TransactionAsync(ctx => ctx.AddTransfer(transfer));
public Task<bool> UpdateTransferAsync(Transfer transfer) => TransactionAsync(ctx => ctx.UpdateTransfer(transfer));
public Task<bool> UpdateTransferAsync(Transfer transfer, TransferToDriver transferToDriver) => UpdateTransferAsync(transfer, [transferToDriver]);
public Task<bool> UpdateTransferAsync(Transfer transfer, List<TransferToDriver> transferToDrivers)
=> TransactionAsync(ctx =>
{
ctx.AddRange(transferToDrivers);
ctx.SaveChanges();
return ctx.UpdateTransfer(transfer);
});
public Task<bool> RemoveTransferAsync(Transfer transfer) => TransactionAsync(ctx => ctx.RemoveTransfer(transfer));
public Task<bool> 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<bool> AddTransferDestinationAsync(TransferDestination transferDestination) => TransactionAsync(ctx => ctx.AddTransferDestination(transferDestination));
@ -47,6 +63,12 @@ namespace TIAM.Database.DataLayers.Admins
public Task<bool> RemoveTransferDestinationAsync(Guid transferDestinationId, bool removeAddress = false) => TransactionAsync(ctx => ctx.RemoveTransferDestination(transferDestinationId, removeAddress));
#endregion TransferDestination
#region TransferToDriver
public Task<bool> AddTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.TransferToDrivers.Add(transferToDriver).State == EntityState.Added);
public Task<bool> UpdateTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.TransferToDrivers.Update(transferToDriver).State == EntityState.Modified);
public Task<bool> 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());

View File

@ -27,6 +27,8 @@ namespace TIAM.Database.DbContexts.Admins
public DbSet<TransferDestination> TransferDestinations { get; set; }
public DbSet<TransferDestinationToProduct> TransferDestinationToProducts { get; set; }
public DbSet<Transfer> Transfers { get; set; }
public DbSet<TransferToDriver> TransferToDrivers { get; set; }
public DbSet<Car> Cars { get; set; }
public DbSet<TiamServiceProvider> ServiceProviders { get; set; }
public DbSet<Product> Products { get; set; }
@ -69,6 +71,8 @@ namespace TIAM.Database.DbContexts.Admins
new TransferEntityTypeDefaultConfigurations().Configure(modelBuilder.Entity<Transfer>());
modelBuilder.Entity<TransferDestination>().Navigation(e => e.Address).AutoInclude(true);
modelBuilder.Entity<TransferToDriver>().Navigation(e => e.Car).AutoInclude(true);
modelBuilder.Entity<TransferDestinationToProduct>().HasOne(e => e.TransferDestination);
modelBuilder.Entity<TransferDestinationToProduct>().Navigation(e => e.TransferDestination).AutoInclude(true);

View File

@ -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<Transfer> GetTransfers(this ITransferDbSet ctx)

View File

@ -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
{
/// <summary>
/// Mapping Transfer to UserProductMapping relation
/// Mapping Transfer to Drivers relation
/// </summary>
public static void BuildTransferToProductMappingToRelation(this EntityTypeBuilder<Transfer> modelBuilder, bool autoInclude)
public static void BuildTransferToDriversMappingToRelation(this EntityTypeBuilder<Transfer> 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);
}
}

View File

@ -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<Transfer> builder)
{
builder.BuildTransferToProductMappingToRelation(true);
builder.Property(e => e.OrderId).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);
//builder.BuildTransferToProductMappingToRelation(true);
builder.BuildTransferToDriversMappingToRelation(true);
}
}

View File

@ -73,11 +73,11 @@ public static class UserEntityTypeBuilderExtensions
/// </summary>
public static void BuildTransferToUserProductMappingRelation(this EntityTypeBuilder<UserProductMapping> 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

View File

@ -16,7 +16,7 @@ public class UserProductMappingEntityTypeDefaultConfiguration : IAcEntityTypeCon
public void Configure(EntityTypeBuilder<UserProductMapping> builder)
{
builder.BuildUserProductMappingToRelations(false);
builder.BuildTransferToUserProductMappingRelation(true);
//builder.BuildTransferToUserProductMappingRelation(true);
}
}

View File

@ -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<Guid?>, 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<TransferToDriver> TransferToDrivers { get; set; }
[Required] public TransferStatusType TransferStatusType { get; set; } = TransferStatusType.OrderSubmitted;

View File

@ -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;
}
}

View File

@ -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<Transfer>? Transfers { get; set; }
//public virtual List<Transfer>? Transfers { get; set; }
public int Permissions { get; set; } = 1;
public bool IsAdmin { get; set; }

View File

@ -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){}