Ejemplo n.º 1
0
        public void Delete(string id)
        {
            var deviceKey = PartionKeyRowKeyPair.CreateFromIdentity(id);

            var deviceRepository  = new DeviceRepository(_tableEntityOperation);
            var networkRepository = new NetworkRepository(_tableEntityOperation);

            var deviceTableEntity = deviceRepository.Get(deviceKey);

            if (deviceTableEntity == null)
            {
                throw new NotFoundException();
            }

            var parentNetworkKey = PartionKeyRowKeyPair.CreateFromIdentity(deviceTableEntity.NetworkId);

            deviceRepository.Delete(deviceTableEntity);

            TransientErrorHandling.Run(() =>
            {
                var parentNetwork = networkRepository.Get(parentNetworkKey);
                for (var idx = 0; idx < parentNetwork.Devices.Count; idx++)
                {
                    if (parentNetwork.Devices[idx].Id == id)
                    {
                        parentNetwork.Devices.RemoveAt(idx);
                        break;
                    }
                }
                networkRepository.Update(parentNetwork);
            });
        }
Ejemplo n.º 2
0
        public string Create(Device device)
        {
            var deviceIdentity = Identity.Next();

            var deviceKey = PartionKeyRowKeyPair.CreateFromIdentity(deviceIdentity);

            var deviceRepository  = new DeviceRepository(_tableEntityOperation);
            var networkRepository = new NetworkRepository(_tableEntityOperation);

            var deviceTableEntity = new DeviceTableEntity(deviceKey, device.Name, device.Network.Id, device.Service.Id,
                                                          device.Company.Id, device.DeviceKey, device.NumericId);

            deviceRepository.Create(deviceTableEntity);

            TransientErrorHandling.Run(() =>
            {
                var parentNetworkKey = PartionKeyRowKeyPair.CreateFromIdentity(device.Network.Id);

                var parentNetwork = networkRepository.Get(parentNetworkKey);
                parentNetwork.Devices.Add(new Small()
                {
                    Id = deviceIdentity, Name = device.Name
                });
                networkRepository.Update(parentNetwork);
            });

            return(deviceIdentity);
        }
Ejemplo n.º 3
0
        public void Delete(string id)
        {
            var serviceKey = PartionKeyRowKeyPair.CreateFromIdentity(id);

            var serviceRepository = new ServiceRepository(_tableEntityOperation);

            var serviceTableEntity = serviceRepository.Get(serviceKey);

            if (serviceTableEntity == null)
            {
                throw new NotFoundException();
            }

            var companyKey = PartionKeyRowKeyPair.CreateFromIdentity(serviceTableEntity.CompanyId);

            serviceRepository.Delete(serviceTableEntity);

            TransientErrorHandling.Run(() =>
            {
                var companyRepository = new CompanyRepository(_tableEntityOperation);

                var company = companyRepository.Get(companyKey);
                for (var idx = 0; idx < company.Services.Count; idx++)
                {
                    if (company.Services[idx].Id == id)
                    {
                        company.Services.RemoveAt(idx);
                        break;
                    }
                }
                companyRepository.Update(company);
            });
        }
Ejemplo n.º 4
0
        public string Create(Service service)
        {
            var serviceIdentity = Identity.Next();

            var serviceKey = PartionKeyRowKeyPair.CreateFromIdentity(serviceIdentity);
            var companyKey = PartionKeyRowKeyPair.CreateFromIdentity(service.Company.Id);

            var serviceRepository  = new ServiceRepository(_tableEntityOperation);
            var serviceTableEntity = new ServiceTableEntity(serviceKey, service.Name, service.Company.Id, service.ApiKey);

            serviceRepository.Create(serviceTableEntity);

            TransientErrorHandling.Run(() =>
            {
                var companyRepository = new CompanyRepository(_tableEntityOperation);
                var company           = companyRepository.Get(companyKey);

                company.Services.Add(new Small()
                {
                    Id = serviceIdentity, Name = service.Name
                });
                companyRepository.Update(company);
            });

            return(serviceIdentity);
        }
