-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Tab on user profile to show starred repos #519
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e999f86
Tab on user profile to show starred repos
andreynering ada59e4
Make golint happy and use transactions on StarRepo function
andreynering 1ad427b
x -> sess
andreynering ad2d212
Use sess.Close() instead of sess.Rollback()
andreynering 6a74c4a
Add copyright
andreynering 993cf81
Fix lint
andreynering File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2016 The Gitea 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 models | ||
|
||
// Star represents a starred repo by an user. | ||
type Star struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
UID int64 `xorm:"UNIQUE(s)"` | ||
RepoID int64 `xorm:"UNIQUE(s)"` | ||
} | ||
|
||
// StarRepo or unstar repository. | ||
func StarRepo(userID, repoID int64, star bool) error { | ||
sess := x.NewSession() | ||
|
||
defer sess.Close() | ||
|
||
if err := sess.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
if star { | ||
if IsStaring(userID, repoID) { | ||
return nil | ||
} | ||
|
||
if _, err := sess.Insert(&Star{UID: userID, RepoID: repoID}); err != nil { | ||
return err | ||
} | ||
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil { | ||
return err | ||
} | ||
if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil { | ||
return err | ||
} | ||
} else { | ||
if !IsStaring(userID, repoID) { | ||
return nil | ||
} | ||
|
||
if _, err := sess.Delete(&Star{0, userID, repoID}); err != nil { | ||
return err | ||
} | ||
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil { | ||
return err | ||
} | ||
if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return sess.Commit() | ||
} | ||
|
||
// IsStaring checks if user has starred given repository. | ||
func IsStaring(userID, repoID int64) bool { | ||
has, _ := x.Get(&Star{0, userID, repoID}) | ||
return has | ||
} | ||
|
||
// GetStargazers returns the users that starred the repo. | ||
func (repo *Repository) GetStargazers(page int) ([]*User, error) { | ||
users := make([]*User, 0, ItemsPerPage) | ||
err := x. | ||
Limit(ItemsPerPage, (page-1)*ItemsPerPage). | ||
Where("star.repo_id = ?", repo.ID). | ||
Join("LEFT", "star", "`user`.id = star.uid"). | ||
Find(&users) | ||
return users, err | ||
} | ||
|
||
// GetStarredRepos returns the repos the user starred. | ||
func (u *User) GetStarredRepos(private bool) (repos []*Repository, err error) { | ||
sess := x. | ||
Join("INNER", "star", "star.repo_id = repository.id"). | ||
Where("star.uid = ?", u.ID) | ||
|
||
if !private { | ||
sess = sess.And("is_private = ?", false) | ||
} | ||
|
||
err = sess. | ||
Find(&repos) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This really doesn't need to be on a separate line, but nitpicking |
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copyright?