github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/dashboard_card.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  	"time"
    26  
    27  	"github.com/e154/smart-home/common"
    28  
    29  	"github.com/pkg/errors"
    30  	"gorm.io/gorm"
    31  
    32  	"github.com/e154/smart-home/common/apperr"
    33  )
    34  
    35  // DashboardCards ...
    36  type DashboardCards struct {
    37  	Db *gorm.DB
    38  }
    39  
    40  // DashboardCard ...
    41  type DashboardCard struct {
    42  	Id             int64 `gorm:"primary_key"`
    43  	Title          string
    44  	Weight         int
    45  	Width          int
    46  	Height         int
    47  	Background     *string
    48  	Enabled        bool
    49  	DashboardTabId int64
    50  	DashboardTab   *DashboardTab
    51  	Items          []*DashboardCardItem
    52  	Payload        json.RawMessage `gorm:"type:jsonb;not null"`
    53  	EntityId       *common.EntityId
    54  	Hidden         bool
    55  	CreatedAt      time.Time `gorm:"<-:create"`
    56  	UpdatedAt      time.Time
    57  }
    58  
    59  // TableName ...
    60  func (d *DashboardCard) TableName() string {
    61  	return "dashboard_cards"
    62  }
    63  
    64  // Add ...
    65  func (n DashboardCards) Add(ctx context.Context, card *DashboardCard) (id int64, err error) {
    66  	if err = n.Db.WithContext(ctx).Create(&card).Error; err != nil {
    67  		err = errors.Wrap(apperr.ErrDashboardCardAdd, err.Error())
    68  		return
    69  	}
    70  	id = card.Id
    71  	return
    72  }
    73  
    74  // GetById ...
    75  func (n DashboardCards) GetById(ctx context.Context, id int64) (card *DashboardCard, err error) {
    76  	card = &DashboardCard{Id: id}
    77  	err = n.Db.WithContext(ctx).Model(card).
    78  		Preload("Items").
    79  		First(&card).
    80  		Error
    81  
    82  	if err != nil {
    83  		if errors.Is(err, gorm.ErrRecordNotFound) {
    84  			err = errors.Wrap(apperr.ErrDashboardCardNotFound, err.Error())
    85  			return
    86  		}
    87  		err = errors.Wrap(apperr.ErrDashboardCardGet, err.Error())
    88  	}
    89  	return
    90  }
    91  
    92  // Update ...
    93  func (n DashboardCards) Update(ctx context.Context, m *DashboardCard) (err error) {
    94  	q := map[string]interface{}{
    95  		"title":            m.Title,
    96  		"height":           m.Height,
    97  		"background":       m.Background,
    98  		"weight":           m.Weight,
    99  		"width":            m.Width,
   100  		"enabled":          m.Enabled,
   101  		"dashboard_tab_id": m.DashboardTabId,
   102  		"payload":          m.Payload,
   103  		"hidden":           m.Hidden,
   104  		"entity_id":        m.EntityId,
   105  	}
   106  
   107  	if err = n.Db.WithContext(ctx).Model(&DashboardCard{Id: m.Id}).Updates(q).Error; err != nil {
   108  		err = errors.Wrap(apperr.ErrDashboardCardUpdate, err.Error())
   109  	}
   110  	return
   111  }
   112  
   113  // Delete ...
   114  func (n DashboardCards) Delete(ctx context.Context, id int64) (err error) {
   115  	if err = n.Db.WithContext(ctx).Delete(&DashboardCard{Id: id}).Error; err != nil {
   116  		err = errors.Wrap(apperr.ErrDashboardCardDelete, err.Error())
   117  	}
   118  	return
   119  }
   120  
   121  // List ...
   122  func (n *DashboardCards) List(ctx context.Context, limit, offset int, orderBy, sort string) (list []*DashboardCard, total int64, err error) {
   123  
   124  	if err = n.Db.WithContext(ctx).Model(DashboardCard{}).Count(&total).Error; err != nil {
   125  		err = errors.Wrap(apperr.ErrDashboardCardList, err.Error())
   126  		return
   127  	}
   128  
   129  	list = make([]*DashboardCard, 0)
   130  	q := n.Db.WithContext(ctx).Model(&DashboardCard{}).
   131  		Preload("Items").
   132  		Limit(limit).
   133  		Offset(offset)
   134  
   135  	if sort != "" && orderBy != "" {
   136  		q = q.
   137  			Order(fmt.Sprintf("%s %s", sort, orderBy))
   138  	}
   139  
   140  	if err = q.Find(&list).Error; err != nil {
   141  		err = errors.Wrap(apperr.ErrDashboardCardList, err.Error())
   142  	}
   143  
   144  	return
   145  }