Ejemplo n.º 5
0
        public string Create(Company company, string userId)
        {
            var companyIdentity = Identity.Next();

            var companyKey = PartionKeyRowKeyPair.CreateFromIdentity(companyIdentity);
            var userKey    = PartionKeyRowKeyPair.CreateFromIdentity(userId);

            var userRepository  = new UserRepository(_tableEntityOperation);
            var userTableEntity = userRepository.Get(userKey);

            var companyRepository  = new CompanyRepository(_tableEntityOperation);
            var companyTableEntity = new CompanyTableEntity(companyKey, company.Name,
                                                            new List <SmallUser>
            {
                new SmallUser {
                    Id = userId, Name = userTableEntity.Name, Email = userTableEntity.Email
                }
            });

            companyRepository.Create(companyTableEntity);

            TransientErrorHandling.Run(() =>
            {
                userTableEntity = userRepository.Get(userKey);

                userTableEntity.Companies.Add(new Small {
                    Id = companyIdentity, Name = company.Name
                });
                userRepository.Update(userTableEntity);
            });

            return(companyIdentity);
        }
Ejemplo n.º 6
0
        public void Delete(string id)
        {
            var companyKey = PartionKeyRowKeyPair.CreateFromIdentity(id);

            var companyRepository  = new CompanyRepository(_tableEntityOperation);
            var companyTableEntity = companyRepository.Get(companyKey);

            if (companyTableEntity == null)
            {
                throw new NotFoundException();
            }

            var userIds = companyTableEntity.Users.Select(u => u.Id).ToList();

            companyRepository.Delete(companyTableEntity);

            TransientErrorHandling.Run(() =>
            {
                var userRepository = new UserRepository(_tableEntityOperation);

                foreach (var userId in userIds)
                {
                    var userKey = PartionKeyRowKeyPair.CreateFromIdentity(userId);

                    var user = userRepository.Get(userKey);
                    user.Companies.Remove(user.Companies.Single(c => c.Id == id));
                    userRepository.Update(user);
                }
            });
        }
Ejemplo n.º 7
0
        public void Update(Network network)
        {
            var networkKey = PartionKeyRowKeyPair.CreateFromIdentity(network.Id);

            var networkRepository = new NetworkRepository(_tableEntityOperation);

            var networkTableEntity = networkRepository.Get(networkKey);

            if (networkTableEntity == null)
            {
                throw new NotFoundException();
            }

            networkTableEntity.Name = network.Name;
            networkTableEntity.TelemetryDataSinkSettings = network.TelemetryDataSinkSettings;

            networkRepository.Update(networkTableEntity);

            if (networkTableEntity.ParentNetworkId == null)
            {
                var serviceKey = PartionKeyRowKeyPair.CreateFromIdentity(networkTableEntity.ServiceId);

                TransientErrorHandling.Run(() =>
                {
                    var serviceRepository = new ServiceRepository(_tableEntityOperation);

                    var service = serviceRepository.Get(serviceKey);
                    for (var idx = 0; idx < service.Networks.Count; idx++)
                    {
                        if (service.Networks[idx].Id == network.Id)
                        {
                            service.Networks[idx].Name = network.Name;
                            break;
                        }
                    }
                    serviceRepository.Update(service);
                });
            }
            else
            {
                var parentNetworkKey = PartionKeyRowKeyPair.CreateFromIdentity(networkTableEntity.ParentNetworkId);

                TransientErrorHandling.Run(() =>
                {
                    var parentNetwork = networkRepository.Get(parentNetworkKey);
                    for (var idx = 0; idx < parentNetwork.Networks.Count; idx++)
                    {
                        if (parentNetwork.Networks[idx].Id == network.Id)
                        {
                            parentNetwork.Networks[idx].Name = network.Name;
                            break;
                        }
                    }
                    networkRepository.Update(parentNetwork);
                });
            }
        }
