59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"users_management/m/model/entity"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type DevicesRepo interface {
|
|
Post(device entity.Device) error
|
|
GetAll() ([]entity.Device, error)
|
|
Update(device entity.Device) error
|
|
|
|
GetByID(id string) (entity.Device, error)
|
|
}
|
|
|
|
type devicesRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewDevicesRepo(db *gorm.DB) DevicesRepo {
|
|
return &devicesRepo{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (r *devicesRepo) Post(device entity.Device) error {
|
|
err := r.db.Create(&device).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *devicesRepo) GetAll() ([]entity.Device, error) {
|
|
var devices []entity.Device
|
|
err := r.db.Find(&devices).Error
|
|
if err != nil {
|
|
return devices, err
|
|
}
|
|
return devices, nil
|
|
}
|
|
func (r *devicesRepo) Update(device entity.Device) error {
|
|
err := r.db.Save(&device).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *devicesRepo) GetByID(id string) (entity.Device, error) {
|
|
var device entity.Device
|
|
err := r.db.Where("id = ?", id).First(&device).Error
|
|
if err != nil {
|
|
return device, err
|
|
}
|
|
return device, nil
|
|
}
|