Dragonfly2

Форк
0
/
seed_peer.go 
104 строки · 3.1 Кб
1
/*
2
 *     Copyright 2022 The Dragonfly Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package service
18

19
import (
20
	"context"
21

22
	"d7y.io/dragonfly/v2/manager/models"
23
	"d7y.io/dragonfly/v2/manager/types"
24
)
25

26
func (s *service) CreateSeedPeer(ctx context.Context, json types.CreateSeedPeerRequest) (*models.SeedPeer, error) {
27
	seedPeer := models.SeedPeer{
28
		Hostname:          json.Hostname,
29
		Type:              json.Type,
30
		IDC:               json.IDC,
31
		Location:          json.Location,
32
		IP:                json.IP,
33
		Port:              json.Port,
34
		DownloadPort:      json.DownloadPort,
35
		ObjectStoragePort: json.ObjectStoragePort,
36
		SeedPeerClusterID: json.SeedPeerClusterID,
37
	}
38

39
	if err := s.db.WithContext(ctx).Create(&seedPeer).Error; err != nil {
40
		return nil, err
41
	}
42

43
	return &seedPeer, nil
44
}
45

46
func (s *service) DestroySeedPeer(ctx context.Context, id uint) error {
47
	seedPeer := models.SeedPeer{}
48
	if err := s.db.WithContext(ctx).First(&seedPeer, id).Error; err != nil {
49
		return err
50
	}
51

52
	if err := s.db.WithContext(ctx).Unscoped().Unscoped().Delete(&models.SeedPeer{}, id).Error; err != nil {
53
		return err
54
	}
55

56
	return nil
57
}
58

59
func (s *service) UpdateSeedPeer(ctx context.Context, id uint, json types.UpdateSeedPeerRequest) (*models.SeedPeer, error) {
60
	seedPeer := models.SeedPeer{}
61
	if err := s.db.WithContext(ctx).First(&seedPeer, id).Updates(models.SeedPeer{
62
		Type:              json.Type,
63
		IDC:               json.IDC,
64
		Location:          json.Location,
65
		IP:                json.IP,
66
		Port:              json.Port,
67
		DownloadPort:      json.DownloadPort,
68
		ObjectStoragePort: json.ObjectStoragePort,
69
		SeedPeerClusterID: json.SeedPeerClusterID,
70
	}).Error; err != nil {
71
		return nil, err
72
	}
73

74
	return &seedPeer, nil
75
}
76

77
func (s *service) GetSeedPeer(ctx context.Context, id uint) (*models.SeedPeer, error) {
78
	seedPeer := models.SeedPeer{}
79
	if err := s.db.WithContext(ctx).First(&seedPeer, id).Error; err != nil {
80
		return nil, err
81
	}
82

83
	return &seedPeer, nil
84
}
85

86
func (s *service) GetSeedPeers(ctx context.Context, q types.GetSeedPeersQuery) ([]models.SeedPeer, int64, error) {
87
	var count int64
88
	var seedPeers []models.SeedPeer
89
	if err := s.db.WithContext(ctx).Scopes(models.Paginate(q.Page, q.PerPage)).Where(&models.SeedPeer{
90
		Type:              q.Type,
91
		Hostname:          q.Hostname,
92
		IDC:               q.IDC,
93
		Location:          q.Location,
94
		IP:                q.IP,
95
		Port:              q.Port,
96
		DownloadPort:      q.DownloadPort,
97
		ObjectStoragePort: q.ObjectStoragePort,
98
		SeedPeerClusterID: q.SeedPeerClusterID,
99
	}).Find(&seedPeers).Limit(-1).Offset(-1).Count(&count).Error; err != nil {
100
		return nil, 0, err
101
	}
102

103
	return seedPeers, count, nil
104
}
105

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.