Esempio n. 1
0
        public IPayload SaveGateway(SaveGatewayDto saveGatewayDto)
        {
            Gateway gateway = _mapper.Map <Gateway>(saveGatewayDto);

            var isCreated = _context.Gateways.Select(
                gat =>
                new
            {
                ValueForAvoidDuplicate = gat.Name + gat.Ipv4
            }
                )
                            .Any(keys => keys.ValueForAvoidDuplicate.Equals(saveGatewayDto.ValueToAvoidDuplicate()));

            if (!isCreated)
            {
                var result = _context.Gateways.Add(gateway);
                _context.SaveChanges();

                var factory = new SingleObjectPayloadFactory <GatewayDto>(_mapper.Map <GatewayDto>(result.Entity));
                return(factory.GetPayload());
            }
            else
            {
                throw new AlreadyExistException("The same Gateway was created before");
            }
        }
Esempio n. 2
0
        public async Task <IPayload> FindById(int id)
        {
            var device = await _context.Devices.FindAsync(id);

            if (device == null)
            {
                throw new DoesNotExistException($"Device with id: { id }, hasn't been found in db.");
            }
            else
            {
                var factory = new SingleObjectPayloadFactory <DeviceDto>(_mapper.Map <DeviceDto>(device));
                return(factory.GetPayload());
            }
        }
Esempio n. 3
0
        public async Task <IPayload> SaveDevice(SaveDeviceDto saveDeviceDto)
        {
            var isCreated = await _context.Devices.Select(
                p =>
                new
            {
                ValueForAvoidDuplicate = p.Vendor + p.GatewayId.ToString()
            }
                )
                            .AnyAsync(keys => keys.ValueForAvoidDuplicate.Equals(saveDeviceDto.ValueToAvoidDuplicate())).ConfigureAwait(true);

            if (isCreated)
            {
                throw new AlreadyExistException($"The same Device was created before");
            }

            Device  device = _mapper.Map <Device>(saveDeviceDto);
            Gateway gateway;

            try
            {
                gateway = await _context.Gateways
                          .Include(p => p.Devices)
                          .SingleAsync(p => p.Id == device.GatewayId).ConfigureAwait(false);
            }
            catch (InvalidOperationException ex)
            {
                throw new DoesNotExistException($"Gateways with id: { device.GatewayId }, hasn't been found in db.");
            }


            if (gateway.Devices.Count >= 10)
            {
                throw new GatewayDeviceLimitExceeded($"Gateways {gateway.Name} cannot support more than 10 devices.");
            }

            var peripheralResult = _context.Devices.Add(device);
            await _context.SaveChangesAsync();

            var factory = new SingleObjectPayloadFactory <DeviceDto>(_mapper.Map <DeviceDto>(peripheralResult.Entity));

            return(factory.GetPayload());
        }
Esempio n. 4
0
        public async Task <IPayload> FindById(Guid id)
        {
            Gateway data;

            try
            {
                data = await _context.Gateways
                       .Include(g => g.Devices)
                       .SingleAsync(g => g.Id.Equals(id));
            }
            catch (InvalidOperationException ex)
            {
                throw new DoesNotExistException($"The Gateways with id:{id.ToString()}, hasn't been found in db.");
            }

            var factory = new SingleObjectPayloadFactory <GatewayDto>(_mapper.Map <GatewayDto>(data));

            return(factory.GetPayload());
        }