reCoreD-UI/controllers/record.go

107 lines
2.1 KiB
Go
Raw Normal View History

2024-04-03 09:05:12 +00:00
package controllers
import (
"fmt"
2024-04-09 08:28:18 +00:00
"reCoreD-UI/database"
2024-04-03 09:05:12 +00:00
"reCoreD-UI/models"
2024-04-09 08:28:18 +00:00
"strconv"
2024-04-03 09:05:12 +00:00
"gorm.io/gorm"
)
2024-04-09 08:28:18 +00:00
type recordsDAO struct {
2024-04-09 13:16:19 +00:00
database.BaseDAO[models.IRecord]
2024-04-09 08:28:18 +00:00
}
2024-04-09 13:16:19 +00:00
func CreateRecord(r models.IRecord) (models.IRecord, error) {
if r.GetType() != models.RecordTypeSOA {
2024-04-09 08:28:18 +00:00
_, err := GetDomains(r.WithOutDotTail())
2024-04-03 09:05:12 +00:00
if err != nil {
2024-04-07 05:08:30 +00:00
return nil, err
2024-04-03 09:05:12 +00:00
}
}
2024-04-09 02:16:06 +00:00
if err := r.CheckZone(); err != nil {
return nil, err
}
2024-04-09 13:16:19 +00:00
res, err := (recordsDAO{}).Create(database.Client, r)
return res, err
2024-04-03 09:05:12 +00:00
}
2024-04-09 13:16:19 +00:00
func CreateRecords(rs []models.IRecord) error {
2024-04-09 08:28:18 +00:00
tx := database.Client.Begin()
for _, r := range rs {
if err := r.CheckZone(); err != nil {
tx.Rollback()
return err
}
2024-04-09 02:16:06 +00:00
2024-04-09 13:16:19 +00:00
if _, err := (recordsDAO{}).Create(tx, r); err != nil {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return err
2024-04-03 09:05:12 +00:00
}
2024-04-09 08:28:18 +00:00
}
2024-04-19 01:44:37 +00:00
2024-04-11 02:51:50 +00:00
return tx.Commit().Error
2024-04-03 09:05:12 +00:00
}
2024-04-09 13:16:19 +00:00
func GetRecords(cond models.IRecord) ([]models.IRecord, error) {
2024-04-09 08:28:18 +00:00
return (recordsDAO{}).GetAll(database.Client, cond)
}
2024-04-03 09:05:12 +00:00
2024-04-09 13:16:19 +00:00
func UpdateRecord(r models.IRecord) error {
2024-04-09 08:28:18 +00:00
if err := r.CheckZone(); err != nil {
return err
2024-04-03 09:05:12 +00:00
}
2024-04-09 13:16:19 +00:00
if _, err := (recordsDAO{}).Update(database.Client, r); err != nil {
2024-04-09 08:28:18 +00:00
return err
}
return nil
2024-04-03 09:05:12 +00:00
}
2024-04-09 08:28:18 +00:00
func DeleteRecord(domain, id string) error {
ID, err := strconv.Atoi(id)
if err != nil {
2024-04-09 02:16:06 +00:00
return err
}
2024-04-09 08:28:18 +00:00
tx := database.Client.Begin()
2024-04-19 01:44:37 +00:00
record, err := (recordsDAO{}).GetOne(tx, &models.Record[models.RecordContentDefault]{ID: uint(ID), Zone: fmt.Sprintf("%s.", domain)})
2024-04-09 08:28:18 +00:00
if err != nil {
tx.Rollback()
return err
}
2024-04-09 13:16:19 +00:00
if record.GetType() == models.RecordTypeSOA {
2024-04-09 08:28:18 +00:00
tx.Rollback()
return gorm.ErrRecordNotFound
}
if err := (recordsDAO{}).Delete(tx, record); err != nil {
tx.Rollback()
return err
}
2024-04-03 09:05:12 +00:00
2024-04-11 02:51:50 +00:00
return tx.Commit().Error
2024-04-03 09:05:12 +00:00
}
2024-04-07 02:07:26 +00:00
2024-04-09 08:28:18 +00:00
// for metrics
func getRecordCounts() (map[string]float64, error) {
2024-04-09 13:16:19 +00:00
rows, err := (recordsDAO{}).GetAll(database.Client, &models.Record[models.RecordContentDefault]{})
2024-04-07 02:07:26 +00:00
if err != nil {
return nil, err
}
result := make(map[string]float64)
2024-04-09 08:28:18 +00:00
for _, row := range rows {
2024-04-09 13:16:19 +00:00
record := &models.Record[models.RecordContentDefault]{}
if err := record.FromEntity(row); err != nil {
return nil, err
}
result[record.Zone] += 1
2024-04-07 02:07:26 +00:00
}
return result, nil
}