public async Task <GetFillLotForEditOutput> GetFillLotForEdit(NullableIdInput <long> input) { long? id; FillLotEditDto fillLotEditDto; if (!input.Id.HasValue) { fillLotEditDto = new FillLotEditDto() { Tanks = new List <FillLotTank>() }; } else { IRepository <FillLot, long> repository = this._fillLotRepository; id = input.Id; FillLot async = await repository.GetAsync(id.Value); fillLotEditDto = async.MapTo <FillLotEditDto>(); FillLotEditDto fillLotEditDto1 = fillLotEditDto; IRepository <FillLotTank, long> repository1 = this._fillLotTankRepository; List <FillLotTank> allListAsync = await repository1.GetAllListAsync((FillLotTank x) => x.FillLotId == async.Id); fillLotEditDto1.Tanks = allListAsync; fillLotEditDto1 = null; } if (!fillLotEditDto.Id.HasValue || !fillLotEditDto.AddressId.HasValue) { fillLotEditDto.Address = new AddressDto(); } else { IRepository <Address, long> repository2 = this._addressRepository; id = fillLotEditDto.AddressId; Address address = await repository2.GetAsync(id.Value); fillLotEditDto.Address = address.MapTo <AddressDto>(); } return(new GetFillLotForEditOutput() { FillLot = fillLotEditDto }); }
public async Task <long> CreateOrUpdateFillLot(CreateOrUpdateFillLotInput input) { if (!input.FillLot.AddressId.HasValue || (input.FillLot.AddressId.HasValue && input.FillLot.AddressId.Value == 0L)) { input.FillLot.AddressId = null; } bool isNew; long fillLotId = 0; if (input.FillLot.Id.HasValue) { if (!PermissionChecker.IsGranted("Pages.Tenant.FillLots.Edit")) { throw new UserFriendlyException(L("Permissions_UserNotAuthorizedMessage")); } isNew = false; FillLot fillLot = new FillLot(); Mapper.Map <FillLotEditDto, FillLot>(input.FillLot, fillLot); fillLot.Tanks = null; await _fillLotRepository.UpdateAsync(fillLot); fillLotId = fillLot.Id; } else { if (!PermissionChecker.IsGranted("Pages.Tenant.FillLots.Create")) { throw new UserFriendlyException(L("Permissions_UserNotAuthorizedMessage")); } isNew = true; FillLot newFillLot = new FillLot(); Mapper.Map <FillLotEditDto, FillLot>(input.FillLot, newFillLot); fillLotId = await this._fillLotRepository.InsertAndGetIdAsync(newFillLot); } var fillLotTanks = await _fillLotTankRepository.GetAllListAsync(m => m.FillLotId == fillLotId); if (input.FillLot.Tanks.Any()) { if (!isNew) { if (fillLotTanks.Any()) { var existingTankIdList = fillLotTanks.Select(t => t.Id).ToList(); foreach (var inputTank in input.FillLot.Tanks) { if (inputTank.Id > 0) { var existing = await _fillLotTankRepository.GetAsync(inputTank.Id); existing.Name = inputTank.Name; existing.Number = inputTank.Number; existing.Capacity = inputTank.Capacity; existing.RemainingCapacity = inputTank.RemainingCapacity; existing.Description = inputTank.Description; existing.LastInspectionComments = inputTank.LastInspectionComments; existing.LastInspectionDate = inputTank.LastInspectionDate; existing.IsActive = inputTank.IsActive; await this._fillLotTankRepository.UpdateAsync(existing); } else { inputTank.FillLotId = fillLotId; await _fillLotTankRepository.InsertAndGetIdAsync(inputTank); } } if (existingTankIdList.Any()) { await _fillLotTankRepository.DeleteAsync(m => m.FillLotId == fillLotId && !existingTankIdList.Contains(m.Id)); } } else { foreach (var inputTank in input.FillLot.Tanks) { inputTank.FillLotId = fillLotId; await _fillLotTankRepository.InsertAsync(inputTank); } } } } else if (!input.FillLot.Tanks.Any() && fillLotTanks.Any()) { var existingTankIdListToDelete = fillLotTanks.Select(x => x.Id); await _fillLotTankRepository.DeleteAsync(x => x.FillLotId == fillLotId && !existingTankIdListToDelete.Contains(x.Id)); } return(fillLotId); }