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