Dragonfly2
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
17package service
18
19import (
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
27func (s *service) CreateApplication(ctx context.Context, json types.CreateApplicationRequest) (*models.Application, error) {
28priority, err := structure.StructToMap(json.Priority)
29if err != nil {
30return nil, err
31}
32
33application := models.Application{
34Name: json.Name,
35URL: json.URL,
36BIO: json.BIO,
37Priority: priority,
38UserID: json.UserID,
39}
40
41if err := s.db.WithContext(ctx).Create(&application).Error; err != nil {
42return nil, err
43}
44
45return &application, nil
46}
47
48func (s *service) DestroyApplication(ctx context.Context, id uint) error {
49application := models.Application{}
50if err := s.db.WithContext(ctx).First(&application, id).Error; err != nil {
51return err
52}
53
54if err := s.db.WithContext(ctx).Unscoped().Delete(&models.Application{}, id).Error; err != nil {
55return err
56}
57
58return nil
59}
60
61func (s *service) UpdateApplication(ctx context.Context, id uint, json types.UpdateApplicationRequest) (*models.Application, error) {
62var (
63priority map[string]any
64err error
65)
66if json.Priority != nil {
67priority, err = structure.StructToMap(json.Priority)
68if err != nil {
69return nil, err
70}
71}
72
73application := models.Application{}
74if err := s.db.WithContext(ctx).Preload("User").First(&application, id).Updates(models.Application{
75Name: json.Name,
76URL: json.URL,
77BIO: json.BIO,
78Priority: priority,
79UserID: json.UserID,
80}).Error; err != nil {
81return nil, err
82}
83
84return &application, nil
85}
86
87func (s *service) GetApplication(ctx context.Context, id uint) (*models.Application, error) {
88application := models.Application{}
89if err := s.db.WithContext(ctx).Preload("User").First(&application, id).Error; err != nil {
90return nil, err
91}
92
93return &application, nil
94}
95
96func (s *service) GetApplications(ctx context.Context, q types.GetApplicationsQuery) ([]models.Application, int64, error) {
97var count int64
98applications := []models.Application{}
99if 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 {
100return nil, 0, err
101}
102
103return applications, count, nil
104}
105