Ejemplo n.º 8
0
        public void Delete(string id)
        {
            var networkKey = PartionKeyRowKeyPair.CreateFromIdentity(id);

            var networkRepository = new NetworkRepository(_tableEntityOperation);

            var networkTableEntity = networkRepository.Get(networkKey);

            if (networkTableEntity == null)
            {
                throw new NotFoundException();
            }

            var serviceKey       = PartionKeyRowKeyPair.CreateFromIdentity(networkTableEntity.ServiceId);
            var parentNetworkKey = networkTableEntity.ParentNetworkId != null
                ? PartionKeyRowKeyPair.CreateFromIdentity(networkTableEntity.ParentNetworkId)
                : null;

            networkRepository.Delete(networkTableEntity);

            if (parentNetworkKey == null)
            {
                TransientErrorHandling.Run(() =>
                {
                    var serviceRepository = new ServiceRepository(_tableEntityOperation);

                    var service = serviceRepository.Get(serviceKey);
                    for (var idx = 0; idx < service.Networks.Count; idx++)
                    {
                        if (service.Networks[idx].Id == id)
                        {
                            service.Networks.RemoveAt(idx);
                            break;
                        }
                    }
                    serviceRepository.Update(service);
                });
            }
            else
            {
                TransientErrorHandling.Run(() =>
                {
                    var parentNetwork = networkRepository.Get(parentNetworkKey);
                    for (var idx = 0; idx < parentNetwork.Networks.Count; idx++)
                    {
                        if (parentNetwork.Networks[idx].Id == id)
                        {
                            parentNetwork.Networks.RemoveAt(idx);
                            break;
                        }
                    }
                    networkRepository.Update(parentNetwork);
                });
            }
        }
Ejemplo n.º 9
0
        public string Create(Network network)
        {
            var networkIdentity = Identity.Next();

            var networkKey = PartionKeyRowKeyPair.CreateFromIdentity(networkIdentity);

            var networkRepository  = new NetworkRepository(_tableEntityOperation);
            var networkTableEntity = new NetworkTableEntity(
                networkKey,
                network.Name,
                network.ParentNetwork != null ? network.ParentNetwork.Id : null,
                network.Service.Id,
                network.Company.Id, network.NetworkKey);

            networkRepository.Create(networkTableEntity);

            if (network.ParentNetwork == null)
            {
                var serviceKey = PartionKeyRowKeyPair.CreateFromIdentity(network.Service.Id);

                TransientErrorHandling.Run(() =>
                {
                    var serviceRepository = new ServiceRepository(_tableEntityOperation);

                    var service = serviceRepository.Get(serviceKey);
                    service.Networks.Add(new Small()
                    {
                        Id = networkIdentity, Name = network.Name
                    });
                    serviceRepository.Update(service);
                });
            }
            else
            {
                TransientErrorHandling.Run(() =>
                {
                    var parentNetworkKey = PartionKeyRowKeyPair.CreateFromIdentity(network.ParentNetwork.Id);

                    var parentNetwork = networkRepository.Get(parentNetworkKey);
                    parentNetwork.Networks.Add(new Small()
                    {
                        Id = networkIdentity, Name = network.Name
                    });
                    networkRepository.Update(parentNetwork);
                });
            }

            return(networkIdentity);
        }
Ejemplo n.º 10
0
        public string Create(User user, string passwordHash, string salt)
        {
            var userIdentity = Identity.Next();

            var userRepository      = new UserRepository(_tableEntityOperation);
            var loginUserRepository = new LoginUserRepository(_tableEntityOperation);

            var partitionKeyRowKeyPair = PartionKeyRowKeyPair.CreateFromIdentity(userIdentity);

            loginUserRepository.Create(new LoginUserTableEntity(PartitionKey(user.Email), user.Email, passwordHash, salt, userIdentity));

            TransientErrorHandling.Run(() => userRepository.Create(new UserTableEntity(partitionKeyRowKeyPair, user.Name, user.Email,
                                                                                       new List <Small>(), user.Activated, user.ActivationCode)));

            return(userIdentity);
        }
Ejemplo n.º 11
0
        public void AddUser(string companyId, string userId)
        {
            var companyKey = PartionKeyRowKeyPair.CreateFromIdentity(companyId);
            var userKey    = PartionKeyRowKeyPair.CreateFromIdentity(userId);

            var companyRepository = new CompanyRepository(_tableEntityOperation);
            var userRepository    = new UserRepository(_tableEntityOperation);

            var companyTableEntity = companyRepository.Get(companyKey);
            var userTableEntity    = userRepository.Get(userKey);

            if (companyTableEntity == null || userTableEntity == null)
            {
                throw new NotFoundException();
            }

            TransientErrorHandling.Run(() =>
            {
                companyTableEntity = companyRepository.Get(companyKey);

                if (companyTableEntity.Users.All(u => u.Id != userId))
                {
                    companyTableEntity.Users.Add(new SmallUser
                    {
                        Id    = userId,
                        Name  = userTableEntity.Name,
                        Email = userTableEntity.Email
                    });
                    companyRepository.Update(companyTableEntity);
                }
            });

            TransientErrorHandling.Run(() =>
            {
                userTableEntity = userRepository.Get(userKey);

                if (userTableEntity.Companies.All(c => c.Id != companyId))
                {
                    userTableEntity.Companies.Add(new Small {
                        Id = companyId, Name = companyTableEntity.Name
                    });
                    userRepository.Update(userTableEntity);
                }
            });
        }
