NAM-APJATEL-BACKEND/repository/olt_repo.go

127 lines
3.6 KiB
Go

package repository
import (
"fmt"
"users_management/m/model/entity"
"users_management/m/model/dto/req"
"github.com/google/uuid"
"gorm.io/gorm"
)
type OLTRepo interface {
Create(olt entity.OLT) error
GetAll() ([]entity.OLT, error)
GetByID(id uuid.UUID) (entity.OLT, error)
GetByIDWithDevices(id uuid.UUID) (entity.OLT, error)
Update(id uuid.UUID, updateDTO req.UpdateOLTDTO) error
Delete(id uuid.UUID) error
AssignDeviceToOLT(oltID, deviceID uuid.UUID) error
UnassignDeviceFromOLT(deviceID uuid.UUID) error
GetByName(name string) (entity.OLT, error)
GetDevicesByOLTID(oltID uuid.UUID) ([]entity.Device, error)
}
type oltRepo struct {
db *gorm.DB
}
func NewOLTRepo(db *gorm.DB) OLTRepo {
return &oltRepo{db: db}
}
func (r *oltRepo) Create(olt entity.OLT) error {
return r.db.Create(&olt).Error
}
func (r *oltRepo) GetAll() ([]entity.OLT, error) {
var olts []entity.OLT
err := r.db.Order("created_at DESC").Find(&olts).Error
return olts, err
}
func (r *oltRepo) GetByID(id uuid.UUID) (entity.OLT, error) {
var olt entity.OLT
err := r.db.Where("id = ?", id).First(&olt).Error
return olt, err
}
func (r *oltRepo) GetByIDWithDevices(id uuid.UUID) (entity.OLT, error) {
var olt entity.OLT
err := r.db.Preload("Devices").
Preload("Devices.DevicePort").
Where("id = ?", id).
First(&olt).Error
return olt, err
}
func (r *oltRepo) Update(id uuid.UUID, updateDTO req.UpdateOLTDTO) error {
updates := make(map[string]interface{})
if updateDTO.OLTName != nil {
updates["olt_name"] = *updateDTO.OLTName
}
if len(updates) == 0 {
return fmt.Errorf("no fields to update")
}
return r.db.Model(&entity.OLT{}).Where("id = ?", id).Updates(updates).Error
}
func (r *oltRepo) Delete(id uuid.UUID) error {
return r.db.Transaction(func(tx *gorm.DB) error {
// First, unassign all devices from this OLT
if err := tx.Model(&entity.Device{}).
Where("olt_id = ?", id).
Update("olt_id", nil).Error; err != nil {
return err
}
// Then delete the OLT
return tx.Where("id = ?", id).Delete(&entity.OLT{}).Error
})
}
func (r *oltRepo) AssignDeviceToOLT(oltID, deviceID uuid.UUID) error {
return r.db.Transaction(func(tx *gorm.DB) error {
// Check if OLT exists
var olt entity.OLT
if err := tx.Where("id = ?", oltID).First(&olt).Error; err != nil {
return fmt.Errorf("OLT not found: %w", err)
}
// Check if device exists and is ODP type
var device entity.Device
if err := tx.Where("id = ?", deviceID).First(&device).Error; err != nil {
return fmt.Errorf("device not found: %w", err)
}
if device.DeviceType != entity.ODP {
return fmt.Errorf("only ODP devices can be assigned to OLT")
}
// Assign device to OLT
return tx.Model(&device).Update("olt_id", oltID).Error
})
}
func (r *oltRepo) UnassignDeviceFromOLT(deviceID uuid.UUID) error {
return r.db.Model(&entity.Device{}).
Where("id = ?", deviceID).
Update("olt_id", nil).Error
}
func (r *oltRepo) GetByName(name string) (entity.OLT, error) {
var olt entity.OLT
err := r.db.Where("olt_name = ?", name).First(&olt).Error
return olt, err
}
func (r *oltRepo) GetDevicesByOLTID(oltID uuid.UUID) ([]entity.Device, error) {
var devices []entity.Device
err := r.db.Preload("DevicePort").
Where("olt_id = ? AND device_type = ?", oltID, entity.ODP).
Find(&devices).Error
return devices, err
}