public async Task <IActionResult> GetEndpointByIdAsync(Guid endpointId, CancellationToken ct) { var endpoint = await _endpointService.GetEndpointByIdAsync(endpointId, ct); if (endpoint == null || endpoint.Owner.IdentityId != _userInfoService.UserId) { return(NotFound()); } var endpointViewModel = Mapper.Map <EndpointViewModel>(endpoint); if (!Request.GetEtagHandler().NoneMatch(endpointViewModel)) { return(StatusCode(304, endpointViewModel)); } return(Ok(endpointViewModel)); }
public async Task <IActionResult> CreateLinkAsync([FromBody] NewEndpointLinkForm newLink, CancellationToken ct) { if (!ModelState.IsValid) { return(BadRequest(new ApiError(ModelState))); } var sourceId = Guid.Parse(newLink.Source); var destinationId = Guid.Parse(newLink.Destination); if (sourceId == destinationId) { return(BadRequest(new ApiError("A device cannot create a link to itself."))); } var endpointLink = await _linkService.GetLinkByEndpointsIdAsync(sourceId, destinationId, ct); if (endpointLink != null) { return(BadRequest(new ApiError("Link to specified device already exists."))); } var sourceEndpoint = await _endpointService.GetEndpointByIdAsync(sourceId, ct); if (sourceEndpoint == null) { return(BadRequest(new ApiError("Requesting device is invalid."))); } if (sourceEndpoint.Owner.IdentityId != _userInfoService.UserId) { return(BadRequest(new ApiError("Cannot create a link from a device not created by current user."))); } var destinationEndpoint = await _endpointService.GetEndpointByIdAsync(destinationId, ct); if (destinationEndpoint == null) { return(BadRequest(new ApiError("Target device is invalid."))); } endpointLink = await _linkService.CreateLinkAsync(sourceEndpoint, destinationEndpoint, ct); if (endpointLink == null) { return(StatusCode(500, new ApiError("Failed to create a link."))); } if (endpointLink.AssociatedEndpoint.HubConnection.Connected) { await _hubContext.Clients.Client(endpointLink.AssociatedEndpoint.HubConnection.ConnectionId) .LinkRequestReceived(endpointLink.LinkId.ToString(), endpointLink.SourceEndpoint.Name, endpointLink.SourceEndpoint.Owner.OwnerName); } var newLinkUrl = Url.Link(nameof(EndpointLinksController.CreateLinkAsync), null); newLinkUrl = newLinkUrl + "/" + endpointLink.LinkId; return(Created(newLinkUrl, null)); }