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,120 +1,54 @@
|
|||
// Package db provides database access for the IMC application.
|
||||
// It uses GORM (Go ORM) for database operations.
|
||||
|
||||
package db
|
||||
|
||||
// =============================================================================
|
||||
// VirtualUser - Mail user operations
|
||||
// These functions interact with the ISPmail virtual_users table.
|
||||
// =============================================================================
|
||||
import (
|
||||
"context"
|
||||
|
||||
// GetAllMailUsers retrieves all mail users from the database.
|
||||
// Returns a slice of VirtualUser or an error.
|
||||
func (d *DB) GetAllMailUsers() ([]VirtualUser, error) {
|
||||
var users []VirtualUser
|
||||
if err := d.Preload("Domain").Find(&users).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if users == nil {
|
||||
users = []VirtualUser{}
|
||||
}
|
||||
return users, nil
|
||||
"database/sql"
|
||||
|
||||
imcdb "git.workaround.org/chaas/imc/backend/internal/db/sqlc"
|
||||
)
|
||||
|
||||
func (d *DB) GetAllMailUsers(ctx context.Context) ([]imcdb.VirtualUser, error) {
|
||||
return d.Queries.GetAllMailUsers(ctx)
|
||||
}
|
||||
|
||||
// GetUsersByDomain retrieves all mail users belonging to a specific domain.
|
||||
// domainID: the ID of the domain.
|
||||
// Returns a slice of VirtualUser or an error.
|
||||
func (d *DB) GetUsersByDomain(domainID uint) ([]VirtualUser, error) {
|
||||
var users []VirtualUser
|
||||
err := d.Where("domain_id = ?", domainID).Find(&users).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if users == nil {
|
||||
users = []VirtualUser{}
|
||||
}
|
||||
return users, nil
|
||||
func (d *DB) GetUsersByDomain(ctx context.Context, domainID uint32) ([]imcdb.VirtualUser, error) {
|
||||
return d.Queries.GetUsersByDomain(ctx, domainID)
|
||||
}
|
||||
|
||||
// GetUserByID retrieves a single mail user by their ID.
|
||||
// id: the user's ID.
|
||||
// Returns the VirtualUser or an error (including "record not found").
|
||||
func (d *DB) GetUserByID(id uint) (*VirtualUser, error) {
|
||||
var user VirtualUser
|
||||
err := d.Where("id = ?", id).Preload("Domain").First(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
func (d *DB) GetUserByID(ctx context.Context, id uint32) (imcdb.VirtualUser, error) {
|
||||
return d.Queries.GetUserByID(ctx, id)
|
||||
}
|
||||
|
||||
// GetUserByEmail looks up a mail user by their email address.
|
||||
// email: the full email address (e.g., "user@example.org").
|
||||
// Returns the VirtualUser or an error (including "record not found").
|
||||
func (d *DB) GetUserByEmail(email string) (*VirtualUser, error) {
|
||||
var user VirtualUser
|
||||
err := d.Where("email = ?", email).First(&user).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
func (d *DB) GetUserByEmail(ctx context.Context, email string) (imcdb.VirtualUser, error) {
|
||||
return d.Queries.GetUserByEmail(ctx, email)
|
||||
}
|
||||
|
||||
// CreateUser creates a new mail user without a domain association.
|
||||
// email: the full email address.
|
||||
// passwordHash: the bcrypt hash of the user's password.
|
||||
// quota: mailbox size limit in bytes (0 = use system default).
|
||||
// Returns the created VirtualUser or an error.
|
||||
func (d *DB) CreateUser(email, passwordHash string, quota int64) (*VirtualUser, error) {
|
||||
user := VirtualUser{
|
||||
Email: email,
|
||||
Password: passwordHash,
|
||||
Quota: quota,
|
||||
}
|
||||
if err := d.Create(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// CreateUserInDomain creates a new mail user in a specific domain.
|
||||
// email: the full email address.
|
||||
// passwordHash: the bcrypt hash of the user's password.
|
||||
// quota: mailbox size limit in bytes (0 = use system default).
|
||||
// domainID: the ID of the domain this user belongs to.
|
||||
// Returns the created VirtualUser or an error.
|
||||
func (d *DB) CreateUserInDomain(email, passwordHash string, quota int64, domainID uint) (*VirtualUser, error) {
|
||||
user := VirtualUser{
|
||||
func (d *DB) CreateUser(ctx context.Context, domainID uint32, email, passwordHash string, quota int64) error {
|
||||
quotaNull := sql.NullInt64{Int64: quota, Valid: quota > 0}
|
||||
return d.Queries.CreateUser(ctx, imcdb.CreateUserParams{
|
||||
DomainID: domainID,
|
||||
Email: email,
|
||||
Password: passwordHash,
|
||||
Quota: quota,
|
||||
}
|
||||
if err := d.Create(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
Quota: quotaNull,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateUserPassword updates the password for a mail user.
|
||||
// id: the user's ID.
|
||||
// passwordHash: the new bcrypt hash of the password.
|
||||
// Returns an error if the update fails.
|
||||
func (d *DB) UpdateUserPassword(id uint, passwordHash string) error {
|
||||
return d.Model(&VirtualUser{}).Updates(map[string]any{"password": passwordHash}).Error
|
||||
func (d *DB) UpdateUserPassword(ctx context.Context, id uint32, passwordHash string) error {
|
||||
return d.Queries.UpdateUserPassword(ctx, imcdb.UpdateUserPasswordParams{
|
||||
Password: passwordHash,
|
||||
ID: id,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateUserQuota updates the mailbox quota for a mail user.
|
||||
// id: the user's ID.
|
||||
// quota: new quota in bytes (0 = use system default).
|
||||
// Returns an error if the update fails.
|
||||
func (d *DB) UpdateUserQuota(id uint, quota int64) error {
|
||||
return d.Model(&VirtualUser{}).Updates(map[string]any{"quota": quota}).Error
|
||||
func (d *DB) UpdateUserQuota(ctx context.Context, id uint32, quota int64) error {
|
||||
quotaNull := sql.NullInt64{Int64: quota, Valid: quota > 0}
|
||||
return d.Queries.UpdateUserQuota(ctx, imcdb.UpdateUserQuotaParams{
|
||||
Quota: quotaNull,
|
||||
ID: id,
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteUser permanently removes a mail user from the database.
|
||||
// id: the user's ID to delete.
|
||||
// Returns an error if the deletion fails.
|
||||
func (d *DB) DeleteUser(id uint) error {
|
||||
return d.Delete(&VirtualUser{}, id).Error
|
||||
func (d *DB) DeleteUser(ctx context.Context, id uint32) error {
|
||||
return d.Queries.DeleteUser(ctx, id)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue