github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/action.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 "fmt" 24 "time" 25 26 "github.com/pkg/errors" 27 "gorm.io/gorm" 28 29 "github.com/e154/smart-home/common" 30 "github.com/e154/smart-home/common/apperr" 31 ) 32 33 // Actions ... 34 type Actions struct { 35 Db *gorm.DB 36 } 37 38 // Action ... 39 type Action struct { 40 Id int64 `gorm:"primary_key"` 41 Name string 42 Script *Script 43 ScriptId *int64 44 Entity *Entity 45 EntityId *common.EntityId 46 EntityActionName *string 47 AreaId *int64 48 Area *Area 49 Description string 50 CreatedAt time.Time `gorm:"<-:create"` 51 UpdatedAt time.Time 52 } 53 54 // TableName ... 55 func (*Action) TableName() string { 56 return "actions" 57 } 58 59 // Add ... 60 func (t Actions) Add(ctx context.Context, action *Action) (id int64, err error) { 61 if err = t.Db.WithContext(ctx).Create(&action).Error; err != nil { 62 err = errors.Wrap(apperr.ErrActionAdd, err.Error()) 63 return 64 } 65 id = action.Id 66 return 67 } 68 69 // GetById ... 70 func (t Actions) GetById(ctx context.Context, id int64) (action *Action, err error) { 71 action = &Action{} 72 err = t.Db.WithContext(ctx).Model(action). 73 Where("id = ?", id). 74 Preload("Entity"). 75 Preload("Script"). 76 Preload("Area"). 77 First(&action). 78 Error 79 if err != nil { 80 if errors.Is(err, gorm.ErrRecordNotFound) { 81 err = errors.Wrap(apperr.ErrActionNotFound, fmt.Sprintf("id \"%d\"", id)) 82 return 83 } 84 err = errors.Wrap(apperr.ErrActionGet, err.Error()) 85 } 86 return 87 } 88 89 // Update ... 90 func (t Actions) Update(ctx context.Context, m *Action) (err error) { 91 q := map[string]interface{}{ 92 "name": m.Name, 93 "description": m.Description, 94 "script_id": m.ScriptId, 95 "entity_id": m.EntityId, 96 "area_id": m.AreaId, 97 "entity_action_name": m.EntityActionName, 98 } 99 if err = t.Db.WithContext(ctx).Model(&Action{}).Where("id = ?", m.Id).Updates(q).Error; err != nil { 100 err = errors.Wrap(apperr.ErrActionUpdate, err.Error()) 101 } 102 return 103 } 104 105 // Delete ... 106 func (t Actions) Delete(ctx context.Context, id int64) (err error) { 107 if err = t.Db.WithContext(ctx).Delete(&Action{}, "id = ?", id).Error; err != nil { 108 err = errors.Wrap(apperr.ErrActionDelete, err.Error()) 109 } 110 return 111 } 112 113 // List ... 114 func (t *Actions) List(ctx context.Context, limit, offset int, orderBy, sort string, ids *[]uint64) (list []*Action, total int64, err error) { 115 116 if err = t.Db.WithContext(ctx).Model(Action{}).Count(&total).Error; err != nil { 117 err = errors.Wrap(apperr.ErrActionList, err.Error()) 118 return 119 } 120 121 list = make([]*Action, 0) 122 q := t.Db.WithContext(ctx).Model(&Action{}). 123 Limit(limit). 124 Offset(offset) 125 126 if sort != "" && orderBy != "" { 127 q = q. 128 Preload("Area"). 129 Order(fmt.Sprintf("%s %s", sort, orderBy)) 130 } 131 if ids != nil { 132 q = q.Where("id IN (?)", *ids) 133 } 134 if err = q.Find(&list).Error; err != nil { 135 err = errors.Wrap(apperr.ErrActionList, err.Error()) 136 } 137 return 138 } 139 140 // Search ...q 141 func (t *Actions) Search(ctx context.Context, query string, limit, offset int) (list []*Action, total int64, err error) { 142 143 q := t.Db.WithContext(ctx).Model(&Action{}). 144 Where("name LIKE ?", "%"+query+"%") 145 146 if err = q.Count(&total).Error; err != nil { 147 err = errors.Wrap(apperr.ErrActionSearch, err.Error()) 148 return 149 } 150 151 q = q. 152 Limit(limit). 153 Offset(offset). 154 Order("name ASC") 155 156 list = make([]*Action, 0) 157 err = q.Find(&list).Error 158 if err != nil { 159 err = errors.Wrap(apperr.ErrActionSearch, err.Error()) 160 } 161 return 162 }