Skip to content

Commit

Permalink
feat(spx-backend): implement Community API (#955)
Browse files Browse the repository at this point in the history
Fixes #920
Fixes #948

Signed-off-by: Aofei Sheng <aofei@aofeisheng.com>
  • Loading branch information
aofei authored Oct 15, 2024
1 parent 1582347 commit 92c514e
Show file tree
Hide file tree
Showing 80 changed files with 7,820 additions and 3,682 deletions.
239 changes: 92 additions & 147 deletions docs/openapi.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion spx-backend/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ ZNIZlCk3CrqUzVnAln07K9++5egCisZ0XADLp1cgCZk0NRM7a2LuMDx/iZXw1QaQ
Iw==
-----END CERTIFICATE-----"
GOP_CASDOOR_ORGANIZATIONNAME="GoPlus"
GOP_CASDOOR_APPLICATONNAME="application_x8aevk"
GOP_CASDOOR_APPLICATIONNAME="application_x8aevk"
2 changes: 1 addition & 1 deletion spx-backend/cmd/spx-backend/delete_asset_#id.yap
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ if err := ctrl.DeleteAsset(ctx.Context(), ${id}); err != nil {
replyWithInnerError(ctx, err)
return
}
json nil
text 204, "", ""
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ if err := ctrl.DeleteProject(ctx.Context(), ${owner}, ${name}); err != nil {
replyWithInnerError(ctx, err)
return
}
json nil
text 204, "", ""
16 changes: 16 additions & 0 deletions spx-backend/cmd/spx-backend/delete_project_#owner_#name_liking.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Unlike a project.
//
// Request:
// DELETE /project/:owner/:name/liking

ctx := &Context

if _, ok := ensureUser(ctx); !ok {
return
}

if err := ctrl.UnlikeProject(ctx.Context(), ${owner}, ${name}); err != nil {
replyWithInnerError(ctx, err)
return
}
text 204, "", ""
16 changes: 16 additions & 0 deletions spx-backend/cmd/spx-backend/delete_user_#username_following.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Unfollow a user.
//
// Request:
// DELETE /user/:username/following

ctx := &Context

if _, ok := ensureUser(ctx); !ok {
return
}

if err := ctrl.UnfollowUser(ctx.Context(), ${username}); err != nil {
replyWithInnerError(ctx, err)
return
}
text 204, "", ""
35 changes: 11 additions & 24 deletions spx-backend/cmd/spx-backend/get_assets_list.yap
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,45 @@
// GET /assets/list

import (
"strconv"

"github.com/goplus/builder/spx-backend/internal/controller"
"github.com/goplus/builder/spx-backend/internal/model"
)

ctx := &Context

user, _ := controller.UserFromContext(ctx.Context())
params := &controller.ListAssetsParams{}
params := controller.NewListAssetsParams()

params.Keyword = ${keyword}
if keyword := ${keyword}; keyword != "" {
params.Keyword = &keyword
}

switch owner := ${owner}; owner {
case "":
if user == nil {
replyWithCode(ctx, errorUnauthorized)
return
}
params.Owner = &user.Name
params.Owner = &user.Username
case "*":
params.Owner = nil
default:
params.Owner = &owner
}

if category := ${category}; category != "" {
params.Category = &category
if typeParam := ctx.Param("type"); typeParam != "" {
params.Type = &typeParam
}

if assetTypeParam := ${assetType}; assetTypeParam != "" {
assetTypeInt, err := strconv.Atoi(assetTypeParam)
if err != nil {
replyWithCode(ctx, errorInvalidArgs)
return
}
assetType := model.AssetType(assetTypeInt)
params.AssetType = &assetType
if category := ${category}; category != "" {
params.Category = &category
}

if filesHash := ${filesHash}; filesHash != "" {
params.FilesHash = &filesHash
}

if isPublicParam := ${isPublic}; isPublicParam != "" {
isPublicInt, err := strconv.Atoi(isPublicParam)
if err != nil {
replyWithCode(ctx, errorInvalidArgs)
return
}
isPublic := model.IsPublic(isPublicInt)
params.IsPublic = &isPublic
if visibility := ${visibility}; visibility != "" {
params.Visibility = &visibility
}

if orderBy := ${orderBy}; orderBy != "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Get project release by its full name.
//
// Request:
// GET /project-release/:owner/:project/:release

ctx := &Context

projectRelease, err := ctrl.GetProjectRelease(ctx.Context(), ${owner}, ${project}, ${release})
if err != nil {
replyWithInnerError(ctx, err)
return
}
json projectRelease
34 changes: 34 additions & 0 deletions spx-backend/cmd/spx-backend/get_project-releases_list.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// List project releases.
//
// Request:
// GET /project-releases/list

import (
"github.com/goplus/builder/spx-backend/internal/controller"
)

ctx := &Context

params := controller.NewListProjectReleasesParams()

if projectFullName := ${projectFullName}; projectFullName != "" {
params.ProjectFullName = &projectFullName
}

if orderBy := ${orderBy}; orderBy != "" {
params.OrderBy = controller.ListProjectReleasesOrderBy(orderBy)
}

params.Pagination.Index = ctx.ParamInt("pageIndex", firstPageIndex)
params.Pagination.Size = ctx.ParamInt("pageSize", defaultPageSize)
if ok, msg := params.Validate(); !ok {
replyWithCodeMsg(ctx, errorInvalidArgs, msg)
return
}

projectReleases, err := ctrl.ListProjectReleases(ctx.Context(), params)
if err != nil {
replyWithInnerError(ctx, err)
return
}
json projectReleases
21 changes: 21 additions & 0 deletions spx-backend/cmd/spx-backend/get_project_#owner_#name_liking.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Check if a project is liked by the authenticated user.
//
// Request:
// GET /project/:owner/:name/liking

ctx := &Context

if _, ok := ensureUser(ctx); !ok {
return
}

hasLiked, err := ctrl.HasLikedProject(ctx.Context(), ${owner}, ${name})
if err != nil {
replyWithInnerError(ctx, err)
return
}
if hasLiked {
ctx.text 204, "", ""
} else {
replyWithCode(ctx, errorNotFound)
}
72 changes: 59 additions & 13 deletions spx-backend/cmd/spx-backend/get_projects_list.yap
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,85 @@

import (
"strconv"
"time"

"github.com/goplus/builder/spx-backend/internal/controller"
"github.com/goplus/builder/spx-backend/internal/model"
)

ctx := &Context

user, _ := controller.UserFromContext(ctx.Context())
params := &controller.ListProjectsParams{}

if isPublicParam := ${isPublic}; isPublicParam != "" {
isPublicInt, err := strconv.Atoi(isPublicParam)
if err != nil {
replyWithCode(ctx, errorInvalidArgs)
return
}
isPublic := model.IsPublic(isPublicInt)
params.IsPublic = &isPublic
}
params := controller.NewListProjectsParams()

switch owner := ${owner}; owner {
case "":
if user == nil {
replyWithCode(ctx, errorUnauthorized)
return
}
params.Owner = &user.Name
params.Owner = &user.Username
case "*":
params.Owner = nil
default:
params.Owner = &owner
}

if remixedFrom := ${remixedFrom}; remixedFrom != "" {
params.RemixedFrom = &remixedFrom
}

if keyword := ${keyword}; keyword != "" {
params.Keyword = &keyword
}

if visibility := ${visibility}; visibility != "" {
params.Visibility = &visibility
}

if liker := ${liker}; liker != "" {
params.Liker = &liker
}

if createdAfter := ${createdAfter}; createdAfter != "" {
createdAfterTime, err := time.Parse(time.RFC3339Nano, createdAfter)
if err != nil {
replyWithCodeMsg(ctx, errorInvalidArgs, "invalid createdAfter")
return
}
params.CreatedAfter = &createdAfterTime
}

if likesReceivedAfter := ${likesReceivedAfter}; likesReceivedAfter != "" {
likesReceivedAfterTime, err := time.Parse(time.RFC3339Nano, likesReceivedAfter)
if err != nil {
replyWithCodeMsg(ctx, errorInvalidArgs, "invalid likesReceivedAfter")
return
}
params.LikesReceivedAfter = &likesReceivedAfterTime
}

if remixesReceivedAfter := ${remixesReceivedAfter}; remixesReceivedAfter != "" {
remixesReceivedAfterTime, err := time.Parse(time.RFC3339Nano, remixesReceivedAfter)
if err != nil {
replyWithCodeMsg(ctx, errorInvalidArgs, "invalid remixesReceivedAfter")
return
}
params.RemixesReceivedAfter = &remixesReceivedAfterTime
}

if fromFollowees := ${fromFollowees}; fromFollowees != "" {
fromFolloweesBool, err := strconv.ParseBool(fromFollowees)
if err != nil {
replyWithCodeMsg(ctx, errorInvalidArgs, "invalid fromFollowees")
return
}
params.FromFollowees = &fromFolloweesBool
}

if orderBy := ${orderBy}; orderBy != "" {
params.OrderBy = controller.ListProjectsOrderBy(orderBy)
}

params.Pagination.Index = paramInt("pageIndex", firstPageIndex)
params.Pagination.Size = paramInt("pageSize", defaultPageSize)
if ok, msg := params.Validate(); !ok {
Expand Down
13 changes: 13 additions & 0 deletions spx-backend/cmd/spx-backend/get_user_#username.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Get user by username.
//
// Request:
// GET /user/:username

ctx := &Context

user, err := ctrl.GetUser(ctx.Context(), ${username})
if err != nil {
replyWithInnerError(ctx, err)
return
}
json user
21 changes: 21 additions & 0 deletions spx-backend/cmd/spx-backend/get_user_#username_following.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Check if a user is followed by the authenticated user.
//
// Request:
// GET /user/:username/following

ctx := &Context

if _, ok := ensureUser(ctx); !ok {
return
}

isFollowing, err := ctrl.IsFollowingUser(ctx.Context(), ${username})
if err != nil {
replyWithInnerError(ctx, err)
return
}
if isFollowing {
ctx.text 204, "", ""
} else {
replyWithCode(ctx, errorNotFound)
}
38 changes: 38 additions & 0 deletions spx-backend/cmd/spx-backend/get_users_list.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// List users.
//
// Request:
// GET /users/list

import (
"github.com/goplus/builder/spx-backend/internal/controller"
)

ctx := &Context

params := controller.NewListUsersParams()

if follower := ${follower}; follower != "" {
params.Follower = &follower
}

if followee := ${followee}; followee != "" {
params.Followee = &followee
}

if orderBy := ${orderBy}; orderBy != "" {
params.OrderBy = controller.ListUsersOrderBy(orderBy)
}

params.Pagination.Index = ctx.ParamInt("pageIndex", firstPageIndex)
params.Pagination.Size = ctx.ParamInt("pageSize", defaultPageSize)
if ok, msg := params.Validate(); !ok {
replyWithCodeMsg(ctx, errorInvalidArgs, msg)
return
}

users, err := ctrl.ListUsers(ctx.Context(), params)
if err != nil {
replyWithInnerError(ctx, err)
return
}
json users
Loading

0 comments on commit 92c514e

Please sign in to comment.