forgejo/models/forgefed/federationhost_repository.go

70 lines
1.7 KiB
Go
Raw Normal View History

2024-05-07 17:58:13 +02:00
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
import (
"context"
"fmt"
"strings"
"forgejo.org/models/db"
"forgejo.org/modules/validation"
2024-05-07 17:58:13 +02:00
)
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 {
2024-05-14 08:24:31 +02:00
return nil, err
2024-05-07 17:58:13 +02:00
}
return host, nil
}
func findFederationHostFromDB(ctx context.Context, searchKey, searchValue string) (*FederationHost, error) {
2024-05-07 17:58:13 +02:00
host := new(FederationHost)
has, err := db.GetEngine(ctx).Where(searchKey, searchValue).Get(host)
2024-05-07 17:58:13 +02:00
if err != nil {
return nil, err
} else if !has {
return nil, nil
}
if res, err := validation.IsValid(host); !res {
2024-05-14 08:24:31 +02:00
return nil, err
2024-05-07 17:58:13 +02:00
}
return host, nil
}
func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost, error) {
return findFederationHostFromDB(ctx, "host_fqdn=?", strings.ToLower(fqdn))
}
func FindFederationHostByKeyID(ctx context.Context, keyID string) (*FederationHost, error) {
return findFederationHostFromDB(ctx, "key_id=?", keyID)
}
2024-05-07 17:58:13 +02:00
func CreateFederationHost(ctx context.Context, host *FederationHost) error {
if res, err := validation.IsValid(host); !res {
2024-05-14 08:24:31 +02:00
return err
2024-05-07 17:58:13 +02:00
}
_, err := db.GetEngine(ctx).Insert(host)
return err
}
func UpdateFederationHost(ctx context.Context, host *FederationHost) error {
if res, err := validation.IsValid(host); !res {
2024-05-14 08:24:31 +02:00
return err
2024-05-07 17:58:13 +02:00
}
_, err := db.GetEngine(ctx).ID(host.ID).Update(host)
return err
}