NAM-APJATEL-BACKEND/repository/devicePort_repo.go

61 lines
1.3 KiB
Go

package repository
import (
"users_management/m/model/entity"
"github.com/google/uuid"
"gorm.io/gorm"
)
type DevicePortRepo interface {
Post(devicePort entity.DevicePort) error
GetAll() ([]entity.DevicePort, error)
Update(id uuid.UUID, updates map[string]interface{}) error
GetByID(id uuid.UUID) (entity.DevicePort, error)
}
type devicePortRepo struct {
db *gorm.DB
}
func NewDevicePortRepo(db *gorm.DB) DevicePortRepo {
return &devicePortRepo{
db: db,
}
}
func (r *devicePortRepo) Post(devicePort entity.DevicePort) error {
err := r.db.Create(&devicePort).Error
if err != nil {
return err
}
return nil
}
func (r *devicePortRepo) GetAll() ([]entity.DevicePort, error) {
var devicePorts []entity.DevicePort
err := r.db.Preload("Device").Find(&devicePorts).Error
if err != nil {
return devicePorts, err
}
return devicePorts, nil
}
func (r *devicePortRepo) Update(id uuid.UUID,updates map[string]interface{}) error {
err := r.db.Model(&entity.DevicePort{}).Where("id = ?", id).Updates(updates).Error
if err != nil {
return err
}
return nil
}
func (r *devicePortRepo) GetByID(id uuid.UUID) (entity.DevicePort, error) {
var devicePort entity.DevicePort
err := r.db.Where("id = ?", id).Preload("Device").First(&devicePort).Error
if err != nil {
return devicePort, err
}
return devicePort, nil
}