public async Task <EndpointLinkEntity> CreateLinkAsync(EndpointEntity sourceEndpoint,
                                                               EndpointEntity associatedEndpoint,
                                                               CancellationToken ct)
        {
            /* The assumption here is that we have already check that a link from sourceEndpoint to
             * associatedEndpoint doesn't exist so we can just go ahead and create a link. */
            var link = new EndpointLinkEntity
            {
                SourceEndpoint     = sourceEndpoint,
                AssociatedEndpoint = associatedEndpoint,
                Confirmed          = false
            };

            _context.Links.Add(link);

            var created = await _context.SaveChangesAsync(ct);

            if (created < 1)
            {
                return(null);
            }

            link = await _context.Links
                   .Where(r => (r.SourceEndpoint.EndpointId == sourceEndpoint.EndpointId && r.AssociatedEndpoint.EndpointId == associatedEndpoint.EndpointId))
                   .Include(r => r.SourceEndpoint).ThenInclude(r => r.Owner)
                   .Include(r => r.AssociatedEndpoint).ThenInclude(r => r.HubConnection)
                   .FirstOrDefaultAsync(ct);

            return(link);
        }
Пример #2
0
        public async Task <bool> CreateNotificationAsync(NotificationEntity notification, CancellationToken ct)
        {
            _context.Notifications.Add(notification);

            var created = await _context.SaveChangesAsync(ct);

            return(created > 0 ? true : false);
        }
        public async Task <bool> ConnectHubConnectionReferenceAsync(Guid endpointId, Guid ownerId, string connectionId, CancellationToken ct)
        {
            var endpoint = await _context.Endpoints
                           .Where(r => r.EndpointId == endpointId && r.Owner.IdentityId == ownerId)
                           .Include(r => r.HubConnection)
                           .FirstOrDefaultAsync(ct);

            if (endpoint == null)
            {
                return(false);
            }

            endpoint.HubConnection.ConnectionId = connectionId;
            endpoint.HubConnection.Connected    = true;

            var updated = await _context.SaveChangesAsync(ct);

            if (updated < 1)
            {
                return(false);
            }

            return(true);
        }
Пример #4
0
        public async Task <EndpointEntity> CreateEndpointAsync(string name,
                                                               EndpointClientEntity client,
                                                               EndpointOwnerEntity owner,
                                                               HubConnectionEntity hubConnection,
                                                               CancellationToken ct)
        {
            var endpoint = await _context.Endpoints.FirstOrDefaultAsync(
                r => (r.Owner.IdentityId == owner.IdentityId && r.Name == name),
                ct);

            /* Endpoint already exists so return null to indicate we failed to create new endpoint. */
            if (endpoint != null)
            {
                return(null);
            }

            var existingClient = await _context.Clients.FirstOrDefaultAsync(
                r => (r.ClientId == client.ClientId && r.ClientType == client.ClientType),
                ct);

            /* Ensure client exist */
            if (existingClient == null)
            {
                _context.Clients.Add(client);
            }

            var existingOwner = await _context.Owners.FirstOrDefaultAsync(
                r => (r.IdentityId == owner.IdentityId && r.OwnerName == owner.OwnerName),
                ct);

            /* Ensure owner exist */
            if (existingOwner == null)
            {
                _context.Owners.Add(owner);
            }

            if (existingClient == null || existingOwner == null)
            {
                var createdNavigationPropertiesResult = await _context.SaveChangesAsync(ct);

                if (createdNavigationPropertiesResult < 1)
                {
                    return(null);
                }

                if (existingClient == null)
                {
                    existingClient = await _context.Clients.FirstOrDefaultAsync(
                        r => (r.ClientId == client.ClientId && r.ClientType == client.ClientType),
                        ct);
                }

                if (existingOwner == null)
                {
                    existingOwner = await _context.Owners.FirstOrDefaultAsync(
                        r => (r.IdentityId == owner.IdentityId && r.OwnerName == owner.OwnerName),
                        ct);
                }
            }

            endpoint = new EndpointEntity
            {
                Name          = name,
                Description   = "No description.",
                Client        = existingClient,
                Owner         = existingOwner,
                HubConnection = hubConnection
            };

            _context.Endpoints.Add(endpoint);

            var created = await _context.SaveChangesAsync(ct);

            if (created < 1)
            {
                return(null);
            }

            /* Everything should be up to date now, so let's try again and return the endpoint. */
            endpoint = await _context.Endpoints
                       .Where(r => r.Name == name && r.Owner.IdentityId == existingOwner.IdentityId)
                       .Include(r => r.Owner)
                       .Include(r => r.Client)
                       .FirstOrDefaultAsync();

            return(endpoint);
        }