Dragonfly2
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
17package service18
19import (20"context"21
22"d7y.io/dragonfly/v2/manager/models"23"d7y.io/dragonfly/v2/manager/types"24)
25
26func (s *service) CreateSeedPeer(ctx context.Context, json types.CreateSeedPeerRequest) (*models.SeedPeer, error) {27seedPeer := models.SeedPeer{28Hostname: json.Hostname,29Type: json.Type,30IDC: json.IDC,31Location: json.Location,32IP: json.IP,33Port: json.Port,34DownloadPort: json.DownloadPort,35ObjectStoragePort: json.ObjectStoragePort,36SeedPeerClusterID: json.SeedPeerClusterID,37}38
39if err := s.db.WithContext(ctx).Create(&seedPeer).Error; err != nil {40return nil, err41}42
43return &seedPeer, nil44}
45
46func (s *service) DestroySeedPeer(ctx context.Context, id uint) error {47seedPeer := models.SeedPeer{}48if err := s.db.WithContext(ctx).First(&seedPeer, id).Error; err != nil {49return err50}51
52if err := s.db.WithContext(ctx).Unscoped().Unscoped().Delete(&models.SeedPeer{}, id).Error; err != nil {53return err54}55
56return nil57}
58
59func (s *service) UpdateSeedPeer(ctx context.Context, id uint, json types.UpdateSeedPeerRequest) (*models.SeedPeer, error) {60seedPeer := models.SeedPeer{}61if err := s.db.WithContext(ctx).First(&seedPeer, id).Updates(models.SeedPeer{62Type: json.Type,63IDC: json.IDC,64Location: json.Location,65IP: json.IP,66Port: json.Port,67DownloadPort: json.DownloadPort,68ObjectStoragePort: json.ObjectStoragePort,69SeedPeerClusterID: json.SeedPeerClusterID,70}).Error; err != nil {71return nil, err72}73
74return &seedPeer, nil75}
76
77func (s *service) GetSeedPeer(ctx context.Context, id uint) (*models.SeedPeer, error) {78seedPeer := models.SeedPeer{}79if err := s.db.WithContext(ctx).First(&seedPeer, id).Error; err != nil {80return nil, err81}82
83return &seedPeer, nil84}
85
86func (s *service) GetSeedPeers(ctx context.Context, q types.GetSeedPeersQuery) ([]models.SeedPeer, int64, error) {87var count int6488var seedPeers []models.SeedPeer89if err := s.db.WithContext(ctx).Scopes(models.Paginate(q.Page, q.PerPage)).Where(&models.SeedPeer{90Type: q.Type,91Hostname: q.Hostname,92IDC: q.IDC,93Location: q.Location,94IP: q.IP,95Port: q.Port,96DownloadPort: q.DownloadPort,97ObjectStoragePort: q.ObjectStoragePort,98SeedPeerClusterID: q.SeedPeerClusterID,99}).Find(&seedPeers).Limit(-1).Offset(-1).Count(&count).Error; err != nil {100return nil, 0, err101}102
103return seedPeers, count, nil104}
105