2024-02-22 20:52:37 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (subnet *Subnet) Save() error {
|
|
|
|
if err := conn.Save(subnet).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (subnet *Subnet) Delete() error {
|
|
|
|
if err := conn.Delete(subnet).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2024-02-23 11:22:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (subnet *Subnet) Get() *Subnet {
|
|
|
|
if err := conn.First(subnet).Error; err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return subnet
|
|
|
|
|
2024-02-22 20:52:37 +01:00
|
|
|
}
|
|
|
|
func CountSubnets() int {
|
|
|
|
var count int64
|
|
|
|
err := conn.Model(&Subnet{}).Count(&count)
|
|
|
|
if err != nil {
|
|
|
|
count = 0
|
|
|
|
log.Println("Error counting subnets: ")
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(count)
|
|
|
|
}
|
2024-02-23 11:22:28 +01:00
|
|
|
|
|
|
|
func (subnet *Subnet) Location() Location {
|
|
|
|
var location Location
|
|
|
|
|
|
|
|
conn.First(&location, subnet.LocationID)
|
|
|
|
return location
|
|
|
|
}
|
|
|
|
|
|
|
|
func SubnetsList(filter *Subnet, limit int, offset int, order string) []Subnet {
|
|
|
|
var subnets []Subnet
|
|
|
|
if order == "" {
|
|
|
|
order = "id desc"
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Where(filter).Limit(limit).Offset(offset).Order(order).Find(&subnets)
|
|
|
|
|
|
|
|
return subnets
|
|
|
|
}
|