Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions api/favorite/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: Apache-2.0

package favorite

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"

"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
)

// swagger:operation POST /api/v1/user/favorites favorites CreateFavorite
//
// Save the current authenticated user's favorites
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '201':
// description: Successfully added user favorite
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"

// CreateFavorite represents the API handler to add a
// favorite for the currently authenticated user.
func CreateFavorite(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

favorite := new(types.Favorite)

err := c.Bind(favorite)
if err != nil {
retErr := err

util.HandleError(ctx, http.StatusBadRequest, retErr)

return
}

err = database.FromContext(c).CreateFavorite(ctx, u, favorite)
if err != nil {
retErr := fmt.Errorf("unable to create favorite for user %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

c.Status(http.StatusCreated)
}
62 changes: 62 additions & 0 deletions api/favorite/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: Apache-2.0

package favorite

import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
)

// swagger:operation DELETE /api/v1/user/favorites/{org}/{repo} favorites DeleteFavorite
//
// Remove the current authenticated user's favorite
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: org
// description: Name of the organization
// required: true
// type: string
// - in: path
// name: repo
// description: Name of the repository
// required: true
// type: string
// security:
// - ApiKeyAuth: []
// responses:
// '204':
// description: Successfully removed user favorite
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"

// DeleteFavorite represents the API handler to delete a
// favorite for the currently authenticated user.
func DeleteFavorite(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
r := repo.Retrieve(c)
ctx := c.Request.Context()

err := database.FromContext(c).DeleteFavorite(ctx, u, r)
if err != nil {
retErr := err

util.HandleError(ctx, http.StatusInternalServerError, retErr)

return
}

c.Status(http.StatusNoContent)
}
54 changes: 54 additions & 0 deletions api/favorite/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: Apache-2.0

package favorite

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"

"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
)

// swagger:operation GET /api/v1/user/favorites favorites ListFavorites
//
// Get the current authenticated user's favorites
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved the current user's favorites
// schema:
// type: array
// items:
// "$ref": "#/definitions/Favorite"
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"

// ListFavorites represents the API handler to capture the
// currently authenticated user's favorites.
func ListFavorites(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()

favorites, err := database.FromContext(c).ListUserFavorites(ctx, u)
if err != nil {
retErr := fmt.Errorf("unable to get favorites for user %s: %w", u.GetName(), err)

util.HandleError(ctx, http.StatusInternalServerError, retErr)

return
}

c.JSON(http.StatusOK, favorites)
}
75 changes: 75 additions & 0 deletions api/favorite/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: Apache-2.0

package favorite

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"

"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
)

// swagger:operation PUT /api/v1/user/favorites/{org}/{repo} favorites UpdateFavorite
//
// Update the current authenticated user's favorite
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: org
// description: Name of the organization
// required: true
// type: string
// - in: path
// name: repo
// description: Name of the repository
// required: true
// type: string
// security:
// - ApiKeyAuth: []
// responses:
// '204':
// description: Successfully updated favorite position
// '401':
// description: Unauthorized
// schema:
// "$ref": "#/definitions/Error"

// UpdateFavorite represents the API handler to update the
// currently authenticated user's favorite position.
func UpdateFavorite(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
r := repo.Retrieve(c)
ctx := c.Request.Context()

favorite := new(types.Favorite)

err := c.Bind(favorite)
if err != nil {
retErr := err

util.HandleError(ctx, http.StatusBadRequest, retErr)

return
}

err = database.FromContext(c).UpdateFavoritePosition(ctx, u, r, favorite)
if err != nil {
retErr := fmt.Errorf("unable to update favorite position for user %s: %w", u.GetName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

c.Status(http.StatusNoContent)
}
78 changes: 78 additions & 0 deletions api/types/favorite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: Apache-2.0

package types

import (
"fmt"
)

// Favorite is the API representation of a user's favorite.
//
// swagger:model Favorite
type Favorite struct {
Position *int64 `json:"position,omitempty"`
Repo *string `json:"repo,omitempty"`
}

// GetPosition returns the Position field.
//
// When the provided Favorite type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (f *Favorite) GetPosition() int64 {
// return zero value if Favorite type or Position field is nil
if f == nil || f.Position == nil {
return 0
}

return *f.Position
}

// GetRepo returns the Repo field.
//
// When the provided Favorite type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (f *Favorite) GetRepo() string {
// return zero value if Favorite type or Repo field is nil
if f == nil || f.Repo == nil {
return ""
}

return *f.Repo
}

// SetPosition sets the Position field.
//
// When the provided Favorite type is nil, it
// will set nothing and immediately return.
func (f *Favorite) SetPosition(v int64) {
// return if Favorite type is nil
if f == nil {
return
}

f.Position = &v
}

// SetRepo sets the Repo field.
//
// When the provided Favorite type is nil, it
// will set nothing and immediately return.
func (f *Favorite) SetRepo(v string) {
// return if Favorite type is nil
if f == nil {
return
}

f.Repo = &v
}

// String implements the Stringer interface for the Favorite type.
func (f *Favorite) String() string {
return fmt.Sprintf(`{
Position: %d,
Repo: %s,
}`,
f.GetPosition(),
f.GetRepo(),
)
}
Loading