watches.go 956 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "context"
  7. "gorm.io/gorm"
  8. )
  9. // WatchesStore is the persistent interface for watches.
  10. //
  11. // NOTE: All methods are sorted in alphabetical order.
  12. type WatchesStore interface {
  13. // ListByRepo returns all watches of the given repository.
  14. ListByRepo(ctx context.Context, repoID int64) ([]*Watch, error)
  15. }
  16. var Watches WatchesStore
  17. var _ WatchesStore = (*watches)(nil)
  18. type watches struct {
  19. *gorm.DB
  20. }
  21. // NewWatchesStore returns a persistent interface for watches with given
  22. // database connection.
  23. func NewWatchesStore(db *gorm.DB) WatchesStore {
  24. return &watches{DB: db}
  25. }
  26. func (db *watches) ListByRepo(ctx context.Context, repoID int64) ([]*Watch, error) {
  27. var watches []*Watch
  28. return watches, db.WithContext(ctx).Where("repo_id = ?", repoID).Find(&watches).Error
  29. }