Пример #1
0
        public async Task <UpdateChargeStationDto> Handle(AddConnectorCommand command, CancellationToken cancellationToken)
        {
            var found = await _stationRepository.ExistsAsync(command.ChargeStationId).ConfigureAwait(false);

            if (!found)
            {
                throw new ChargeStationNotFoundException(command.ChargeStationId);
            }

            var chargeStation = await _stationRepository.GetAsyncExtended(command.ChargeStationId).ConfigureAwait(false);

            var result = chargeStation.AddConnector(command.ConnectorMaxCurrentAmps, command.ConnectorId);

            if (result.IsError)
            {
                return(new UpdateChargeStationDto
                {
                    IsError = true,
                    ErrorMessage = "ChargeGroup capacity exceeded. You can unplug these connectors:",
                    ConnectorsToUnplug = result.Suggestions.ToResultStrings()
                });
            }

            await _stationRepository.UpdateConnectorsAsync(chargeStation).ConfigureAwait(false);

            return(_mapper.Map <UpdateChargeStationDto>(chargeStation));
        }
        public async Task <UpdateChargeStationDto> Handle(ChangeGroupCommand command, CancellationToken cancellationToken)
        {
            var chargeStation = await _stationRepository.GetAsyncExtended(command.ChargeStationId).ConfigureAwait(false);

            if (chargeStation == null)
            {
                throw new ChargeStationNotFoundException(command.ChargeStationId);
            }

            var newGroup = await _groupRepository.GetAsyncExtended(command.ChargeGroupId).ConfigureAwait(false);

            if (newGroup == null)
            {
                throw new ChargeGroupNotFoundException(command.ChargeGroupId);
            }

            var oldGroupId = chargeStation.ParentChargeGroup.Id;

            var result = newGroup.AddChargeStation(chargeStation);

            if (result.IsError)
            {
                return(new UpdateChargeStationDto
                {
                    IsError = true,
                    ErrorMessage = "ChargeGroup capacity exceeded. You can unplug these connectors:",
                    ConnectorsToUnplug = result.Suggestions.ToResultStrings()
                });
            }

            var oldGroup = await _groupRepository.GetAsyncExtended(oldGroupId).ConfigureAwait(false);

            if (oldGroup == null)
            {
                throw new ChargeGroupNotFoundException(oldGroupId);
            }
            oldGroup.RemoveChargeStation(chargeStation.Id);

            await _stationRepository.UpdateChargeGroupAsync(chargeStation.Id, chargeStation.ParentChargeGroup.Id).ConfigureAwait(false);

            await _groupRepository.UpdateAsync(newGroup).ConfigureAwait(false);

            await _groupRepository.UpdateAsync(oldGroup).ConfigureAwait(false);

            return(_mapper.Map <UpdateChargeStationDto>(chargeStation));
        }