github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/alexa_skill.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/e154/smart-home/common"
    29  	"github.com/pkg/errors"
    30  	"gorm.io/gorm"
    31  )
    32  
    33  // AlexaSkills ...
    34  type AlexaSkills struct {
    35  	Db *gorm.DB
    36  }
    37  
    38  // AlexaSkill ...
    39  type AlexaSkill struct {
    40  	Id          int64 `gorm:"primary_key"`
    41  	SkillId     string
    42  	Description string
    43  	Intents     []*AlexaIntent `gorm:"foreignkey:AlexaSkillId"`
    44  	Status      common.StatusType
    45  	Script      *Script
    46  	ScriptId    *int64
    47  	CreatedAt   time.Time `gorm:"<-:create"`
    48  	UpdatedAt   time.Time
    49  }
    50  
    51  // TableName ...
    52  func (d *AlexaSkill) TableName() string {
    53  	return "alexa_skills"
    54  }
    55  
    56  // Add ...
    57  func (n AlexaSkills) Add(ctx context.Context, v *AlexaSkill) (id int64, err error) {
    58  	if err = n.Db.WithContext(ctx).Create(&v).Error; err != nil {
    59  		err = errors.Wrap(apperr.ErrAlexaSkillAdd, err.Error())
    60  		return
    61  	}
    62  	id = v.Id
    63  	return
    64  }
    65  
    66  // GetById ...
    67  func (n AlexaSkills) GetById(ctx context.Context, id int64) (v *AlexaSkill, err error) {
    68  	v = &AlexaSkill{Id: id}
    69  	err = n.Db.WithContext(ctx).Model(v).
    70  		Preload("Script").
    71  		Preload("Intents").
    72  		Preload("Intents.Script").
    73  		Find(v).
    74  		Error
    75  	if err != nil {
    76  		if errors.Is(err, gorm.ErrRecordNotFound) {
    77  			err = errors.Wrap(apperr.ErrAlexaSkillNotFound, fmt.Sprintf("id \"%d\"", id))
    78  			return
    79  		}
    80  		err = errors.Wrap(apperr.ErrAlexaSkillGet, err.Error())
    81  		return
    82  	}
    83  	if err = n.preload(v); err != nil {
    84  		err = errors.Wrap(apperr.ErrAlexaSkillGet, err.Error())
    85  	}
    86  
    87  	return
    88  }
    89  
    90  // List ...
    91  func (n *AlexaSkills) List(ctx context.Context, limit, offset int, orderBy, sort string) (list []*AlexaSkill, total int64, err error) {
    92  
    93  	if err = n.Db.WithContext(ctx).Model(AlexaSkill{}).Count(&total).Error; err != nil {
    94  		err = errors.Wrap(apperr.ErrAlexaSkillList, err.Error())
    95  		return
    96  	}
    97  
    98  	list = make([]*AlexaSkill, 0)
    99  	q := n.Db.WithContext(ctx).Model(&AlexaSkill{}).
   100  		Limit(limit).
   101  		Offset(offset)
   102  
   103  	if sort != "" && orderBy != "" {
   104  		q = q.
   105  			Order(fmt.Sprintf("%s %s", sort, orderBy))
   106  	}
   107  
   108  	if err = q.Find(&list).Error; err != nil {
   109  		err = errors.Wrap(apperr.ErrAlexaSkillList, err.Error())
   110  	}
   111  
   112  	return
   113  }
   114  
   115  // ListEnabled ...
   116  func (n *AlexaSkills) ListEnabled(ctx context.Context, limit, offset int) (list []*AlexaSkill, err error) {
   117  
   118  	list = make([]*AlexaSkill, 0)
   119  	err = n.Db.WithContext(ctx).Model(&AlexaSkill{}).
   120  		Where("status = 'enabled'").
   121  		Limit(limit).
   122  		Offset(offset).
   123  		Preload("Intents").
   124  		Preload("Intents.Script").
   125  		Preload("Script").
   126  		Find(&list).Error
   127  
   128  	if err != nil {
   129  		err = errors.Wrap(apperr.ErrAlexaSkillList, err.Error())
   130  		return
   131  	}
   132  
   133  	//????
   134  	for _, skill := range list {
   135  		_ = n.preload(skill)
   136  	}
   137  
   138  	return
   139  }
   140  
   141  func (n AlexaSkills) preload(v *AlexaSkill) (err error) {
   142  	//todo fix
   143  	//err = n.Db.Model(v).
   144  	//	Related(&v.Intents).Error
   145  	//
   146  	//if err != nil {
   147  	//	err = errors.Wrap(err, "get related intents failed")
   148  	//	return
   149  	//}
   150  	//
   151  	//for _, intent := range v.Intents {
   152  	//	intent.Script = &Script{Id: intent.ScriptId}
   153  	//	err = n.Db.Model(intent).
   154  	//		Related(intent.Script).Error
   155  	//}
   156  	return
   157  }
   158  
   159  // Update ...
   160  func (n AlexaSkills) Update(ctx context.Context, v *AlexaSkill) (err error) {
   161  	q := map[string]interface{}{
   162  		"skill_id":    v.SkillId,
   163  		"status":      v.Status,
   164  		"description": v.Description,
   165  	}
   166  	if v.ScriptId != nil {
   167  		q["script_id"] = common.Int64Value(v.ScriptId)
   168  	}
   169  	if err = n.Db.WithContext(ctx).Model(&AlexaSkill{}).Where("id = ?", v.Id).Updates(q).Error; err != nil {
   170  		err = errors.Wrap(apperr.ErrAlexaSkillUpdate, err.Error())
   171  	}
   172  	return
   173  }
   174  
   175  // Delete ...
   176  func (n AlexaSkills) Delete(ctx context.Context, id int64) (err error) {
   177  	if err = n.Db.WithContext(ctx).Delete(&AlexaSkill{}, "id = ?", id).Error; err != nil {
   178  		err = errors.Wrap(apperr.ErrAlexaSkillDelete, err.Error())
   179  	}
   180  	return
   181  }