Exemplo n.º 1
0
        public async Task <GatewayResponse> UpdateAsync(string Id, Gateway gateway)
        {
            var existing = await _gatewayRepo.FindByIdAsync(Id);

            if (existing == null)
            {
                return(new GatewayResponse("Gateway not found."));
            }

            existing.Name = gateway.Name;
            existing.IP   = gateway.IP;

            try {
                _gatewayRepo.Update(existing);
                await _gatewayRepo.SaveAllAsync();

                return(new GatewayResponse(existing));
            } catch (Exception ex) {
                return(new GatewayResponse($"An error occurred : {ex.Message}"));
            }
        }
Exemplo n.º 2
0
        public async Task <DeviceResponse> CreateAsync(Device device)
        {
            try {
                var gateway = _gatewayRepo.FindByIdAsync(device.GatewayId.ToString()).Result;
                if (gateway != null && gateway.DeviceNumb >= 10)
                {
                    return(new DeviceResponse("No more than 10 devices allowed per gateway."));
                }
                else
                {
                    gateway.DeviceNumb++;
                    _gatewayRepo.Update(gateway);
                    await _deviceRepo.CreateAync(device);

                    await _deviceRepo.SaveAllAsync();
                }
                return(new DeviceResponse(device));
            } catch (Exception ex) {
                return(new DeviceResponse($"An error occurred : {ex.Message}")); //handle concurency here
            }
        }