WIP: Migrate from GORM to sqlc - partial migration, build broken
This commit is contained in:
parent
560b40503a
commit
dad96978e0
20 changed files with 1241 additions and 709 deletions
|
|
@ -1,98 +1,45 @@
|
|||
// Package db provides database access for the IMC application.
|
||||
// It uses GORM (Go ORM) for database operations.
|
||||
|
||||
package db
|
||||
|
||||
// =============================================================================
|
||||
// VirtualDomain - Mail domain operations
|
||||
// These functions interact with the ISPmail virtual_domains table.
|
||||
// =============================================================================
|
||||
import (
|
||||
"context"
|
||||
|
||||
// GetAllDomains retrieves all domains with user and alias counts.
|
||||
// Returns a slice of DomainStats (includes counts) or an error.
|
||||
func (d *DB) GetAllDomains() ([]DomainStats, error) {
|
||||
var domains []VirtualDomain
|
||||
err := d.Order("name ASC").Find(&domains).Error
|
||||
imcdb "git.workaround.org/chaas/imc/backend/internal/db/sqlc"
|
||||
)
|
||||
|
||||
func (d *DB) GetAllDomains(ctx context.Context) ([]imcdb.VirtualDomain, error) {
|
||||
return d.Queries.GetAllDomains(ctx)
|
||||
}
|
||||
|
||||
func (d *DB) GetDomainByID(ctx context.Context, id uint32) (imcdb.VirtualDomain, error) {
|
||||
return d.Queries.GetDomainByID(ctx, id)
|
||||
}
|
||||
|
||||
func (d *DB) GetDomainByName(ctx context.Context, name string) (imcdb.VirtualDomain, error) {
|
||||
return d.Queries.GetDomainByName(ctx, name)
|
||||
}
|
||||
|
||||
func (d *DB) CreateDomain(ctx context.Context, name string) error {
|
||||
return d.Queries.CreateDomain(ctx, name)
|
||||
}
|
||||
|
||||
func (d *DB) DeleteDomain(ctx context.Context, id uint32) error {
|
||||
return d.Queries.DeleteDomain(ctx, id)
|
||||
}
|
||||
|
||||
func (d *DB) GetAllDomainsWithCounts(ctx context.Context) ([]DomainStats, error) {
|
||||
results, err := d.Queries.GetAllDomainsWithCounts(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stats := make([]DomainStats, len(domains))
|
||||
for i, dom := range domains {
|
||||
var userCount, aliasCount int64
|
||||
d.Model(&VirtualUser{}).Where("domain_id = ?", dom.ID).Count(&userCount)
|
||||
d.Model(&VirtualAlias{}).Where("domain_id = ?", dom.ID).Count(&aliasCount)
|
||||
|
||||
stats := make([]DomainStats, len(results))
|
||||
for i, r := range results {
|
||||
stats[i] = DomainStats{
|
||||
ID: dom.ID,
|
||||
Name: dom.Name,
|
||||
UserCount: userCount,
|
||||
AliasCount: aliasCount,
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
UserCount: r.UserCount,
|
||||
AliasCount: r.AliasCount,
|
||||
}
|
||||
}
|
||||
|
||||
if stats == nil {
|
||||
stats = []DomainStats{}
|
||||
}
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// GetDomainByID retrieves a single domain by its ID.
|
||||
// id: the domain's ID.
|
||||
// Returns the VirtualDomain or an error (including "record not found").
|
||||
func (d *DB) GetDomainByID(id uint) (*VirtualDomain, error) {
|
||||
var domain VirtualDomain
|
||||
err := d.Where("id = ?", id).First(&domain).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &domain, nil
|
||||
}
|
||||
|
||||
// GetDomainByName retrieves a single domain by its name.
|
||||
// name: the domain name (e.g., "example.org").
|
||||
// Returns the VirtualDomain or an error (including "record not found").
|
||||
func (d *DB) GetDomainByName(name string) (*VirtualDomain, error) {
|
||||
var domain VirtualDomain
|
||||
err := d.Where("name = ?", name).First(&domain).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &domain, nil
|
||||
}
|
||||
|
||||
// CreateDomain creates a new mail domain.
|
||||
// name: the domain name (e.g., "example.org").
|
||||
// Returns the created VirtualDomain or an error.
|
||||
func (d *DB) CreateDomain(name string) (*VirtualDomain, error) {
|
||||
domain := VirtualDomain{Name: name}
|
||||
err := d.Create(&domain).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &domain, nil
|
||||
}
|
||||
|
||||
// DeleteDomain permanently removes a domain and all associated users and aliases.
|
||||
// id: the domain's ID to delete.
|
||||
// This also deletes all virtual_users and virtual_aliases belonging to this domain.
|
||||
// Returns an error if the deletion fails.
|
||||
func (d *DB) DeleteDomain(id uint) error {
|
||||
tx := d.Begin()
|
||||
defer func() { tx.Rollback() }()
|
||||
|
||||
err := tx.Where("domain_id = ?", id).Delete(&VirtualUser{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tx.Where("domain_id = ?", id).Delete(&VirtualAlias{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = tx.Delete(&VirtualDomain{}, id).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue