Skip to content

Commit

Permalink
changed GetResource to use address as unique identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-aschlackman committed Apr 23, 2024
1 parent 2b6701d commit 8ad8dfb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
17 changes: 5 additions & 12 deletions src/Caster.Api/Features/Resources/Queries/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,10 @@ public class Query : IRequest<Resource>
public Guid WorkspaceId { get; set; }

/// <summary>
/// Id of the Resource.
/// Address of the Resource
/// </summary>
[JsonIgnore]
public string Id { get; set; }

/// <summary>
/// Type of the Resource.
/// </summary>
[DataMember]
[Required]
public string Type { get; set; }
public string Address { get; set; }
}

public class Handler : IRequestHandler<Query, Resource>
Expand All @@ -70,17 +63,17 @@ public async Task<Resource> Handle(Query request, CancellationToken cancellation
if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
throw new ForbiddenException();

var workspace = await _db.Workspaces.FindAsync(request.WorkspaceId);
var workspace = await _db.Workspaces.FindAsync(request.WorkspaceId, cancellationToken);

if (workspace == null)
throw new EntityNotFoundException<Workspace>();

var state = workspace.GetState();
var resources = state.GetResources();

var id = HttpUtility.UrlDecode(request.Id);
var address = HttpUtility.UrlDecode(request.Address);

var resource = resources.Where(r => r.Type == request.Type && r.Id == id).FirstOrDefault();
var resource = resources.Where(r => r.Address == address).FirstOrDefault();
return _mapper.Map<Resource>(resource, opts => opts.ExcludeMembers());
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Caster.Api/Features/Resources/ResourcesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public ResourcesController(IMediator mediator)
/// <summary>
/// Get a single resource in a Workspace.
/// </summary>
[HttpGet("workspaces/{workspaceId}/resources/{id}")]
[HttpGet("workspaces/{workspaceId}/resources/{address}")]
[ProducesResponseType(typeof(Resource), (int)HttpStatusCode.OK)]
[SwaggerOperation(OperationId = "GetResource")]
public async Task<IActionResult> Get([FromRoute] Guid workspaceId, [FromRoute] string id, [FromQuery] Get.Query query)
public async Task<IActionResult> Get([FromRoute] Guid workspaceId, [FromRoute] string address, [FromQuery] Get.Query query)
{
query.WorkspaceId = workspaceId;
query.Id = id;
query.Address = address;
var result = await _mediator.Send(query);
return Ok(result);
}
Expand Down

0 comments on commit 8ad8dfb

Please sign in to comment.