From 416bd1ba3668d86a49e928743b10515ec580c044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Ga=C4=87e=C5=A1a?= Date: Mon, 31 Jul 2023 12:36:31 +0200 Subject: [PATCH] openapi update for the sapce membership list API --- internal/api/openapi/space.go | 40 +++++++++++++++++++++++++-- internal/store/database/membership.go | 1 - types/enum/membership.go | 26 +++++++++++------ 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/internal/api/openapi/space.go b/internal/api/openapi/space.go index 0a95c73de..04569762c 100644 --- a/internal/api/openapi/space.go +++ b/internal/api/openapi/space.go @@ -115,6 +115,36 @@ var queryParameterQuerySpace = openapi3.ParameterOrRef{ }, } +var queryParameterSpaceMembers = openapi3.ParameterOrRef{ + Parameter: &openapi3.Parameter{ + Name: request.QueryParamQuery, + In: openapi3.ParameterInQuery, + Description: ptr.String("The substring by which the space members are filtered."), + Required: ptr.Bool(false), + Schema: &openapi3.SchemaOrRef{ + Schema: &openapi3.Schema{ + Type: ptrSchemaType(openapi3.SchemaTypeString), + }, + }, + }, +} + +var queryParameterSortSpaceMembers = openapi3.ParameterOrRef{ + Parameter: &openapi3.Parameter{ + Name: request.QueryParamSort, + In: openapi3.ParameterInQuery, + Description: ptr.String("The field by which the space members are sorted."), + Required: ptr.Bool(false), + Schema: &openapi3.SchemaOrRef{ + Schema: &openapi3.Schema{ + Type: ptrSchemaType(openapi3.SchemaTypeString), + Default: ptrptr(enum.MembershipSortName), + Enum: enum.MembershipSort("").Enum(), + }, + }, + }, +} + //nolint:funlen // api spec generation no need for checking func complexity func spaceOperations(reflector *openapi3.Reflector) { opCreate := openapi3.Operation{} @@ -252,7 +282,7 @@ func spaceOperations(reflector *openapi3.Reflector) { spaceRequest space.MembershipAddInput }{}, http.MethodPost) - _ = reflector.SetJSONResponse(&opMembershipAdd, &types.Membership{}, http.StatusCreated) + _ = reflector.SetJSONResponse(&opMembershipAdd, &types.MembershipUser{}, http.StatusCreated) _ = reflector.SetJSONResponse(&opMembershipAdd, new(usererror.Error), http.StatusInternalServerError) _ = reflector.SetJSONResponse(&opMembershipAdd, new(usererror.Error), http.StatusUnauthorized) _ = reflector.SetJSONResponse(&opMembershipAdd, new(usererror.Error), http.StatusForbidden) @@ -281,7 +311,7 @@ func spaceOperations(reflector *openapi3.Reflector) { UserUID string `path:"user_uid"` space.MembershipUpdateInput }{}, http.MethodPatch) - _ = reflector.SetJSONResponse(&opMembershipUpdate, &types.Membership{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opMembershipUpdate, &types.MembershipUser{}, http.StatusOK) _ = reflector.SetJSONResponse(&opMembershipUpdate, new(usererror.Error), http.StatusInternalServerError) _ = reflector.SetJSONResponse(&opMembershipUpdate, new(usererror.Error), http.StatusUnauthorized) _ = reflector.SetJSONResponse(&opMembershipUpdate, new(usererror.Error), http.StatusForbidden) @@ -291,10 +321,14 @@ func spaceOperations(reflector *openapi3.Reflector) { opMembershipList := openapi3.Operation{} opMembershipList.WithTags("space") opMembershipList.WithMapOfAnything(map[string]interface{}{"operationId": "membershipList"}) + opMembershipList.WithParameters( + queryParameterSpaceMembers, + queryParameterOrder, queryParameterSortSpaceMembers, + queryParameterPage, queryParameterLimit) _ = reflector.SetRequest(&opMembershipList, &struct { spaceRequest }{}, http.MethodGet) - _ = reflector.SetJSONResponse(&opMembershipList, []types.Membership{}, http.StatusOK) + _ = reflector.SetJSONResponse(&opMembershipList, []types.MembershipUser{}, http.StatusOK) _ = reflector.SetJSONResponse(&opMembershipList, new(usererror.Error), http.StatusInternalServerError) _ = reflector.SetJSONResponse(&opMembershipList, new(usererror.Error), http.StatusUnauthorized) _ = reflector.SetJSONResponse(&opMembershipList, new(usererror.Error), http.StatusForbidden) diff --git a/internal/store/database/membership.go b/internal/store/database/membership.go index 4aaf54ebc..3826612ad 100644 --- a/internal/store/database/membership.go +++ b/internal/store/database/membership.go @@ -235,7 +235,6 @@ func (s *MembershipStore) ListUsers(ctx context.Context, stmt = stmt.OrderBy("principal_display_name " + order.String()) case enum.MembershipSortCreated: stmt = stmt.OrderBy("membership_created " + order.String()) - case enum.MembershipSortNone: } sql, args, err := stmt.ToSql() diff --git a/types/enum/membership.go b/types/enum/membership.go index 42a63d73b..60e831900 100644 --- a/types/enum/membership.go +++ b/types/enum/membership.go @@ -9,15 +9,25 @@ import ( ) // MembershipSort represents membership sort order. -type MembershipSort int +type MembershipSort string // Order enumeration. const ( - MembershipSortNone MembershipSort = iota - MembershipSortName - MembershipSortCreated + MembershipSortName = name + MembershipSortCreated = created ) +var membershipSorts = sortEnum([]MembershipSort{ + MembershipSortName, + MembershipSortCreated, +}) + +func (MembershipSort) Enum() []interface{} { return toInterfaceSlice(membershipSorts) } +func (s MembershipSort) Sanitize() (MembershipSort, bool) { return Sanitize(s, GetAllMembershipSorts) } +func GetAllMembershipSorts() ([]MembershipSort, MembershipSort) { + return membershipSorts, MembershipSortName +} + // ParseMembershipSort parses the membership sort attribute string // and returns the equivalent enumeration. func ParseMembershipSort(s string) MembershipSort { @@ -27,19 +37,17 @@ func ParseMembershipSort(s string) MembershipSort { case created, createdAt: return MembershipSortCreated default: - return MembershipSortNone + return MembershipSortName } } // String returns the string representation of the attribute. -func (a MembershipSort) String() string { - switch a { +func (s MembershipSort) String() string { + switch s { case MembershipSortName: return name case MembershipSortCreated: return created - case MembershipSortNone: - return "" default: return undefined }