mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-04 05:40:13 +00:00
refactor FederationInfo -> FederationHost
This commit is contained in:
parent
e926ea16e2
commit
c58a995fb2
5 changed files with 88 additions and 88 deletions
61
models/forgefed/federationhost_repository.go
Normal file
61
models/forgefed/federationhost_repository.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package forgefed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/validation"
|
||||
)
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(FederationHost))
|
||||
}
|
||||
|
||||
func GetFederationHost(ctx context.Context, ID int64) (*FederationHost, error) {
|
||||
host := new(FederationHost)
|
||||
has, err := db.GetEngine(ctx).Where("id=?", ID).Get(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, fmt.Errorf("FederationInfo record %v does not exist", ID)
|
||||
}
|
||||
if res, err := validation.IsValid(host); !res {
|
||||
return nil, fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
return host, nil
|
||||
}
|
||||
|
||||
func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost, error) {
|
||||
host := new(FederationHost)
|
||||
// TODO: use parameter with toLower
|
||||
has, err := db.GetEngine(ctx).Where("host_fqdn=?", fqdn).Get(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, nil
|
||||
}
|
||||
if res, err := validation.IsValid(host); !res {
|
||||
return nil, fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
return host, nil
|
||||
}
|
||||
|
||||
func CreateFederationHost(ctx context.Context, host FederationHost) error {
|
||||
if res, err := validation.IsValid(host); !res {
|
||||
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
_, err := db.GetEngine(ctx).Insert(host)
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateFederationHost(ctx context.Context, host FederationHost) error {
|
||||
if res, err := validation.IsValid(host); !res {
|
||||
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
||||
}
|
||||
_, err := db.GetEngine(ctx).ID(host.ID).Update(host)
|
||||
return err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue