github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/dashboard_card_item.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" 34 "github.com/e154/smart-home/common/apperr" 35 ) 36 37 // DashboardCardItems ... 38 type DashboardCardItems struct { 39 Db *gorm.DB 40 } 41 42 // DashboardCardItem ... 43 type DashboardCardItem struct { 44 Id int64 `gorm:"primary_key"` 45 Title string 46 Type string 47 Weight int 48 Enabled bool 49 DashboardCardId int64 50 DashboardCard *DashboardCard 51 EntityId *common.EntityId 52 Payload json.RawMessage `gorm:"type:jsonb;not null"` 53 Hidden bool 54 Frozen bool 55 CreatedAt time.Time `gorm:"<-:create"` 56 UpdatedAt time.Time 57 } 58 59 // TableName ... 60 func (d *DashboardCardItem) TableName() string { 61 return "dashboard_card_items" 62 } 63 64 // Add ... 65 func (n DashboardCardItems) Add(ctx context.Context, item *DashboardCardItem) (id int64, err error) { 66 if err = n.Db.WithContext(ctx).Create(&item).Error; err != nil { 67 var pgErr *pgconn.PgError 68 if errors.As(err, &pgErr) { 69 switch pgErr.Code { 70 case pgerrcode.ForeignKeyViolation: 71 if strings.Contains(pgErr.Message, "dashboard_card_item_2_entities_fk") { 72 details := pgErr.Detail 73 details = strings.Split(details, `Key (entity_id)=(`)[1] 74 details = strings.Split(details, `) is not present in table "entities".`)[0] 75 err = errors.Wrap(apperr.ErrEntityGet, fmt.Sprintf("with name \"%s\"", details)) 76 return 77 } 78 default: 79 fmt.Printf("unknown code \"%s\"\n", pgErr.Code) 80 } 81 } 82 err = errors.Wrap(apperr.ErrDashboardCardItemAdd, err.Error()) 83 return 84 } 85 id = item.Id 86 return 87 } 88 89 // GetById ... 90 func (n DashboardCardItems) GetById(ctx context.Context, id int64) (item *DashboardCardItem, err error) { 91 item = &DashboardCardItem{Id: id} 92 if err = n.Db.WithContext(ctx).First(&item).Error; err != nil { 93 if errors.Is(err, gorm.ErrRecordNotFound) { 94 err = errors.Wrap(apperr.ErrDashboardCardItemNotFound, fmt.Sprintf("with id \"%d\"", id)) 95 return 96 } 97 err = errors.Wrap(apperr.ErrDashboardCardItemGet, err.Error()) 98 } 99 return 100 } 101 102 // Update ... 103 func (n DashboardCardItems) Update(ctx context.Context, m *DashboardCardItem) (err error) { 104 q := map[string]interface{}{ 105 "title": m.Title, 106 "type": m.Type, 107 "weight": m.Weight, 108 "enabled": m.Enabled, 109 "dashboard_card_id": m.DashboardCardId, 110 "entity_id": m.EntityId, 111 "payload": m.Payload, 112 "hidden": m.Hidden, 113 } 114 115 if err = n.Db.WithContext(ctx).Model(&DashboardCardItem{Id: m.Id}).Updates(q).Error; err != nil { 116 err = errors.Wrap(apperr.ErrDashboardCardItemUpdate, err.Error()) 117 } 118 return 119 } 120 121 // Delete ... 122 func (n DashboardCardItems) Delete(ctx context.Context, id int64) (err error) { 123 if err = n.Db.WithContext(ctx).Delete(&DashboardCardItem{Id: id}).Error; err != nil { 124 err = errors.Wrap(apperr.ErrDashboardCardItemDelete, err.Error()) 125 } 126 return 127 } 128 129 // List ... 130 func (n *DashboardCardItems) List(ctx context.Context, limit, offset int, orderBy, sort string) (list []*DashboardCardItem, total int64, err error) { 131 132 if err = n.Db.WithContext(ctx).Model(DashboardCardItem{}).Count(&total).Error; err != nil { 133 err = errors.Wrap(apperr.ErrDashboardCardItemList, err.Error()) 134 return 135 } 136 137 list = make([]*DashboardCardItem, 0) 138 q := n.Db.WithContext(ctx).Model(&DashboardCardItem{}). 139 Limit(limit). 140 Offset(offset) 141 142 if sort != "" && orderBy != "" { 143 q = q. 144 Order(fmt.Sprintf("%s %s", sort, orderBy)) 145 } 146 147 if err = q.Find(&list).Error; err != nil { 148 err = errors.Wrap(apperr.ErrDashboardCardItemList, err.Error()) 149 } 150 151 return 152 }