Beispiel #1
0
        public async Task <Battery> EnterNewBatteryAsync(int accountId, Battery battery)
        {
            Guard.AgainstNull(battery, "battery");
            Guard.AgainstAccountNumberMismatch(accountId, battery.AccountId, "accountId", "battery.AccountId");
            //Guard.AgainstNull(batteryType, "batteryType");

            // If the type does not exist, add it
            if (battery.BatteryType != null)
            {
                var repoBatteryType = await _batteryTypeRepository.GetByIdAsync(battery.BatteryType.Id);

                battery.BatteryType = repoBatteryType;
            }
            try
            {
                //battery.BatteryType = await _batteryTypeRepository.GetByIdAsync(battery.BatteryTypeId);    // Ensures the battery type is attached
                battery = await _batteryRepository.AddAsync(battery);

                _logger.LogInformation($"Added battery, new Id = {battery.Id}");
                return(battery);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error adding battery: {battery}.");
                return(null);
            }
        }
Beispiel #2
0
        public async Task <BatteryResponse> SaveAsync(Battery battery)
        {
            try
            {
                await _batteryRepository.AddAsync(battery);


                return(new BatteryResponse(battery));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new BatteryResponse($"An error occurred when saving the battery: {ex.Message}"));
            }
        }