This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
/
resource_control.go
71 lines (63 loc) · 2 KB
/
resource_control.go
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
71
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package category
import (
"context"
"fmt"
"openpitrix.io/openpitrix/pkg/constants"
"openpitrix.io/openpitrix/pkg/db"
"openpitrix.io/openpitrix/pkg/logger"
"openpitrix.io/openpitrix/pkg/models"
"openpitrix.io/openpitrix/pkg/pi"
)
func (p *Server) getCategory(ctx context.Context, categoryId string) (*models.Category, error) {
categories, err := p.getCategories(ctx, []string{categoryId})
if err != nil {
return nil, err
}
if len(categories) == 0 {
logger.Error(ctx, "Failed to get category [%s]", categoryId)
return nil, fmt.Errorf("failed to get category [%s]", categoryId)
}
return categories[0], nil
}
func (p *Server) getCategories(ctx context.Context, categoryIds []string) ([]*models.Category, error) {
var categories []*models.Category
_, err := pi.Global().DB(ctx).
Select(models.CategoryColumns...).
From(constants.TableCategory).
Where(db.Eq(constants.ColumnCategoryId, categoryIds)).
Load(&categories)
if err != nil {
return nil, err
}
return categories, nil
}
func deleteRelations(ctx context.Context, categoryIds []string) (err error) {
_, err = pi.Global().DB(ctx).
Update(constants.TableCategoryResource).
Set(constants.ColumnStatus, constants.StatusDisabled).
Where(db.Eq(constants.ColumnCategoryId, categoryIds)).
Exec()
return
}
func deleteCateogries(ctx context.Context, categoryIds []string) (err error) {
_, err = pi.Global().DB(ctx).
DeleteFrom(constants.TableCategory).
Where(db.Eq(constants.ColumnCategoryId, categoryIds)).
Exec()
return
}
func countRelations(ctx context.Context, categoryIds []string) (uint32, error) {
count, err := pi.Global().DB(ctx).
Select("").
From(constants.TableCategoryResource).
Where(db.Eq(constants.ColumnCategoryId, categoryIds)).
Where(db.Eq(constants.ColumnStatus, constants.StatusEnabled)).
Count()
if err != nil {
return 0, err
}
return count, nil
}