github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/alexa_intent.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/e154/smart-home/common/apperr" 27 28 "github.com/pkg/errors" 29 "gorm.io/gorm" 30 ) 31 32 // AlexaIntents ... 33 type AlexaIntents struct { 34 Db *gorm.DB 35 } 36 37 // AlexaIntent ... 38 type AlexaIntent struct { 39 Name string `gorm:"primary_key"` 40 AlexaSkill *AlexaSkill 41 AlexaSkillId int64 42 Script *Script 43 ScriptId int64 44 Description string 45 CreatedAt time.Time `gorm:"<-:create"` 46 UpdatedAt time.Time 47 } 48 49 // TableName ... 50 func (d *AlexaIntent) TableName() string { 51 return "alexa_intents" 52 } 53 54 // Add ... 55 func (n AlexaIntents) Add(ctx context.Context, v *AlexaIntent) (err error) { 56 if err = n.Db.WithContext(ctx).Create(&v).Error; err != nil { 57 err = errors.Wrap(apperr.ErrAlexaIntentAdd, err.Error()) 58 } 59 return 60 } 61 62 // GetByName ... 63 func (n AlexaIntents) GetByName(ctx context.Context, name string) (intent *AlexaIntent, err error) { 64 intent = &AlexaIntent{} 65 if err = n.Db.WithContext(ctx).Model(intent).Where("name = ?", name).Error; err != nil { 66 if errors.Is(err, gorm.ErrRecordNotFound) { 67 err = errors.Wrap(apperr.ErrAlexaIntentNotFound, fmt.Sprintf("name \"w%s\"", name)) 68 return 69 } 70 err = errors.Wrap(apperr.ErrAlexaIntentGet, err.Error()) 71 } 72 return 73 } 74 75 // Update ... 76 func (n AlexaIntents) Update(ctx context.Context, v *AlexaIntent) (err error) { 77 err = n.Db.WithContext(ctx).Model(v).Where("name = ? and alexa_skill_id = ?", v.Name, v.AlexaSkillId).Updates(map[string]interface{}{ 78 "name": v.Name, 79 "description": v.Description, 80 "script_id": v.ScriptId, 81 }).Error 82 if err != nil { 83 err = errors.Wrap(apperr.ErrAlexaIntentUpdate, err.Error()) 84 } 85 return 86 } 87 88 // Delete ... 89 func (n AlexaIntents) Delete(ctx context.Context, v *AlexaIntent) (err error) { 90 if err = n.Db.WithContext(ctx).Delete(&AlexaIntent{}, "name = ? and alexa_skill_id = ?", v.Name, v.AlexaSkillId).Error; err != nil { 91 err = errors.Wrap(apperr.ErrAlexaIntentDelete, err.Error()) 92 } 93 return 94 }