155 lines
5.9 KiB
Go
155 lines
5.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"users_management/m/model/entity"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type DeviceInspectionRepo interface {
|
|
Create(inspection entity.DeviceInspection) error
|
|
GetAll() ([]entity.DeviceInspection, error)
|
|
GetByID(id uuid.UUID) (entity.DeviceInspection, error)
|
|
GetByUserID(userID uuid.UUID, limit, offset int) ([]entity.DeviceInspection, error)
|
|
Update(id uuid.UUID, updates map[string]interface{}) error
|
|
CheckDeviceExists(deviceID uuid.UUID) (bool, error)
|
|
CheckBackboneExists(backboneID uuid.UUID) (bool, error)
|
|
CheckFishboneExists(fishboneID uuid.UUID) (bool, error)
|
|
CheckTowerExists(towerID uuid.UUID) (bool, error)
|
|
GetDeviceByID(deviceID uuid.UUID) (entity.Device, error)
|
|
CountCablesByDevice(deviceID uuid.UUID, deviceType string) (int, error)
|
|
CountByUserID(userID uuid.UUID) (int64, error)
|
|
CountAll() (int64, error)
|
|
UpdateDeviceStatus(deviceID uuid.UUID, status string) error
|
|
GetDevicePortByDeviceID(deviceID uuid.UUID) (entity.DevicePort, error)
|
|
UpdateDevicePort(deviceID uuid.UUID, updates map[string]interface{}) error
|
|
CheckOwnership(inspectionID, userID uuid.UUID) (bool, error) // Add this line
|
|
}
|
|
|
|
type deviceInspectionRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewDeviceInspectionRepo(db *gorm.DB) DeviceInspectionRepo {
|
|
return &deviceInspectionRepo{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) CheckOwnership(inspectionID, userID uuid.UUID) (bool, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.DeviceInspection{}).
|
|
Where("id = ? AND user_id = ?", inspectionID, userID).
|
|
Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) Create(inspection entity.DeviceInspection) error {
|
|
return r.db.Create(&inspection).Error
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) GetAll() ([]entity.DeviceInspection, error) {
|
|
var inspections []entity.DeviceInspection
|
|
err := r.db.Preload("Device").Preload("User").Preload("Backbone").
|
|
Preload("Fishbone").Preload("Tower").Find(&inspections).Error
|
|
return inspections, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) GetByID(id uuid.UUID) (entity.DeviceInspection, error) {
|
|
var inspection entity.DeviceInspection
|
|
err := r.db.Preload("Device").Preload("User").Preload("Backbone").
|
|
Preload("Fishbone").Preload("Tower").Where("id = ?", id).First(&inspection).Error
|
|
return inspection, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) GetByUserID(userID uuid.UUID, limit, offset int) ([]entity.DeviceInspection, error) {
|
|
var inspections []entity.DeviceInspection
|
|
err := r.db.Preload("Device").Preload("User").Preload("Backbone").
|
|
Preload("Fishbone").Preload("Tower").
|
|
Where("user_id = ?", userID).
|
|
Order("created_at DESC").
|
|
Limit(limit).Offset(offset).
|
|
Find(&inspections).Error
|
|
return inspections, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) Update(id uuid.UUID, updates map[string]interface{}) error {
|
|
return r.db.Model(&entity.DeviceInspection{}).Where("id = ?", id).Updates(updates).Error
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) CheckDeviceExists(deviceID uuid.UUID) (bool, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.Device{}).Where("id = ?", deviceID).Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) CheckBackboneExists(backboneID uuid.UUID) (bool, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.Backbone{}).Where("id = ?", backboneID).Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) CheckFishboneExists(fishboneID uuid.UUID) (bool, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.Fishbone{}).Where("id = ?", fishboneID).Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) CheckTowerExists(towerID uuid.UUID) (bool, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.Tower{}).Where("id = ?", towerID).Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) GetDeviceByID(deviceID uuid.UUID) (entity.Device, error) {
|
|
var device entity.Device
|
|
err := r.db.Where("id = ?", deviceID).First(&device).Error
|
|
return device, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) CountCablesByDevice(deviceID uuid.UUID, deviceType string) (int, error) {
|
|
var count int64
|
|
|
|
if deviceType == "OTB" {
|
|
// Count backbones using this device
|
|
err := r.db.Model(&entity.Backbone{}).
|
|
Where("dev_start_id = ? OR dev_end_id = ?", deviceID, deviceID).
|
|
Count(&count).Error
|
|
return int(count), err
|
|
} else if deviceType == "ODP" {
|
|
// Sum core amounts of fishbones using this device
|
|
var totalCores int64
|
|
err := r.db.Model(&entity.Fishbone{}).
|
|
Where("dev_start_id = ? OR dev_end_id = ?", deviceID, deviceID).
|
|
Select("COALESCE(SUM(core_amount), 0)").Scan(&totalCores).Error
|
|
return int(totalCores), err
|
|
}
|
|
|
|
return 0, nil
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) CountByUserID(userID uuid.UUID) (int64, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.DeviceInspection{}).Where("user_id = ?", userID).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) CountAll() (int64, error) {
|
|
var count int64
|
|
err := r.db.Model(&entity.DeviceInspection{}).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) UpdateDeviceStatus(deviceID uuid.UUID, status string) error {
|
|
return r.db.Model(&entity.Device{}).Where("id = ?", deviceID).Update("status", status).Error
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) GetDevicePortByDeviceID(deviceID uuid.UUID) (entity.DevicePort, error) {
|
|
var devicePort entity.DevicePort
|
|
err := r.db.Where("device_id = ?", deviceID).First(&devicePort).Error
|
|
return devicePort, err
|
|
}
|
|
|
|
func (r *deviceInspectionRepo) UpdateDevicePort(deviceID uuid.UUID, updates map[string]interface{}) error {
|
|
return r.db.Model(&entity.DevicePort{}).Where("device_id = ?", deviceID).Updates(updates).Error
|
|
} |