From 0a02411eafda922243d36ba3d474ae22ad0fbf82 Mon Sep 17 00:00:00 2001 From: Loretta Date: Thu, 27 Jun 2024 19:41:13 +0200 Subject: [PATCH] TransferToDriver ComboBox, improvements, fixes, etc... --- TIAM.Core/Consts/TiamConst.cs | 5 + TIAM.Database/DataLayers/Admins/AdminDal.cs | 26 +++- TIAM.Services/SignalRTags.cs | 24 ++-- .../Pages/User/SysAdmins/ManageDrivers.razor | 7 +- .../TransferToDriverGridComponent.razor | 135 ++++++++++++++++-- .../ServiceProviderAPIController.cs | 16 ++- .../Controllers/TransferDataAPIController.cs | 22 ++- 7 files changed, 201 insertions(+), 34 deletions(-) diff --git a/TIAM.Core/Consts/TiamConst.cs b/TIAM.Core/Consts/TiamConst.cs index 29b54de5..14b72abd 100644 --- a/TIAM.Core/Consts/TiamConst.cs +++ b/TIAM.Core/Consts/TiamConst.cs @@ -2,6 +2,11 @@ namespace TIAM.Core.Consts; +public static class TiamConstClient +{ + public static Guid TransferProductId = Guid.Parse("814b5495-c2e9-4f1d-a73f-37cd5d353078"); +} + public class TiamConst : AcConst { public static string ProjectIdString = "684f34d1-163a-4077-918f-a9d9df5ce789"; diff --git a/TIAM.Database/DataLayers/Admins/AdminDal.cs b/TIAM.Database/DataLayers/Admins/AdminDal.cs index d2a517b2..585af77a 100644 --- a/TIAM.Database/DataLayers/Admins/AdminDal.cs +++ b/TIAM.Database/DataLayers/Admins/AdminDal.cs @@ -34,9 +34,10 @@ namespace TIAM.Database.DataLayers.Admins #region Car public Task> GetAllCarsAsync() => SessionAsync(ctx => ctx.Cars.OrderBy(x => x.Manufacture).ThenBy(x => x.CarModel).ToList()); + public Task> GetAllCarsbyProductIdAsync(Guid productId) => SessionAsync(ctx => ctx.Cars.Where(x => x.UserProductMapping.ProductId == productId).OrderBy(x => x.Manufacture).ThenBy(x => x.CarModel).ToList()); 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()); - public Task> GetCarByUserProductMappingIdAsync(Guid userProductMappingId) => SessionAsync(ctx => ctx.Cars.Where(x => x.UserProductMappingId == userProductMappingId).ToList()); + public Task> GetCarsByUserProductMappingIdAsync(Guid userProductMappingId) => SessionAsync(ctx => ctx.Cars.Where(x => x.UserProductMappingId == userProductMappingId).ToList()); public Task AddCarAsync(Car car) => TransactionAsync(ctx => ctx.Cars.Add(car).State == EntityState.Added); public Task UpdateCarAsync(Car car) => TransactionAsync(ctx => ctx.Cars.Update(car).State == EntityState.Modified); public Task RemoveCarAsync(Car car) => TransactionAsync(ctx => ctx.Cars.Remove(car).State == EntityState.Deleted); @@ -86,10 +87,31 @@ namespace TIAM.Database.DataLayers.Admins public Task> GetTransferToDriversByTransferIdAsync(Guid transferId, bool autoInclude = false) => SessionAsync(ctx => ctx.TransferToDrivers.Where(x => x.TransferId == transferId).ToList()); 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 async Task UpdateTransferToDriverAsync(TransferToDriver transferToDriver) + { + var transferToDriverId = transferToDriver.Id; + var result = await TransactionAsync(ctx => + { + var transferToDriver2 = ctx.TransferToDrivers.FirstOrDefault(x => x.Id == transferToDriverId)!; + transferToDriver2.CarId = transferToDriver.CarId; + transferToDriver2.LicencePlate = transferToDriver.LicencePlate; + transferToDriver2.UserProductMappingId = transferToDriver.UserProductMappingId; + transferToDriver2.TransferId = transferToDriver.TransferId; + + return ctx.TransferToDrivers.Update(transferToDriver2).State == EntityState.Modified; + }); + + return result ? transferToDriver : null; + } + public Task RemoveTransferToDriverAsync(TransferToDriver transferToDriver) => TransactionAsync(ctx => ctx.TransferToDrivers.Remove(transferToDriver).State == EntityState.Deleted); #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()); + #endregion Drivers #region TransferDestinationToProduct public TransferDestinationToProduct? GetTransferDestinationToProductById(Guid transferDestinationToProductId) => Session(ctx=>ctx.GetTransferDestinationToProductById(transferDestinationToProductId)); public Task GetTransferDestinationToProductByIdAsync(Guid transferDestinationToProductId) => SessionAsync(ctx=>ctx.GetTransferDestinationToProductById(transferDestinationToProductId)); diff --git a/TIAM.Services/SignalRTags.cs b/TIAM.Services/SignalRTags.cs index ce41ce85..d0265c46 100644 --- a/TIAM.Services/SignalRTags.cs +++ b/TIAM.Services/SignalRTags.cs @@ -26,18 +26,19 @@ public class SignalRTags : AcSignalRTags public const int RemoveCompany = 18; public const int GetTransferDriver = 22; - //public const int GetTransferToDrivers = 23; - public const int GetTransferDriversByTransferId = 24; - public const int AddTransferToDriver = 25; - public const int UpdateTransferToDriver = 26; - public const int RemoveTransferToDriver = 27; + public const int GetAllDrivers = 23; + public const int GetAllDriversByProductId = 24; + public const int GetTransferDriversByTransferId = 25; + public const int AddTransferToDriver = 26; + public const int UpdateTransferToDriver = 27; + public const int RemoveTransferToDriver = 28; - public const int GetAddress = 28; - //public const int GetAddresses = 29; - public const int GetAddressesByContextId = 30; - public const int UpdateAddress = 31; - //public const int AddAddress = 32; - //public const int RemoveAddress = 33; + public const int GetAddress = 29; + //public const int GetAddresses = 30; + public const int GetAddressesByContextId = 31; + public const int UpdateAddress = 32; + //public const int AddAddress = 33; + //public const int RemoveAddress = 34; public const int GetProfileById = 35; //public const int GetProfiles = 36; @@ -61,6 +62,7 @@ public class SignalRTags : AcSignalRTags public const int UpdateCar = 52; public const int DeleteCar = 53; public const int GetAllCars = 54; + public const int GetAllCarsByProductId = 55; public const int GetMessagesByContextId = 60; public const int GetAllMessages = 61; diff --git a/TIAMSharedUI/Pages/User/SysAdmins/ManageDrivers.razor b/TIAMSharedUI/Pages/User/SysAdmins/ManageDrivers.razor index b8ec745d..9041f9ce 100644 --- a/TIAMSharedUI/Pages/User/SysAdmins/ManageDrivers.razor +++ b/TIAMSharedUI/Pages/User/SysAdmins/ManageDrivers.razor @@ -11,6 +11,7 @@ @using TIAMWebApp.Shared.Application.Models.PageModels @using TIAMWebApp.Shared.Application.Utility @using AyCode.Services.Loggers +@using TIAM.Core.Consts @layout AdminLayout @inject IEnumerable LogWriters @inject IStringLocalizer Localizer @@ -37,7 +38,7 @@ Click="ColumnChooserButton_Click" /> - + @@ -54,9 +55,7 @@ @code { private LoggerClient _logger; //public UserModelDtoDetail UserModelDtoDetail = new(); - - private string TransferProductId = "814b5495-c2e9-4f1d-a73f-37cd5d353078"; - + IGrid Grid { get; set; } //object? MasterGridData { get; set; } diff --git a/TIAMSharedUI/Pages/User/SysAdmins/TransferToDriverGridComponent.razor b/TIAMSharedUI/Pages/User/SysAdmins/TransferToDriverGridComponent.razor index bf0121e7..66110c20 100644 --- a/TIAMSharedUI/Pages/User/SysAdmins/TransferToDriverGridComponent.razor +++ b/TIAMSharedUI/Pages/User/SysAdmins/TransferToDriverGridComponent.razor @@ -13,6 +13,8 @@ @using AyCode.Core @using AyCode.Core.Helpers @using AyCode.Services.SignalRs +@using AyCode.Utils.Extensions +@using TIAM.Core.Consts @using TIAM.Entities.Users @using TIAM.Services @inject IUserDataService UserDataService @@ -20,7 +22,7 @@ @inject AdminSignalRClient AdminSignalRClient; - - @* + + @{ + var userEmailFieldName = $"{nameof(TransferToDriver.UserProductMapping)}.{nameof(UserProductMapping.User)}.{nameof(User.EmailAddress)}"; + var userEmailFieldName2 = $"{nameof(UserProductMapping.User)}.{nameof(User.EmailAddress)}"; + } + + + @{ + var transferToDriverDataItem = (TransferToDriver)context.DataItem; + var transferToDriverEditModel = (TransferToDriver)context.EditModel; + } + + + + @* *@ + + + + + + + + + + @{ + var transferToDriver = (TransferToDriver)context.DataItem; + @_cars.FirstOrDefault(x => x.Id == transferToDriver.CarId)?.LicencePlate + } + + + @{ + var transferToDriverEditModel = (TransferToDriver)context.EditModel; + } + + + + @* *@ + + + + + + + + + +@* + + @{ + var transferToDriverId = ((TransferToDriver)context.DataItem).Id; + } + + + + + + + + + *@ + @* - *@ + + - - @{ - var userEmailFieldName = $"{nameof(TransferToDriver.UserProductMapping)}.{nameof(UserProductMapping.User)}.{nameof(User.EmailAddress)}"; - } - - + @* *@ @code { - [Parameter] public Guid? ContextId { get; set; } + [Parameter] public Guid ContextId { get; set; } [Parameter] public ITransferToDriversRelation ParentData { get; set; } = null!; [Parameter] public EventCallback OnTransferToDriverChanged { get; set; } @@ -60,6 +135,7 @@ private LoggerClient _logger = null!; private List _cars = []; + private List _drivers = []; protected override async Task OnInitializedAsync() { @@ -67,7 +143,22 @@ _logger.Info($"DetailGridData: {ParentData.TransferToDrivers.Count}"); - //_cars.AddRange((await AdminSignalRClient.GetAllAsync>(SignalRTags.GetAllCars))!); + //EZ NEM JÓ, FILTER-ELNI KELL A PRODUCT-RA!!! - J. + _cars.AddRange((await AdminSignalRClient.GetAllAsync>(SignalRTags.GetAllCarsByProductId, TiamConstClient.TransferProductId))!); + // AdminSignalRClient.GetAllAsync>(SignalRTags.GetAllCars, response => + // { + // _cars.AddRange(response.ResponseData!); + // return Task.CompletedTask; + // }).Forget(); + + //EZ NEM JÓ, FILTER-ELNI KELL A PRODUCT-RA!!! - J. + _drivers.AddRange(_cars.DistinctBy(x => x.UserProductMappingId).Select(x => x.UserProductMapping)); + //_drivers.AddRange((await AdminSignalRClient.GetAllAsync>(SignalRTags.GetAllDrivers))!); + // AdminSignalRClient.GetAllAsync>(SignalRTags.GetAllDrivers, response => + // { + // _drivers.AddRange(response.ResponseData!); + // return Task.CompletedTask; + // }).Forget(); await base.OnInitializedAsync(); } @@ -97,12 +188,28 @@ OnTransferToDriverChanged.InvokeAsync(args.DataItem); } - private void DataItemSaving(GridEditModelSavingEventArgs obj) + private void DataItemSaving(GridEditModelSavingEventArgs args) { _logger.Debug($"DataItemSaving"); + + var transferToDriverEditModel = (TransferToDriver)args.EditModel; + + if (transferToDriverEditModel.UserProductMappingId.IsNullOrEmpty() || transferToDriverEditModel.CarId.IsNullOrEmpty() || + transferToDriverEditModel.LicencePlate.IsNullOrWhiteSpace() || transferToDriverEditModel.Price <= 0) + { + args.Cancel = true; + _logger.Error($"transferToDriverEditModel.UserProductMappingId.IsNullOrEmpty() || transferToDriverEditModel.CarId.IsNullOrEmpty() || transferToDriverEditModel.LicencePlate.IsNullOrWhiteSpace() || transferToDriverEditModel.Price <= 0"); + } + + if (args.IsNew) + { + transferToDriverEditModel.TransferId = ContextId; + transferToDriverEditModel.Id = Guid.NewGuid(); + transferToDriverEditModel.Car = null; + } } - private void DataItemDeleting(GridDataItemDeletingEventArgs obj) + private void DataItemDeleting(GridDataItemDeletingEventArgs args) { _logger.Debug($"DataItemDeleting"); } diff --git a/TIAMWebApp/Server/Controllers/ServiceProviderAPIController.cs b/TIAMWebApp/Server/Controllers/ServiceProviderAPIController.cs index f9ceed0d..c17cd644 100644 --- a/TIAMWebApp/Server/Controllers/ServiceProviderAPIController.cs +++ b/TIAMWebApp/Server/Controllers/ServiceProviderAPIController.cs @@ -285,7 +285,7 @@ namespace TIAMWebApp.Server.Controllers { _logger.Info($@"GetCarsForUserProductMapping called with userProductMappingId: {userProductMappingId}"); - var cars = await adminDal.GetCarByUserProductMappingIdAsync(userProductMappingId); + var cars = await adminDal.GetCarsByUserProductMappingIdAsync(userProductMappingId); return cars; } @@ -295,12 +295,24 @@ namespace TIAMWebApp.Server.Controllers [SignalR(SignalRTags.GetAllCars)] public async Task> GetAllCars() { - _logger.Info($@"GetAllCars called "); + _logger.Info($@"GetAllCars called"); var cars = await adminDal.GetAllCarsAsync(); return cars; } + //[AllowAnonymous] + //[HttpGet] + //[Route(APIUrls.GetAllCarsRouteName)] + [SignalR(SignalRTags.GetAllCarsByProductId)] + public async Task> GetAllCarsByProductId(Guid productId) + { + _logger.Info($@"GetAllCarsByProductId called"); + + var cars = await adminDal.GetAllCarsbyProductIdAsync(productId); + return cars; + } + [NonAction] [ApiExplorerSettings(IgnoreApi = true)] private async Task CarDataChanging(Car car, TrackingState trackingState) diff --git a/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs b/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs index 9604543a..3bf2cafc 100644 --- a/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs +++ b/TIAMWebApp/Server/Controllers/TransferDataAPIController.cs @@ -582,6 +582,26 @@ namespace TIAMWebApp.Server.Controllers return transferToModify; } + //[Authorize] + //[HttpGet] + //[Route(APIUrls.GetTransferDriverRouteName)] + [SignalR(SignalRTags.GetAllDrivers)] + public async Task> GetAllDrivers() + { + var result = await _adminDal.GetTAllDrivers(); + return result; + } + + //[Authorize] + //[HttpGet] + //[Route(APIUrls.GetTransferDriverRouteName)] + [SignalR(SignalRTags.GetAllDriversByProductId)] + public async Task> GetAllDriversByProductId(Guid productId) + { + var result = await _adminDal.GetTAllDriversByProductId(productId); + return result; + } + [Authorize] [HttpGet] [Route(APIUrls.GetTransferDriverRouteName)] @@ -619,7 +639,7 @@ namespace TIAMWebApp.Server.Controllers public async Task UpdateTransferDriver([FromBody] TransferToDriver transferToDriver) { var result = await _adminDal.UpdateTransferToDriverAsync(transferToDriver); - return result ? transferToDriver : null; + return result; // ? transferToDriver : null; } //[Authorize]