Пример #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));
        }
Пример #2
0
        public async Task <bool> Handle(DeleteChargeStationCommand command, CancellationToken cancellationToken)
        {
            var stationFound = await _stationRepository.ExistsAsync(command.Id).ConfigureAwait(false);

            if (!stationFound)
            {
                throw new ChargeStationNotFoundException(command.Id);
            }

            var station = await _stationRepository.GetAsync(command.Id);

            if (station.ParentChargeGroup == null)
            {
                throw new ChargeGroupNotFoundException(command.Id);
            }

            var group = await _groupRepository.GetAsyncExtended(station.ParentChargeGroup.Id);

            group.RemoveChargeStation(station.Id);

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

            await _stationRepository.DeleteAsync(command.Id).ConfigureAwait(false);

            return(true);
        }
Пример #3
0
        public async Task <UpdateChargeStationDto> Handle(UpdateChargeStationNameCommand command, CancellationToken cancellationToken)
        {
            var found = await _chargeStationRepository.ExistsAsync(command.Id).ConfigureAwait(false);

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

            await _chargeStationRepository.UpdateNameAsync(command.Id, command.Name).ConfigureAwait(false);

            var resource = await _chargeStationRepository.GetAsync(command.Id);

            return(_mapper.Map <UpdateChargeStationDto>(resource));
        }
Пример #4
0
        public async Task <AddChargeStationDto> Handle(AddChargeStationCommand command, CancellationToken cancellationToken)
        {
            if (await _stationRepository.ExistsAsync(command.Id).ConfigureAwait(false))
            {
                throw new ChargeStationAlreadyExistException(command.Id);
            }

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

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

            var chargeStation = ChargeStation.Create(command.Id, command.Name, chargeGroup);

            chargeGroup.AddChargeStation(chargeStation);

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

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

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

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

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