Sort domains alphabetically

- GetAllDomains: order by name ASC
- GetUserAccessibleDomains: order by name ASC for both admin and regular users
This commit is contained in:
Christoph Haas 2026-03-23 00:11:03 +01:00
parent c692fe436f
commit 36258bf53c
4 changed files with 11 additions and 9 deletions

View file

@ -1,6 +1,7 @@
package handlers
import (
"log"
"net/http"
"strconv"
@ -138,6 +139,7 @@ func (h *UserHandler) Create(c *gin.Context) {
user, err := h.db.CreateUserInDomain(req.Email, passwordHash, req.Quota, domain.ID)
if err != nil {
log.Printf("CreateUserInDomain error: email=%s, domain=%s, err=%v", req.Email, domainName, err)
Error(c, http.StatusInternalServerError, "failed to create user")
return
}

View file

@ -44,13 +44,12 @@ func (Domain) TableName() string { return "virtual_domains" }
// User represents a mail user (e.g., "user@example.org").
// This corresponds to the existing ISPmail virtual_users table.
type User struct {
ID uint `gorm:"primaryKey" json:"id"` // Primary key
DomainID uint `gorm:"not null" json:"domainId"` // Foreign key to Domain
Email string `gorm:"uniqueIndex;size:100;not null" json:"email"` // Full email address, must be unique
Password string `gorm:"size:150;not null" json:"-"` // Mailbox password (bcrypt hash, - means exclude from JSON)
Quota int64 `gorm:"default:0" json:"quota"` // Mailbox size limit in bytes (0 = default)
Domain *Domain `gorm:"foreignKey:DomainID" json:"domain,omitempty"` // Associated domain
CreatedAt time.Time `json:"createdAt,omitempty"` // When the user was created
ID uint `gorm:"primaryKey" json:"id"` // Primary key
DomainID uint `gorm:"not null" json:"domainId"` // Foreign key to Domain
Email string `gorm:"uniqueIndex;size:100;not null" json:"email"` // Full email address, must be unique
Password string `gorm:"size:150;not null" json:"-"` // Mailbox password (bcrypt hash, - means exclude from JSON)
Quota int64 `gorm:"default:0" json:"quota"` // Mailbox size limit in bytes (0 = default)
Domain *Domain `gorm:"foreignKey:DomainID" json:"domain,omitempty"` // Associated domain
}
// TableName tells GORM to use the existing ISPmail table name.

View file

@ -2,7 +2,7 @@ package db
func (d *DB) GetAllDomains() ([]DomainStats, error) {
var domains []Domain
if err := d.Find(&domains).Error; err != nil {
if err := d.Order("name ASC").Find(&domains).Error; err != nil {
return nil, err
}

View file

@ -99,7 +99,7 @@ func (d *DB) GetUserAccessibleDomains(userID uint, isAdmin bool) ([]Domain, erro
// Admins get access to all domains.
if isAdmin {
var domains []Domain
if err := d.Find(&domains).Error; err != nil {
if err := d.Order("name ASC").Find(&domains).Error; err != nil {
return nil, err
}
return domains, nil
@ -113,6 +113,7 @@ func (d *DB) GetUserAccessibleDomains(userID uint, isAdmin bool) ([]Domain, erro
Select("virtual_domains.*"). // Select all columns from domains
Joins("JOIN virtual_domains ON virtual_domains.id = imc_users2domains.domain_id"). // Join to get domain details
Where("imc_users2domains.user_id = ?", userID). // Only this user's domains
Order("name ASC").
Find(&domains).Error
if err != nil {
return nil, err