aboutsummaryrefslogtreecommitdiffstats
path: root/models/packages/package_property.go
blob: bf7dc346c6c975e8e532243c27e5f2fd883a84f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package packages

import (
	"context"

	"code.gitea.io/gitea/models/db"
)

func init() {
	db.RegisterModel(new(PackageProperty))
}

type PropertyType int64

const (
	// PropertyTypeVersion means the reference is a package version
	PropertyTypeVersion PropertyType = iota // 0
	// PropertyTypeFile means the reference is a package file
	PropertyTypeFile // 1
)

// PackageProperty represents a property of a package version or file
type PackageProperty struct {
	ID      int64        `xorm:"pk autoincr"`
	RefType PropertyType `xorm:"INDEX NOT NULL"`
	RefID   int64        `xorm:"INDEX NOT NULL"`
	Name    string       `xorm:"INDEX NOT NULL"`
	Value   string       `xorm:"TEXT NOT NULL"`
}

// InsertProperty creates a property
func InsertProperty(ctx context.Context, refType PropertyType, refID int64, name, value string) (*PackageProperty, error) {
	pp := &PackageProperty{
		RefType: refType,
		RefID:   refID,
		Name:    name,
		Value:   value,
	}

	_, err := db.GetEngine(ctx).Insert(pp)
	return pp, err
}

// GetProperties gets all properties
func GetProperties(ctx context.Context, refType PropertyType, refID int64) ([]*PackageProperty, error) {
	pps := make([]*PackageProperty, 0, 10)
	return pps, db.GetEngine(ctx).Where("ref_type = ? AND ref_id = ?", refType, refID).Find(&pps)
}

// GetPropertiesByName gets all properties with a specific name
func GetPropertiesByName(ctx context.Context, refType PropertyType, refID int64, name string) ([]*PackageProperty, error) {
	pps := make([]*PackageProperty, 0, 10)
	return pps, db.GetEngine(ctx).Where("ref_type = ? AND ref_id = ? AND name = ?", refType, refID, name).Find(&pps)
}

// DeleteAllProperties deletes all properties of a ref
func DeleteAllProperties(ctx context.Context, refType PropertyType, refID int64) error {
	_, err := db.GetEngine(ctx).Where("ref_type = ? AND ref_id = ?", refType, refID).Delete(&PackageProperty{})
	return err
}

// DeletePropertyByID deletes a property
func DeletePropertyByID(ctx context.Context, propertyID int64) error {
	_, err := db.GetEngine(ctx).ID(propertyID).Delete(&PackageProperty{})
	return err
}