Ejemplo n.º 12
0
        public void Update(Company company)
        {
            var companyKey = PartionKeyRowKeyPair.CreateFromIdentity(company.Id);

            var companyRepository = new CompanyRepository(_tableEntityOperation);

            CompanyTableEntity companyTableEntity = null;

            TransientErrorHandling.Run(() =>
            {
                companyTableEntity = companyRepository.Get(companyKey);
                if (companyTableEntity == null)
                {
                    throw new NotFoundException();
                }

                companyTableEntity.Name = company.Name;
                companyTableEntity.TelemetryDataSinkSettings = company.TelemetryDataSinkSettings;

                companyRepository.Update(companyTableEntity);
            });

            var userIds = companyTableEntity.Users.Select(u => u.Id).ToList();

            TransientErrorHandling.Run(() =>
            {
                var userRepository = new UserRepository(_tableEntityOperation);

                foreach (var userId in userIds)
                {
                    var userKey = PartionKeyRowKeyPair.CreateFromIdentity(userId);

                    var user = userRepository.Get(userKey);
                    user.Companies.Single(c => c.Id == company.Id).Name = company.Name;
                    userRepository.Update(user);
                }
            });
        }
Ejemplo n.º 13
0
        public void Update(Service service)
        {
            var serviceKey = PartionKeyRowKeyPair.CreateFromIdentity(service.Id);

            var serviceRepository = new ServiceRepository(_tableEntityOperation);

            var serviceTableEntity = serviceRepository.Get(serviceKey);

            if (serviceTableEntity == null)
            {
                throw new NotFoundException();
            }

            serviceTableEntity.Name   = service.Name;
            serviceTableEntity.ApiKey = service.ApiKey;
            serviceTableEntity.TelemetryDataSinkSettings = service.TelemetryDataSinkSettings;

            serviceRepository.Update(serviceTableEntity);

            var companyKey = PartionKeyRowKeyPair.CreateFromIdentity(serviceTableEntity.CompanyId);

            TransientErrorHandling.Run(() =>
            {
                var companyRepository = new CompanyRepository(_tableEntityOperation);

                var company = companyRepository.Get(companyKey);
                for (var idx = 0; idx < company.Services.Count; idx++)
                {
                    if (company.Services[idx].Id == service.Id)
                    {
                        company.Services[idx].Name = service.Name;
                        break;
                    }
                }
                companyRepository.Update(company);
            });
        }
Ejemplo n.º 14
0
        public void Update(Device device)
        {
            var deviceKey = PartionKeyRowKeyPair.CreateFromIdentity(device.Id);

            var deviceRepository  = new DeviceRepository(_tableEntityOperation);
            var networkRepository = new NetworkRepository(_tableEntityOperation);

            var deviceTableEntity = deviceRepository.Get(deviceKey);

            if (deviceTableEntity == null)
            {
                throw new NotFoundException();
            }

            deviceTableEntity.Name      = device.Name;
            deviceTableEntity.DeviceKey = device.DeviceKey;
            deviceTableEntity.NumericId = device.NumericId;

            deviceRepository.Update(deviceTableEntity);

            TransientErrorHandling.Run(() =>
            {
                var parentNetworkKey = PartionKeyRowKeyPair.CreateFromIdentity(deviceTableEntity.NetworkId);

                var parentNetwork = networkRepository.Get(parentNetworkKey);
                for (var idx = 0; idx < parentNetwork.Devices.Count; idx++)
                {
                    if (parentNetwork.Devices[idx].Id == device.Id)
                    {
                        parentNetwork.Devices[idx].Name = device.Name;
                        break;
                    }
                }
                networkRepository.Update(parentNetwork);
            });
        }