Dragonfly2

Форк
0
/
application.go 
104 строки · 2.9 Кб
1
/*
2
 *     Copyright 2020 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
	"d7y.io/dragonfly/v2/pkg/structure"
25
)
26

27
func (s *service) CreateApplication(ctx context.Context, json types.CreateApplicationRequest) (*models.Application, error) {
28
	priority, err := structure.StructToMap(json.Priority)
29
	if err != nil {
30
		return nil, err
31
	}
32

33
	application := models.Application{
34
		Name:     json.Name,
35
		URL:      json.URL,
36
		BIO:      json.BIO,
37
		Priority: priority,
38
		UserID:   json.UserID,
39
	}
40

41
	if err := s.db.WithContext(ctx).Create(&application).Error; err != nil {
42
		return nil, err
43
	}
44

45
	return &application, nil
46
}
47

48
func (s *service) DestroyApplication(ctx context.Context, id uint) error {
49
	application := models.Application{}
50
	if err := s.db.WithContext(ctx).First(&application, id).Error; err != nil {
51
		return err
52
	}
53

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

58
	return nil
59
}
60

61
func (s *service) UpdateApplication(ctx context.Context, id uint, json types.UpdateApplicationRequest) (*models.Application, error) {
62
	var (
63
		priority map[string]any
64
		err      error
65
	)
66
	if json.Priority != nil {
67
		priority, err = structure.StructToMap(json.Priority)
68
		if err != nil {
69
			return nil, err
70
		}
71
	}
72

73
	application := models.Application{}
74
	if err := s.db.WithContext(ctx).Preload("User").First(&application, id).Updates(models.Application{
75
		Name:     json.Name,
76
		URL:      json.URL,
77
		BIO:      json.BIO,
78
		Priority: priority,
79
		UserID:   json.UserID,
80
	}).Error; err != nil {
81
		return nil, err
82
	}
83

84
	return &application, nil
85
}
86

87
func (s *service) GetApplication(ctx context.Context, id uint) (*models.Application, error) {
88
	application := models.Application{}
89
	if err := s.db.WithContext(ctx).Preload("User").First(&application, id).Error; err != nil {
90
		return nil, err
91
	}
92

93
	return &application, nil
94
}
95

96
func (s *service) GetApplications(ctx context.Context, q types.GetApplicationsQuery) ([]models.Application, int64, error) {
97
	var count int64
98
	applications := []models.Application{}
99
	if err := s.db.WithContext(ctx).Scopes(models.Paginate(q.Page, q.PerPage)).Preload("User").Find(&applications).Limit(-1).Offset(-1).Count(&count).Error; err != nil {
100
		return nil, 0, err
101
	}
102

103
	return applications, count, nil
104
}
105

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

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

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

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