Skip to content

Commit

Permalink
feat(api): implement get resource by uid
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Jul 11, 2024
1 parent 1ab2c89 commit 457cf92
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 130 deletions.
21 changes: 21 additions & 0 deletions docs/apidocs.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,27 @@ paths:
$ref: '#/definitions/v1Resource'
tags:
- ResourceService
/api/v1/resources:by-uid/{uid}:
get:
summary: GetResourceByUid returns a resource by uid.
operationId: ResourceService_GetResourceByUid
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/v1Resource'
default:
description: An unexpected error response.
schema:
$ref: '#/definitions/googlerpcStatus'
parameters:
- name: uid
description: The uid of the resource.
in: path
required: true
type: string
tags:
- ResourceService
/api/v1/resources:search:
get:
summary: SearchResources searches memos.
Expand Down
10 changes: 10 additions & 0 deletions proto/api/v1/resource_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ service ResourceService {
option (google.api.http) = {get: "/api/v1/{name=resources/*}"};
option (google.api.method_signature) = "name";
}
// GetResourceByUid returns a resource by uid.
rpc GetResourceByUid(GetResourceByUidRequest) returns (Resource) {
option (google.api.http) = {get: "/api/v1/resources:by-uid/{uid}"};
option (google.api.method_signature) = "uid";
}
// GetResourceBinary returns a resource binary by name.
rpc GetResourceBinary(GetResourceBinaryRequest) returns (google.api.HttpBody) {
option (google.api.http) = {get: "/file/{name=resources/*}/{filename}"};
Expand Down Expand Up @@ -104,6 +109,11 @@ message GetResourceRequest {
string name = 1;
}

message GetResourceByUidRequest {
// The uid of the resource.
string uid = 1;
}

message GetResourceBinaryRequest {
// The name of the resource.
// Format: resources/{id}
Expand Down
310 changes: 192 additions & 118 deletions proto/gen/api/v1/resource_service.pb.go

Large diffs are not rendered by default.

103 changes: 103 additions & 0 deletions proto/gen/api/v1/resource_service.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions proto/gen/api/v1/resource_service_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions server/router/api/v1/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (s *APIV1Service) SearchResources(ctx context.Context, request *v1pb.Search
return nil, status.Errorf(codes.InvalidArgument, "failed to parse filter: %v", err)
}
resourceFind := &store.FindResource{}
if filter.UID != nil {
resourceFind.UID = filter.UID
if filter.Filename != nil {
resourceFind.FilenameSearch = filter.Filename
}
user, err := s.GetCurrentUser(ctx)
if err != nil {
Expand Down Expand Up @@ -145,7 +145,19 @@ func (s *APIV1Service) GetResource(ctx context.Context, request *v1pb.GetResourc
if resource == nil {
return nil, status.Errorf(codes.NotFound, "resource not found")
}
return s.convertResourceFromStore(ctx, resource), nil
}

func (s *APIV1Service) GetResourceByUid(ctx context.Context, request *v1pb.GetResourceByUidRequest) (*v1pb.Resource, error) {

Check failure on line 151 in server/router/api/v1/resource_service.go

View workflow job for this annotation

GitHub Actions / go-static-checks

var-naming: method GetResourceByUid should be GetResourceByUID (revive)
resource, err := s.Store.GetResource(ctx, &store.FindResource{
UID: &request.Uid,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get resource: %v", err)
}
if resource == nil {
return nil, status.Errorf(codes.NotFound, "resource not found")
}
return s.convertResourceFromStore(ctx, resource), nil
}

Expand Down Expand Up @@ -427,11 +439,11 @@ func replaceFilenameWithPathTemplate(path, filename string) string {

// SearchResourcesFilterCELAttributes are the CEL attributes for SearchResourcesFilter.
var SearchResourcesFilterCELAttributes = []cel.EnvOption{
cel.Variable("uid", cel.StringType),
cel.Variable("filename", cel.StringType),
}

type SearchResourcesFilter struct {
UID *string
Filename *string
}

func parseSearchResourcesFilter(expression string) (*SearchResourcesFilter, error) {
Expand All @@ -457,9 +469,9 @@ func findSearchResourcesField(callExpr *expr.Expr_Call, filter *SearchResourcesF
if len(callExpr.Args) == 2 {
idExpr := callExpr.Args[0].GetIdentExpr()
if idExpr != nil {
if idExpr.Name == "uid" {
uid := callExpr.Args[1].GetConstExpr().GetStringValue()
filter.UID = &uid
if idExpr.Name == "filename" {
filename := callExpr.Args[1].GetConstExpr().GetStringValue()
filter.Filename = &filename
}
return
}
Expand Down
3 changes: 3 additions & 0 deletions store/db/mysql/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*st
if v := find.Filename; v != nil {
where, args = append(where, "`filename` = ?"), append(args, *v)
}
if v := find.FilenameSearch; v != nil {
where, args = append(where, "`filename` LIKE ?"), append(args, "%"+*v+"%")
}
if v := find.MemoID; v != nil {
where, args = append(where, "`memo_id` = ?"), append(args, *v)
}
Expand Down
3 changes: 3 additions & 0 deletions store/db/postgres/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*st
if v := find.Filename; v != nil {
where, args = append(where, "filename = "+placeholder(len(args)+1)), append(args, *v)
}
if v := find.FilenameSearch; v != nil {
where, args = append(where, "filename LIKE "+placeholder(len(args)+1)), append(args, fmt.Sprintf("%%%s%%", *v))
}
if v := find.MemoID; v != nil {
where, args = append(where, "memo_id = "+placeholder(len(args)+1)), append(args, *v)
}
Expand Down
3 changes: 3 additions & 0 deletions store/db/sqlite/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func (d *DB) ListResources(ctx context.Context, find *store.FindResource) ([]*st
if v := find.Filename; v != nil {
where, args = append(where, "`filename` = ?"), append(args, *v)
}
if v := find.FilenameSearch; v != nil {
where, args = append(where, "`filename` LIKE ?"), append(args, fmt.Sprintf("%%%s%%", *v))
}
if v := find.MemoID; v != nil {
where, args = append(where, "`memo_id` = ?"), append(args, *v)
}
Expand Down
1 change: 1 addition & 0 deletions store/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type FindResource struct {
UID *string
CreatorID *int32
Filename *string
FilenameSearch *string
MemoID *int32
HasRelatedMemo bool
StorageType *storepb.ResourceStorageType
Expand Down
Loading

0 comments on commit 457cf92

Please sign in to comment.