github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/dashboard_tab.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package db 20 21 import ( 22 "context" 23 "encoding/json" 24 "fmt" 25 "strings" 26 "time" 27 28 "github.com/jackc/pgerrcode" 29 "github.com/jackc/pgx/v5/pgconn" 30 "github.com/pkg/errors" 31 "gorm.io/gorm" 32 33 "github.com/e154/smart-home/common/apperr" 34 ) 35 36 // DashboardTabs ... 37 type DashboardTabs struct { 38 Db *gorm.DB 39 } 40 41 // DashboardTab ... 42 type DashboardTab struct { 43 Id int64 `gorm:"primary_key"` 44 Name string 45 Icon string 46 Enabled bool 47 Weight int 48 ColumnWidth int 49 Gap bool 50 Background *string 51 DashboardId int64 52 Dashboard *Dashboard 53 Cards []*DashboardCard 54 Payload json.RawMessage `gorm:"type:jsonb;not null"` 55 CreatedAt time.Time `gorm:"<-:create"` 56 UpdatedAt time.Time 57 } 58 59 // TableName ... 60 func (d *DashboardTab) TableName() string { 61 return "dashboard_tabs" 62 } 63 64 // Add ... 65 func (n DashboardTabs) Add(ctx context.Context, tab *DashboardTab) (id int64, err error) { 66 if err = n.Db.WithContext(ctx).Create(&tab).Error; err != nil { 67 var pgErr *pgconn.PgError 68 if errors.As(err, &pgErr) { 69 switch pgErr.Code { 70 case pgerrcode.UniqueViolation: 71 if strings.Contains(pgErr.Message, "name_at_dashboard_tabs_unq") { 72 err = errors.Wrap(apperr.ErrDashboardTabAdd, fmt.Sprintf("tab name \"%s\" not unique", tab.Name)) 73 return 74 } 75 default: 76 fmt.Printf("unknown code \"%s\"\n", pgErr.Code) 77 } 78 } 79 err = errors.Wrap(apperr.ErrDashboardTabAdd, err.Error()) 80 return 81 } 82 id = tab.Id 83 return 84 } 85 86 // GetById ... 87 func (n DashboardTabs) GetById(ctx context.Context, id int64) (tab *DashboardTab, err error) { 88 tab = &DashboardTab{} 89 err = n.Db.WithContext(ctx).Model(tab). 90 Where("id = ?", id). 91 Preload("Cards"). 92 Preload("Cards.Items"). 93 First(&tab).Error 94 95 if err != nil { 96 if errors.Is(err, gorm.ErrRecordNotFound) { 97 err = errors.Wrap(apperr.ErrDashboardTabNotFound, fmt.Sprintf("id \"%d\"", id)) 98 return 99 } 100 err = errors.Wrap(apperr.ErrDashboardTabGet, err.Error()) 101 return 102 } 103 return 104 } 105 106 // Update ... 107 func (n DashboardTabs) Update(ctx context.Context, tab *DashboardTab) (err error) { 108 q := map[string]interface{}{ 109 "name": tab.Name, 110 "icon": tab.Icon, 111 "column_width": tab.ColumnWidth, 112 "gap": tab.Gap, 113 "background": tab.Background, 114 "enabled": tab.Enabled, 115 "weight": tab.Weight, 116 "dashboard_id": tab.DashboardId, 117 "payload": tab.Payload, 118 } 119 120 if err = n.Db.WithContext(ctx).Model(&DashboardTab{Id: tab.Id}).Updates(q).Error; err != nil { 121 var pgErr *pgconn.PgError 122 if errors.As(err, &pgErr) { 123 switch pgErr.Code { 124 case pgerrcode.UniqueViolation: 125 if strings.Contains(pgErr.Message, "name_at_dashboard_tabs_unq") { 126 err = errors.Wrap(apperr.ErrDashboardTabUpdate, fmt.Sprintf("tab name \"%s\" not unique", tab.Name)) 127 return 128 } 129 default: 130 fmt.Printf("unknown code \"%s\"\n", pgErr.Code) 131 } 132 } 133 err = errors.Wrap(apperr.ErrDashboardTabUpdate, err.Error()) 134 } 135 return 136 } 137 138 // Delete ... 139 func (n DashboardTabs) Delete(ctx context.Context, id int64) (err error) { 140 if err = n.Db.WithContext(ctx).Delete(&DashboardTab{Id: id}).Error; err != nil { 141 err = errors.Wrap(apperr.ErrDashboardTabDelete, err.Error()) 142 } 143 return 144 } 145 146 // List ... 147 func (n *DashboardTabs) List(ctx context.Context, limit, offset int, orderBy, sort string) (list []*DashboardTab, total int64, err error) { 148 149 if err = n.Db.WithContext(ctx).Model(DashboardTab{}).Count(&total).Error; err != nil { 150 err = errors.Wrap(apperr.ErrDashboardTabList, err.Error()) 151 return 152 } 153 154 list = make([]*DashboardTab, 0) 155 q := n.Db. 156 WithContext(ctx). 157 Preload("Cards"). 158 Preload("Cards.Items"). 159 Limit(limit). 160 Offset(offset) 161 162 if sort != "" && orderBy != "" { 163 q = q.Order(fmt.Sprintf("%s %s", sort, orderBy)) 164 } 165 166 err = q. 167 Find(&list). 168 Error 169 170 if err != nil { 171 err = errors.Wrap(apperr.ErrDashboardTabList, err.Error()) 172 } 173 174 return 175 }