github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/db/user_device.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 "strings" 26 "time" 27 28 "github.com/jackc/pgx/v5/pgconn" 29 30 "github.com/jackc/pgerrcode" 31 "github.com/pkg/errors" 32 "gorm.io/gorm" 33 34 "github.com/e154/smart-home/common/apperr" 35 ) 36 37 // UserDevices ... 38 type UserDevices struct { 39 Db *gorm.DB 40 } 41 42 // UserDevice ... 43 type UserDevice struct { 44 Id int64 `gorm:"primary_key"` 45 UserId int64 46 PushRegistration json.RawMessage `gorm:"type:jsonb;not null"` 47 CreatedAt time.Time `gorm:"<-:create"` 48 } 49 50 // TableName ... 51 func (d *UserDevice) TableName() string { 52 return "user_devices" 53 } 54 55 // Add ... 56 func (d *UserDevices) Add(ctx context.Context, device *UserDevice) (id int64, err error) { 57 if err = d.Db.WithContext(ctx).Create(&device).Error; err != nil { 58 var pgErr *pgconn.PgError 59 if errors.As(err, &pgErr) { 60 switch pgErr.Code { 61 case pgerrcode.UniqueViolation: 62 if strings.Contains(pgErr.Message, "push_registration_at_user_devices_unq") { 63 err = errors.Wrap(apperr.ErrUserDeviceAdd, fmt.Sprintf("device \"%s\" not unique", device.PushRegistration)) 64 return 65 } 66 default: 67 fmt.Printf("unknown code \"%s\"\n", pgErr.Code) 68 } 69 } 70 err = errors.Wrap(apperr.ErrUserDeviceAdd, err.Error()) 71 return 72 } 73 id = device.Id 74 return 75 } 76 77 // GetByUserId ... 78 func (d *UserDevices) GetByUserId(ctx context.Context, id int64) (devices []*UserDevice, err error) { 79 devices = make([]*UserDevice, 0) 80 err = d.Db.WithContext(ctx).Model(&UserDevice{}). 81 Where("user_id = ?", id). 82 Find(&devices). 83 Error 84 85 if err != nil { 86 err = errors.Wrap(apperr.ErrUserDeviceGet, err.Error()) 87 } 88 return 89 } 90 91 // Delete ... 92 func (d *UserDevices) Delete(ctx context.Context, id int64) (err error) { 93 if err = d.Db.WithContext(ctx).Delete(&UserDevice{}, "id = ?", id).Error; err != nil { 94 err = errors.Wrap(apperr.ErrUserDeviceDelete, err.Error()) 95 } 96 return 97 } 98 99 // List ... 100 func (d *UserDevices) List(ctx context.Context, limit, offset int, orderBy, sort string) (list []*UserDevice, total int64, err error) { 101 102 list = make([]*UserDevice, 0) 103 q := d.Db.WithContext(ctx).Model(UserDevice{}) 104 if err = q.Count(&total).Error; err != nil { 105 err = errors.Wrap(apperr.ErrUserDeviceList, err.Error()) 106 return 107 } 108 err = q. 109 Limit(limit). 110 Offset(offset). 111 //Order(fmt.Sprintf("%s %s", sort, orderBy)). 112 Find(&list). 113 Error 114 if err != nil { 115 err = errors.Wrap(apperr.ErrUserDeviceList, err.Error()) 116 } 117 return 118 }