58 lines
999 B
Go
58 lines
999 B
Go
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
|
|
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// Location() allows accessing the associated Location from the Subnet object
|
|
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
|
|
}
|
|
|
|
func SubnetByID(id uint) Subnet {
|
|
var subnet Subnet
|
|
conn.First(&subnet, id)
|
|
return subnet
|
|
}
|