This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/internal/db/devices.go

45 lines
1.2 KiB
Go

package db
import (
"errors"
"gorm.io/gorm"
"golang.org/x/crypto/bcrypt"
log "github.com/sirupsen/logrus"
"reichard.io/imagini/internal/models"
)
func (dbm *DBManager) CreateDevice(device models.Device) error {
log.Info("[query] Creating device: ", device.Name)
_, err := dbm.Device(device)
if !errors.Is(err, gorm.ErrRecordNotFound) {
log.Warn("[query] Device already exists: ", device.Name)
return errors.New("Device already exists")
}
// Generate random password
refreshToken := "asd123"
hashedToken, err := bcrypt.GenerateFromPassword([]byte(refreshToken), bcrypt.DefaultCost)
if err != nil {
log.Error(err)
return err
}
device.RefreshToken = string(hashedToken)
return dbm.db.Create(&device).Error
}
func (dbm *DBManager) Device (device models.Device) (models.Device, error) {
var foundDevice models.Device
var count int64
err := dbm.db.Where(&device).First(&foundDevice).Count(&count).Error
return foundDevice, err
}
func (dbm *DBManager) DeleteDevice (user models.Device) error {
return nil
}
func (dbm *DBManager) UpdateRefreshToken (device models.Device, refreshToken string) error {
return nil
}