diff --git a/TIAM.Database/DataLayers/Admins/AdminDal.cs b/TIAM.Database/DataLayers/Admins/AdminDal.cs index 585af77a..3690f339 100644 --- a/TIAM.Database/DataLayers/Admins/AdminDal.cs +++ b/TIAM.Database/DataLayers/Admins/AdminDal.cs @@ -23,6 +23,7 @@ using TIAM.Entities.Transfers; using TIAM.Entities.Users; using TIAM.Models.Dtos.Products; using AyCode.Database.DbSets.Profiles; +using TIAM.Database.DbSets.Drivers; namespace TIAM.Database.DataLayers.Admins { @@ -66,7 +67,7 @@ namespace TIAM.Database.DataLayers.Admins return ctx.UpdateTransfer(transfer); }); - public Task RemoveTransferAsync(Transfer transfer) => TransactionAsync(ctx => ctx.RemoveTransfer(transfer)); + public Task RemoveTransferAsync(Transfer transfer) => RemoveTransferAsync(transfer.Id); public Task RemoveTransferAsync(Guid transferId) => TransactionAsync(ctx => ctx.RemoveTransfer(transferId)); #endregion Transfer @@ -78,15 +79,15 @@ namespace TIAM.Database.DataLayers.Admins public Task AddTransferDestinationAsync(TransferDestination transferDestination) => TransactionAsync(ctx => ctx.AddTransferDestination(transferDestination)); public Task UpdateTransferDestinationAsync(TransferDestination transferDestination) => TransactionAsync(ctx => ctx.UpdateTransferDestination(transferDestination)); - public Task RemoveTransferDestinationAsync(TransferDestination transferDestination, bool removeAddress) => TransactionAsync(ctx => ctx.RemoveTransferDestination(transferDestination.Id, removeAddress)); + public Task RemoveTransferDestinationAsync(TransferDestination transferDestination, bool removeAddress) => RemoveTransferDestinationAsync(transferDestination.Id, removeAddress); public Task RemoveTransferDestinationAsync(Guid transferDestinationId, bool removeAddress) => TransactionAsync(ctx => ctx.RemoveTransferDestination(transferDestinationId, removeAddress)); #endregion TransferDestination #region TransferToDriver - public Task GetTransferToDriverByIdAsync(Guid transferToDriverId, bool autoInclude = false) => SessionAsync(ctx => ctx.TransferToDrivers.FirstOrDefault(x=>x.Id == transferToDriverId)); - public Task> GetTransferToDriversByTransferIdAsync(Guid transferId, bool autoInclude = false) => SessionAsync(ctx => ctx.TransferToDrivers.Where(x => x.TransferId == transferId).ToList()); + public Task GetTransferToDriverByIdAsync(Guid transferToDriverId, bool autoInclude = true) => SessionAsync(ctx => ctx.GetTransferToDriverById(transferToDriverId, autoInclude)); + public Task> GetTransferToDriversByTransferIdAsync(Guid transferId, bool autoInclude = true) => SessionAsync(ctx => ctx.GetTransferToDriversByTransferId(transferId, autoInclude).ToList()); - public Task AddTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.TransferToDrivers.Add(transferToDriver).State == EntityState.Added); + public Task AddTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.AddTransferToDriver(transferToDriver)); public async Task UpdateTransferToDriverAsync(TransferToDriver transferToDriver) { var transferToDriverId = transferToDriver.Id; @@ -104,14 +105,14 @@ namespace TIAM.Database.DataLayers.Admins return result ? transferToDriver : null; } - public Task RemoveTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.TransferToDrivers.Remove(transferToDriver).State == EntityState.Deleted); + public Task RemoveTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.RemoveTransferToDriver(transferToDriver.Id)); #endregion TransferToDriver #region Drivers - - public Task> GetTAllDrivers() => SessionAsync(ctx => ctx.UserProductMappings.Where(x => ctx.Cars.Any(car => car.UserProductMappingId == x.Id)).ToList()); - public Task> GetTAllDriversByProductId(Guid productId) => SessionAsync(ctx => ctx.UserProductMappings.Where(x => x.ProductId == productId && ctx.Cars.Any(car => car.UserProductMappingId == x.Id)).ToList()); + public Task> GetAllDriversAsync(bool autoInclude = true) => SessionAsync(ctx => ctx.GetAllDrivers(autoInclude).ToList()); + public Task> GetAllDriversByProductIdAsync(Guid productId, bool autoInclude = true) => SessionAsync(ctx => ctx.GetAllDriversByProductId(productId, autoInclude).ToList()); #endregion Drivers + #region TransferDestinationToProduct public TransferDestinationToProduct? GetTransferDestinationToProductById(Guid transferDestinationToProductId) => Session(ctx=>ctx.GetTransferDestinationToProductById(transferDestinationToProductId)); public Task GetTransferDestinationToProductByIdAsync(Guid transferDestinationToProductId) => SessionAsync(ctx=>ctx.GetTransferDestinationToProductById(transferDestinationToProductId)); @@ -125,7 +126,7 @@ namespace TIAM.Database.DataLayers.Admins public Task AddTransferDestinationToProductAsync(TransferDestinationToProduct transferDestinationToProduct) => TransactionAsync(ctx => ctx.AddTransferDestinationToProduct(transferDestinationToProduct)); public Task UpdateTransferDestinationToProductAsync(TransferDestinationToProduct transferDestinationToProduct) => TransactionAsync(ctx => ctx.UpdateTransferDestinationToProduct(transferDestinationToProduct)); - public Task RemoveTransferDestinationToProductAsync(TransferDestinationToProduct transferDestinationToProduct) => TransactionAsync(ctx => ctx.RemoveTransferDestinationToProduct(transferDestinationToProduct.Id)); + public Task RemoveTransferDestinationToProductAsync(TransferDestinationToProduct transferDestinationToProduct) => RemoveTransferDestinationToProductAsync(transferDestinationToProduct.Id); public Task RemoveTransferDestinationToProductAsync(Guid transferDestinationToProductId) => TransactionAsync(ctx => ctx.RemoveTransferDestinationToProduct(transferDestinationToProductId)); #endregion TransferDestinationToProduct @@ -158,7 +159,7 @@ namespace TIAM.Database.DataLayers.Admins public Task AddProductAsync(Product product) => TransactionAsync(ctx => ctx.AddProduct(product)); public Task UpdateProductAsync(Product product) => TransactionAsync(ctx => ctx.UpdateProduct(product)); - //public Task RemoveProductAsync(Product product) => TransactionAsync(ctx => ctx.RemoveProduct(product.Id)); + public Task RemoveProductAsync(Product product) => RemoveProductAsync(product.Id); public Task RemoveProductAsync(Guid productId) => TransactionAsync(ctx => ctx.RemoveProduct(productId)); public UserProductMapping? GetUserProductMappingById(Guid userProductMappingId, bool autoInclude = true) => Session(ctx => ctx.GetUserProductMappingById(userProductMappingId, autoInclude)); @@ -217,9 +218,7 @@ namespace TIAM.Database.DataLayers.Admins } public Task RemoveUserProductMappingAsync(Guid userProductMappingId) => TransactionAsync(ctx => ctx.RemoveUserProductMapping(userProductMappingId)); - - public Task RemoveUserProductMappingAsync(Guid userId, Guid productId) - => TransactionAsync(ctx => ctx.RemoveUserProductMapping(userId, productId)); + public Task RemoveUserProductMappingAsync(Guid userId, Guid productId) => TransactionAsync(ctx => ctx.RemoveUserProductMapping(userId, productId)); #endregion UserProductMapping diff --git a/TIAM.Database/DbContexts/Admins/IAdminDbContext.cs b/TIAM.Database/DbContexts/Admins/IAdminDbContext.cs index 0aa15e51..f3884e5c 100644 --- a/TIAM.Database/DbContexts/Admins/IAdminDbContext.cs +++ b/TIAM.Database/DbContexts/Admins/IAdminDbContext.cs @@ -1,4 +1,5 @@ using AyCode.Database.DbContexts.Users; +using TIAM.Database.DbSets.Drivers; using TIAM.Database.DbSets.Emails; using TIAM.Database.DbSets.Permissions; using TIAM.Database.DbSets.Products; @@ -14,7 +15,7 @@ using TIAM.Entities.Users; namespace TIAM.Database.DbContexts.Admins; public interface IAdminDbContext : - ICompanyDbSet, IProductDbSet, IUserProductMappingDbSet, + ICompanyDbSet, IProductDbSet, IDriverDbSet, IAcUserDbContextBase, IPermissionsDbSetContext, ITransferDestinationDbSet, ITransferDbSet, IEmailMessageDbSet { diff --git a/TIAM.Database/DbSets/Cars/ICarDbSet.cs b/TIAM.Database/DbSets/Cars/ICarDbSet.cs new file mode 100644 index 00000000..ad3e973a --- /dev/null +++ b/TIAM.Database/DbSets/Cars/ICarDbSet.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using TIAM.Entities.Drivers; + +namespace TIAM.Database.DbSets.Cars; + +public interface ICarDbSet +{ + public DbSet Cars { get; set; } +} \ No newline at end of file diff --git a/TIAM.Database/DbSets/Drivers/DriverDbSetExtensions.cs b/TIAM.Database/DbSets/Drivers/DriverDbSetExtensions.cs new file mode 100644 index 00000000..4c31cc85 --- /dev/null +++ b/TIAM.Database/DbSets/Drivers/DriverDbSetExtensions.cs @@ -0,0 +1,18 @@ +using AyCode.Core.Extensions; +using Microsoft.EntityFrameworkCore; +using TIAM.Database.DbSets.Transfers; +using TIAM.Database.DbSets.Users; +using TIAM.Entities.Drivers; +using TIAM.Entities.Transfers; +using TIAM.Entities.Users; + +namespace TIAM.Database.DbSets.Drivers; + +public static class DriverDbSetExtensions +{ + public static IQueryable GetAllDrivers(this IDriverDbSet ctx, bool autoInclude = true) + => ctx.UserProductMappingsWithRelations(autoInclude).Where(x => ctx.Cars.Any(car => car.UserProductMappingId == x.Id)); + + public static IQueryable GetAllDriversByProductId(this IDriverDbSet ctx, Guid productId, bool autoInclude = true) + => ctx.GetAllDrivers(autoInclude).Where(x => x.ProductId == productId); +} \ No newline at end of file diff --git a/TIAM.Database/DbSets/Drivers/IDriverDbSet.cs b/TIAM.Database/DbSets/Drivers/IDriverDbSet.cs new file mode 100644 index 00000000..a5881652 --- /dev/null +++ b/TIAM.Database/DbSets/Drivers/IDriverDbSet.cs @@ -0,0 +1,9 @@ +using TIAM.Database.DbSets.Cars; +using TIAM.Database.DbSets.Transfers; +using TIAM.Database.DbSets.Users; +using TIAM.Entities.Users; + +namespace TIAM.Database.DbSets.Drivers; + +public interface IDriverDbSet : IUserProductMappingDbSet, ICarDbSet, ITransferToDriverDbSet, ITransferDbSet +{ } \ No newline at end of file diff --git a/TIAM.Database/DbSets/Transfers/ITransferDbSet.cs b/TIAM.Database/DbSets/Transfers/ITransferDbSet.cs index 013f3356..41d947d3 100644 --- a/TIAM.Database/DbSets/Transfers/ITransferDbSet.cs +++ b/TIAM.Database/DbSets/Transfers/ITransferDbSet.cs @@ -3,7 +3,7 @@ using TIAM.Entities.Transfers; namespace TIAM.Database.DbSets.Transfers; -public interface ITransferDbSet +public interface ITransferDbSet : ITransferToDriverDbSet { public DbSet Transfers { get; set; } } \ No newline at end of file diff --git a/TIAM.Database/DbSets/Transfers/ITransferToDriverDbSet.cs b/TIAM.Database/DbSets/Transfers/ITransferToDriverDbSet.cs new file mode 100644 index 00000000..6cca1a83 --- /dev/null +++ b/TIAM.Database/DbSets/Transfers/ITransferToDriverDbSet.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using TIAM.Entities.Transfers; + +namespace TIAM.Database.DbSets.Transfers; + +public interface ITransferToDriverDbSet +{ + public DbSet TransferToDrivers { get; set; } +} \ No newline at end of file diff --git a/TIAM.Database/DbSets/Transfers/TransferDbSetExtensions.cs b/TIAM.Database/DbSets/Transfers/TransferDbSetExtensions.cs index 0d396706..9d784d40 100644 --- a/TIAM.Database/DbSets/Transfers/TransferDbSetExtensions.cs +++ b/TIAM.Database/DbSets/Transfers/TransferDbSetExtensions.cs @@ -13,8 +13,12 @@ public static class TransferDbSetExtensions public static bool UpdateTransfer(this ITransferDbSet ctx, Transfer transfer) => ctx.Transfers.Update(transfer).State == EntityState.Modified; - public static bool RemoveTransfer(this ITransferDbSet ctx, Transfer transfer) - => ctx.Transfers.Remove(transfer).State == EntityState.Deleted; + private static bool RemoveTransfer(this ITransferDbSet ctx, Transfer transfer) + { + ctx.TransferToDrivers.RemoveRange(ctx.TransferToDrivers.Where(x => x.TransferId == transfer.Id)); + + return ctx.Transfers.Remove(transfer).State == EntityState.Deleted; + } public static bool RemoveTransfer(this ITransferDbSet ctx, Guid transferId) { diff --git a/TIAM.Database/DbSets/Transfers/TransferToDriverDbSetExtensions.cs b/TIAM.Database/DbSets/Transfers/TransferToDriverDbSetExtensions.cs new file mode 100644 index 00000000..a73905d4 --- /dev/null +++ b/TIAM.Database/DbSets/Transfers/TransferToDriverDbSetExtensions.cs @@ -0,0 +1,40 @@ +using Microsoft.EntityFrameworkCore; +using TIAM.Core.Enums; +using TIAM.Database.DbSets.Drivers; +using TIAM.Database.DbSets.Users; +using TIAM.Entities.Transfers; +using TIAM.Entities.Users; + +namespace TIAM.Database.DbSets.Transfers; + +public static class TransferToDriverDbSetExtensions +{ + #region TransferToDriver + public static TransferToDriver? GetTransferToDriverById(this ITransferToDriverDbSet ctx, Guid transferToDriverId, bool autoInclude = true) + => ctx.TransferToDrivers.FirstOrDefault(x => x.Id == transferToDriverId); + + public static IQueryable GetTransferToDriversByTransferId(this ITransferToDriverDbSet ctx, Guid transferId, bool autoInclude = true) + => ctx.TransferToDrivers.Where(x => x.TransferId == transferId); + + public static bool AddTransferToDriver(this IDriverDbSet ctx, TransferToDriver transferToDriver) + { + var transfer = ctx.GetTransferById(transferToDriver.TransferId)!; + transfer.TransferStatusType = TransferStatusType.AssignedToDriver; + + return ctx.TransferToDrivers.Add(transferToDriver).State == EntityState.Added; + } + + private static bool RemoveTransferToDriver(this ITransferToDriverDbSet ctx, TransferToDriver transferToDriver) + { + //TODO: TransferStatusType change, ha nincs sofÅ‘r a törlés után! - J. + return ctx.TransferToDrivers.Remove(transferToDriver).State == EntityState.Deleted; + } + + public static bool RemoveTransferToDriver(this ITransferToDriverDbSet ctx, Guid transferToDriverId) + { + var transferToDriver = ctx.GetTransferToDriverById(transferToDriverId); + return transferToDriver == null || ctx.RemoveTransferToDriver(transferToDriver); + } + + #endregion TransferToDriver +} \ No newline at end of file diff --git a/TIAM.Database/DbSets/Users/UserProductMappingDbSetExtensions.cs b/TIAM.Database/DbSets/Users/UserProductMappingDbSetExtensions.cs index 25be5147..e4b79ae3 100644 --- a/TIAM.Database/DbSets/Users/UserProductMappingDbSetExtensions.cs +++ b/TIAM.Database/DbSets/Users/UserProductMappingDbSetExtensions.cs @@ -25,7 +25,6 @@ public static class UserProductMappingDbSetExtensions public static IQueryable GetUserProductMappingsByProductId(this IUserProductMappingDbSet ctx, Guid productId, bool autoInclude = true) => ctx.UserProductMappingsWithRelations(autoInclude).Where(x => x.ProductId == productId); - public static bool AddUserProductMapping(this IUserProductMappingDbSet ctx, UserProductMapping userProductMapping) { if (userProductMapping.UserId.IsNullOrEmpty() || userProductMapping.ProductId.IsNullOrEmpty() || userProductMapping.Permissions < 0) diff --git a/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs b/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs index 3bf2cafc..1b0f7c12 100644 --- a/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs +++ b/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs @@ -588,7 +588,7 @@ namespace TIAMWebApp.Server.Controllers [SignalR(SignalRTags.GetAllDrivers)] public async Task> GetAllDrivers() { - var result = await _adminDal.GetTAllDrivers(); + var result = await _adminDal.GetAllDriversAsync(); return result; } @@ -598,7 +598,7 @@ namespace TIAMWebApp.Server.Controllers [SignalR(SignalRTags.GetAllDriversByProductId)] public async Task> GetAllDriversByProductId(Guid productId) { - var result = await _adminDal.GetTAllDriversByProductId(productId); + var result = await _adminDal.GetAllDriversByProductIdAsync(productId); return result; } @@ -629,6 +629,8 @@ namespace TIAMWebApp.Server.Controllers public async Task AddTransferDriver([FromBody] TransferToDriver transferToDriver) { var result = await _adminDal.AddTransferToDriverAsync(transferToDriver); + //TODO: Send email to driver... - J. + return result ? transferToDriver : null; } @@ -639,6 +641,8 @@ namespace TIAMWebApp.Server.Controllers public async Task UpdateTransferDriver([FromBody] TransferToDriver transferToDriver) { var result = await _adminDal.UpdateTransferToDriverAsync(transferToDriver); + //TODO: Send email to driver/user, ha van változás... - J. + return result; // ? transferToDriver : null; } @@ -649,6 +653,8 @@ namespace TIAMWebApp.Server.Controllers public async Task RemoveTransferDriver([FromBody] TransferToDriver transferToDriver) { var result = await _adminDal.RemoveTransferToDriverAsync(transferToDriver); + //TODO: Send email to driver/user... - J. + return result ? transferToDriver : null; }