NAM-APJATEL-BACKEND/repository/devices_repo.go

48 lines
813 B
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
}
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
}