From e68b9d00a6e05b3a941f63ffb696f91e554ac5ec Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 18 Oct 2024 20:33:49 +0200 Subject: Adding upstream version 9.0.3. Signed-off-by: Daniel Baumann --- models/forgefed/federationhost_repository.go | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 models/forgefed/federationhost_repository.go (limited to 'models/forgefed/federationhost_repository.go') diff --git a/models/forgefed/federationhost_repository.go b/models/forgefed/federationhost_repository.go new file mode 100644 index 0000000..03d8741 --- /dev/null +++ b/models/forgefed/federationhost_repository.go @@ -0,0 +1,61 @@ +// Copyright 2024 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package forgefed + +import ( + "context" + "fmt" + "strings" + + "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, err + } + return host, nil +} + +func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost, error) { + host := new(FederationHost) + has, err := db.GetEngine(ctx).Where("host_fqdn=?", strings.ToLower(fqdn)).Get(host) + if err != nil { + return nil, err + } else if !has { + return nil, nil + } + if res, err := validation.IsValid(host); !res { + return nil, err + } + return host, nil +} + +func CreateFederationHost(ctx context.Context, host *FederationHost) error { + if res, err := validation.IsValid(host); !res { + return 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 err + } + _, err := db.GetEngine(ctx).ID(host.ID).Update(host) + return err +} -- cgit v1.2.3