2024-04-12 11:52:26 +00:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
2024-03-21 07:25:40 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2024-03-22 16:41:59 +00:00
|
|
|
"context"
|
|
|
|
|
2024-03-21 07:25:40 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-03-22 16:41:59 +00:00
|
|
|
"code.gitea.io/gitea/modules/validation"
|
2024-03-21 07:25:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2024-04-03 10:26:33 +00:00
|
|
|
db.RegisterModel(new(FollowingRepo))
|
2024-03-21 07:25:40 +00:00
|
|
|
}
|
2024-03-22 16:41:59 +00:00
|
|
|
|
2024-04-04 13:08:02 +00:00
|
|
|
func FindFollowingReposByRepoID(ctx context.Context, repoID int64) ([]*FollowingRepo, error) {
|
2024-04-03 10:08:44 +00:00
|
|
|
maxFollowingRepos := 10
|
2024-04-04 13:08:02 +00:00
|
|
|
sess := db.GetEngine(ctx).Where("repo_id=?", repoID)
|
2024-04-03 10:08:44 +00:00
|
|
|
sess = sess.Limit(maxFollowingRepos, 0)
|
2024-04-03 10:26:33 +00:00
|
|
|
followingRepoList := make([]*FollowingRepo, 0, maxFollowingRepos)
|
|
|
|
err := sess.Find(&followingRepoList)
|
2024-03-25 15:47:18 +00:00
|
|
|
if err != nil {
|
2024-04-03 10:26:33 +00:00
|
|
|
return make([]*FollowingRepo, 0, maxFollowingRepos), err
|
2024-03-25 15:47:18 +00:00
|
|
|
}
|
2024-04-03 10:26:33 +00:00
|
|
|
for _, followingRepo := range followingRepoList {
|
|
|
|
if res, err := validation.IsValid(*followingRepo); !res {
|
2024-05-13 09:14:04 +00:00
|
|
|
return make([]*FollowingRepo, 0, maxFollowingRepos), err
|
2024-03-25 15:47:18 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-03 10:26:33 +00:00
|
|
|
return followingRepoList, nil
|
2024-03-22 16:41:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 13:08:02 +00:00
|
|
|
func StoreFollowingRepos(ctx context.Context, localRepoID int64, followingRepoList []*FollowingRepo) error {
|
2024-04-03 10:26:33 +00:00
|
|
|
for _, followingRepo := range followingRepoList {
|
|
|
|
if res, err := validation.IsValid(*followingRepo); !res {
|
2024-05-13 09:14:04 +00:00
|
|
|
return err
|
2024-03-22 19:14:20 +00:00
|
|
|
}
|
2024-03-22 16:41:59 +00:00
|
|
|
}
|
2024-03-22 19:14:20 +00:00
|
|
|
|
|
|
|
// Begin transaction
|
|
|
|
ctx, committer, err := db.TxContext((ctx))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
2024-04-04 13:08:02 +00:00
|
|
|
_, err = db.GetEngine(ctx).Where("repo_id=?", localRepoID).Delete(FollowingRepo{})
|
2024-03-22 19:14:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-03 10:26:33 +00:00
|
|
|
for _, followingRepo := range followingRepoList {
|
|
|
|
_, err = db.GetEngine(ctx).Insert(followingRepo)
|
2024-03-22 19:14:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
return committer.Commit()
|
2024-03-22 16:41:59 +00:00
|
|
|
}
|