summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/user
diff options
context:
space:
mode:
authorEthan Koenig <ethantkoenig@gmail.com>2016-11-14 17:33:58 -0500
committerEthan Koenig <ethantkoenig@gmail.com>2016-11-16 22:51:54 -0500
commit0834e492c06e30d30c2bc8eec4fcb6c499a62ced (patch)
tree9a7888958b31770b2a4a4411e08095452f1d5e4a /routers/api/v1/user
parent871c964ef7aaf70af5e9eab00a3d6642432b122a (diff)
downloadgitea-0834e492c06e30d30c2bc8eec4fcb6c499a62ced.tar.gz
gitea-0834e492c06e30d30c2bc8eec4fcb6c499a62ced.zip
API endpoints for stars
Diffstat (limited to 'routers/api/v1/user')
-rw-r--r--routers/api/v1/user/star.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go
new file mode 100644
index 0000000000..c856ecefdd
--- /dev/null
+++ b/routers/api/v1/user/star.go
@@ -0,0 +1,76 @@
+// Copyright 2016 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package user
+
+import (
+ api "code.gitea.io/sdk/gitea"
+
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/context"
+)
+
+// getStarredRepos returns the repos that the user with the specified userID has
+// starred
+func getStarredRepos(userID int64, private bool) ([]*api.Repository, error) {
+ starredRepos, err := models.GetStarredRepos(userID, private)
+ if err != nil {
+ return nil, err
+ }
+ repos := make([]*api.Repository, len(starredRepos))
+ for i, starred := range starredRepos {
+ repos[i] = starred.APIFormat(&api.Permission{true, true, true})
+ }
+ return repos, nil
+}
+
+// GetStarredRepos returns the repos that the user specified by the APIContext
+// has starred
+func GetStarredRepos(ctx *context.APIContext) {
+ user := GetUserByParams(ctx)
+ private := user.ID == ctx.User.ID
+ repos, err := getStarredRepos(user.ID, private)
+ if err != nil {
+ ctx.Error(500, "getStarredRepos", err)
+ }
+ ctx.JSON(200, &repos)
+}
+
+// GetMyStarredRepos returns the repos that the authenticated user has starred
+func GetMyStarredRepos(ctx *context.APIContext) {
+ repos, err := getStarredRepos(ctx.User.ID, true)
+ if err != nil {
+ ctx.Error(500, "getStarredRepos", err)
+ }
+ ctx.JSON(200, &repos)
+}
+
+// IsStarring returns whether the authenticated is starring the repo
+func IsStarring(ctx *context.APIContext) {
+ if models.IsStaring(ctx.User.ID, ctx.Repo.Repository.ID) {
+ ctx.Status(204)
+ } else {
+ ctx.Status(404)
+ }
+}
+
+// Star the repo specified in the APIContext, as the authenticated user
+func Star(ctx *context.APIContext) {
+ err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
+ if err != nil {
+ ctx.Error(500, "StarRepo", err)
+ return
+ }
+ ctx.Status(204)
+}
+
+// Unstar the repo specified in the APIContext, as the authenticated user
+func Unstar(ctx *context.APIContext) {
+ err := models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
+ if err != nil {
+ ctx.Error(500, "StarRepo", err)
+ return
+ }
+ ctx.Status(204)
+}