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,6 +1,9 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
|
@ -30,17 +33,28 @@ func (h *DomainHandler) List(c *gin.Context) {
|
|||
}
|
||||
|
||||
isAdmin := authCtx.IsAdmin()
|
||||
domains, err := h.db.GetUserAccessibleDomains(authCtx.UserID, isAdmin)
|
||||
domains, err := h.db.GetUserAccessibleDomains(c.Request.Context(), authCtx.UserID, isAdmin)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
}
|
||||
|
||||
// If admin, use optimized single-query method.
|
||||
if isAdmin {
|
||||
domainStats, err := h.db.GetAllDomainsWithCounts(c.Request.Context())
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
}
|
||||
Success(c, domainStats)
|
||||
return
|
||||
}
|
||||
|
||||
// For non-admins, build stats from their accessible domains.
|
||||
domainStats := make([]db.DomainStats, len(domains))
|
||||
for i, d := range domains {
|
||||
var userCount, aliasCount int64
|
||||
h.db.Model(&db.VirtualUser{}).Where("domain_id = ?", d.ID).Count(&userCount)
|
||||
h.db.Model(&db.VirtualAlias{}).Where("domain_id = ?", d.ID).Count(&aliasCount)
|
||||
userCount, _ := h.db.CountUsersByDomain(c.Request.Context(), d.ID)
|
||||
aliasCount, _ := h.db.CountAliasesByDomain(c.Request.Context(), d.ID)
|
||||
domainStats[i] = db.DomainStats{
|
||||
ID: d.ID,
|
||||
Name: d.Name,
|
||||
|
|
@ -59,7 +73,7 @@ func (h *DomainHandler) Get(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
domain, err := h.db.GetDomainByName(c.Request.Context(), domainName)
|
||||
if err != nil {
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue