package dao import ( "videoplayer/proto" "gorm.io/gorm" ) // ==================== DNSServer 相关操作 ==================== func CreateDNSServer(server proto.DNSServer) (uint, error) { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Create(&server) if res.Error != nil { return 0, res.Error } return server.ID, nil } func FindDNSServerByID(id uint) (proto.DNSServer, error) { var server proto.DNSServer var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("id = ?", id).First(&server) if res.Error != nil { return proto.DNSServer{}, res.Error } return server, nil } func FindDNSServerByUserID(userID uint) ([]proto.DNSServer, error) { var servers []proto.DNSServer var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("user_id = ?", userID).Find(&servers) if res.Error != nil { return nil, res.Error } return servers, nil } func FindAllDNSServer() ([]proto.DNSServer, error) { var servers []proto.DNSServer var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Find(&servers) if res.Error != nil { return nil, res.Error } return servers, nil } func UpdateDNSServer(id uint, server *proto.DNSServer) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Model(&proto.DNSServer{}).Where("id = ?", id).Updates(server) return res.Error } func DeleteDNSServerByID(id uint) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("id = ?", id).Delete(&proto.DNSServer{}) return res.Error } func DeleteDNSServerByUserID(userID uint) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("user_id = ?", userID).Delete(&proto.DNSServer{}) return res.Error } // ==================== DNSZone 相关操作 ==================== func CreateDNSZone(zone proto.DNSZone) (uint, error) { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Create(&zone) if res.Error != nil { return 0, res.Error } return zone.ID, nil } func FindDNSZoneByID(id uint) (proto.DNSZone, error) { var zone proto.DNSZone var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("id = ?", id).First(&zone) if res.Error != nil { return proto.DNSZone{}, res.Error } return zone, nil } func FindDNSZoneByServerID(serverID uint) ([]proto.DNSZone, error) { var zones []proto.DNSZone var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("server_id = ?", serverID).Find(&zones) if res.Error != nil { return nil, res.Error } return zones, nil } func FindAllDNSZone() ([]proto.DNSZone, error) { var zones []proto.DNSZone var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Find(&zones) if res.Error != nil { return nil, res.Error } return zones, nil } func UpdateDNSZone(id uint, zone *proto.DNSZone) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Model(&proto.DNSZone{}).Where("id = ?", id).Updates(zone) return res.Error } func DeleteDNSZoneByID(id uint) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("id = ?", id).Delete(&proto.DNSZone{}) return res.Error } func DeleteDNSZoneByServerID(serverID uint) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("server_id = ?", serverID).Delete(&proto.DNSZone{}) return res.Error } // ==================== DNSRecord 相关操作 ==================== func CreateDNSRecord(record proto.DNSRecord) (uint, error) { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Create(&record) if res.Error != nil { return 0, res.Error } return record.ID, nil } func FindDNSRecordByID(id uint) (proto.DNSRecord, error) { var record proto.DNSRecord var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("id = ?", id).First(&record) if res.Error != nil { return proto.DNSRecord{}, res.Error } return record, nil } func FindDNSRecordByZoneID(zoneID uint) ([]proto.DNSRecord, error) { var records []proto.DNSRecord var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("zone_id = ?", zoneID).Find(&records) if res.Error != nil { return nil, res.Error } return records, nil } func FindAllDNSRecord() ([]proto.DNSRecord, error) { var records []proto.DNSRecord var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Find(&records) if res.Error != nil { return nil, res.Error } return records, nil } func UpdateDNSRecord(id uint, record *proto.DNSRecord) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Model(&proto.DNSRecord{}).Where("id = ?", id).Updates(record) return res.Error } func DeleteDNSRecordByID(id uint) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("id = ?", id).Delete(&proto.DNSRecord{}) return res.Error } func DeleteDNSRecordByZoneID(zoneID uint) error { var db2 *gorm.DB if proto.Config.SERVER_SQL_LOG { db2 = DB.Debug() } else { db2 = DB } res := db2.Where("zone_id = ?", zoneID).Delete(&proto.DNSRecord{}) return res.Error }