Skip to content

API endpoints for stars #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
repos := make([]*Repository, 0, pageSize)
// FIXME: use XORM chain operations instead of raw SQL.
if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
INNER JOIN team_repo
INNER JOIN team_repo
ON team_repo.repo_id = repository.id
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
GROUP BY repository.id
Expand All @@ -507,7 +507,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
}

results, err := x.Query(fmt.Sprintf(`SELECT repository.id FROM repository
INNER JOIN team_repo
INNER JOIN team_repo
ON team_repo.repo_id = repository.id
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
GROUP BY repository.id
Expand All @@ -534,7 +534,7 @@ func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error)

repos := make([]*Repository, 0, 10)
if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
INNER JOIN team_repo
INNER JOIN team_repo
ON team_repo.repo_id = repository.id AND repository.is_mirror = ?
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
GROUP BY repository.id
Expand Down
19 changes: 19 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,3 +1121,22 @@ func UnfollowUser(userID, followID int64) (err error) {
}
return sess.Commit()
}

func GetStarredRepos(userID int64) ([]*Repository, error) {
sess := x.NewSession()
defer sessionRelease(sess)
stars := make([]*Star, 0, 10)
err := sess.Find(&stars, &Star{UID: userID})
if err != nil {
return nil, err
}
repos := make([]*Repository, len(stars))
for i, star := range stars {
r, err := GetRepositoryByID(star.RepoID)
if err != nil {
return nil, err
}
repos[i] = r
}
return repos, nil
}
11 changes: 11 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("", user.ListFollowing)
m.Get("/:target", user.CheckFollowing)
})

m.Get("/starred", user.GetStarredRepos)
})
}, reqToken())

Expand All @@ -221,6 +223,15 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Combo("/:id").Get(user.GetPublicKey).
Delete(user.DeletePublicKey)
})

m.Group("/starred", func() {
m.Get("", user.GetMyStarredRepos)
m.Group("/:username/:reponame", func() {
m.Get("", user.IsStarring)
m.Put("", user.Star)
m.Delete("", user.Unstar)
})
})
}, reqToken())

// Repositories
Expand Down
6 changes: 3 additions & 3 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
}

func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
func ParseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
owner, err := models.GetUserByName(ctx.Params(":username"))
if err != nil {
if models.IsErrUserNotExist(err) {
Expand All @@ -264,7 +264,7 @@ func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repositor

// https://github.com/gogits/go-gogs-client/wiki/Repositories#get
func Get(ctx *context.APIContext) {
_, repo := parseOwnerAndRepo(ctx)
_, repo := ParseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
Expand All @@ -274,7 +274,7 @@ func Get(ctx *context.APIContext) {

// https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
func Delete(ctx *context.APIContext) {
owner, repo := parseOwnerAndRepo(ctx)
owner, repo := ParseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
Expand Down
80 changes: 80 additions & 0 deletions routers/api/v1/user/star.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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 "github.com/gogits/go-gogs-client"

"github.com/go-gitea/gitea/models"
"github.com/go-gitea/gitea/modules/context"
"github.com/go-gitea/gitea/routers/api/v1/repo"
)

func getStarredRepos(userID int64) ([]*api.Repository, error) {
starredRepos, err := models.GetStarredRepos(userID)
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
}

func GetStarredRepos(ctx *context.APIContext) {
user := GetUserByParams(ctx)
repos, err := getStarredRepos(user.ID)
if err != nil {
ctx.Error(500, "getStarredRepos", err)
}
ctx.JSON(200, &repos)
}

func GetMyStarredRepos(ctx *context.APIContext) {
repos, err := getStarredRepos(ctx.User.ID)
if err != nil {
ctx.Error(500, "getStarredRepos", err)
}
ctx.JSON(200, &repos)
}

func IsStarring(ctx *context.APIContext) {
_, repository := repo.ParseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
if models.IsStaring(ctx.User.ID, repository.ID) {
ctx.Status(204)
} else {
ctx.Status(404)
}
}

func Star(ctx *context.APIContext) {
_, repository := repo.ParseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
err := models.StarRepo(ctx.User.ID, repository.ID, true)
if err != nil {
ctx.Error(500, "StarRepo", err)
return
}
ctx.Status(204)
}

func Unstar(ctx *context.APIContext) {
_, repository := repo.ParseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
err := models.StarRepo(ctx.User.ID, repository.ID, false)
if err != nil {
ctx.Error(500, "StarRepo", err)
return
}
ctx.Status(204)
}