2024-01-12 08:33:52 -05:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package forgefed
|
|
|
|
|
|
|
|
import (
|
2024-01-12 10:12:54 -05:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-01-12 08:33:52 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-01-12 10:12:54 -05:00
|
|
|
"code.gitea.io/gitea/modules/validation"
|
2024-01-12 08:33:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(FederationInfo))
|
|
|
|
}
|
2024-01-12 10:12:54 -05:00
|
|
|
|
|
|
|
func GetFederationInfo(ctx context.Context, ID int64) (*FederationInfo, error) {
|
|
|
|
info := new(FederationInfo)
|
|
|
|
has, err := db.GetEngine(ctx).Where("id=?", ID).Get(info)
|
|
|
|
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(info); !res {
|
|
|
|
return nil, fmt.Errorf("FederationInfo is not valid: %v", err)
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2024-01-12 11:00:17 -05:00
|
|
|
func FindFederationInfoByHostFqdn(ctx context.Context, fqdn string) (*FederationInfo, error) {
|
2024-01-12 10:12:54 -05:00
|
|
|
info := new(FederationInfo)
|
2024-01-16 03:31:36 -05:00
|
|
|
// TODO: use parameter with toLower
|
2024-01-12 10:12:54 -05:00
|
|
|
has, err := db.GetEngine(ctx).Where("host_fqdn=?", fqdn).Get(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
2024-01-12 11:00:17 -05:00
|
|
|
return nil, nil
|
2024-01-12 10:12:54 -05:00
|
|
|
}
|
|
|
|
if res, err := validation.IsValid(info); !res {
|
|
|
|
return nil, fmt.Errorf("FederationInfo is not valid: %v", err)
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
2024-01-12 11:27:52 -05:00
|
|
|
|
|
|
|
func CreateFederationInfo(ctx context.Context, info FederationInfo) error {
|
|
|
|
if res, err := validation.IsValid(info); !res {
|
|
|
|
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
|
|
|
}
|
|
|
|
_, err := db.GetEngine(ctx).Insert(info)
|
|
|
|
return err
|
|
|
|
}
|
2024-01-13 08:17:11 -05:00
|
|
|
|
|
|
|
func UpdateFederationInfo(ctx context.Context, info FederationInfo) error {
|
|
|
|
if res, err := validation.IsValid(info); !res {
|
|
|
|
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
|
|
|
}
|
|
|
|
_, err := db.GetEngine(ctx).ID(info.ID).Update(info)
|
|
|
|
return err
|
|
|
|
}
|