Complete GORM to sqlc migration
- Remove GORM dependency, use sqlc for type-safe SQL queries - Update all handlers to use sqlc patterns (context, value types) - Fix N+1 query problem in domain listing with JOIN query - Enable SQL query logging in debug mode (USE_EMBEDDED=false) - Add comprehensive comments for non-Go developers
This commit is contained in:
parent
dad96978e0
commit
c4e3a31b69
9 changed files with 94 additions and 97 deletions
|
|
@ -1,9 +1,6 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
|
@ -33,15 +30,10 @@ func (h *DomainHandler) List(c *gin.Context) {
|
|||
}
|
||||
|
||||
isAdmin := authCtx.IsAdmin()
|
||||
domains, err := h.db.GetUserAccessibleDomains(c.Request.Context(), authCtx.UserID, isAdmin)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
|
||||
// If admin, use optimized single-query method.
|
||||
if isAdmin {
|
||||
domainStats, err := h.db.GetAllDomainsWithCounts(c.Request.Context())
|
||||
domainStats, err := h.db.GetAllDomainsWithCounts(ctx)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
|
|
@ -50,11 +42,16 @@ func (h *DomainHandler) List(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
// For non-admins, build stats from their accessible domains.
|
||||
domains, err := h.db.GetUserAccessibleDomains(ctx, uint32(authCtx.UserID), isAdmin)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
}
|
||||
|
||||
domainStats := make([]db.DomainStats, len(domains))
|
||||
for i, d := range domains {
|
||||
userCount, _ := h.db.CountUsersByDomain(c.Request.Context(), d.ID)
|
||||
aliasCount, _ := h.db.CountAliasesByDomain(c.Request.Context(), d.ID)
|
||||
userCount, _ := h.db.CountUsersByDomain(ctx, d.ID)
|
||||
aliasCount, _ := h.db.CountAliasesByDomain(ctx, d.ID)
|
||||
domainStats[i] = db.DomainStats{
|
||||
ID: d.ID,
|
||||
Name: d.Name,
|
||||
|
|
@ -73,7 +70,8 @@ func (h *DomainHandler) Get(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(c.Request.Context(), domainName)
|
||||
ctx := c.Request.Context()
|
||||
domain, err := h.db.GetDomainByName(ctx, domainName)
|
||||
if err != nil {
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
|
|
@ -85,7 +83,7 @@ func (h *DomainHandler) Get(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
canAccess, _ := h.db.CanAccessDomain(ctx, uint32(authCtx.UserID), domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
|
|
@ -113,19 +111,20 @@ func (h *DomainHandler) Create(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
existing, err := h.db.GetDomainByName(name)
|
||||
if err == nil && existing != nil {
|
||||
ctx := c.Request.Context()
|
||||
_, err := h.db.GetDomainByName(ctx, name)
|
||||
if err == nil {
|
||||
Error(c, http.StatusConflict, "domain already exists")
|
||||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.CreateDomain(name)
|
||||
err = h.db.CreateDomain(ctx, name)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "failed to create domain")
|
||||
return
|
||||
}
|
||||
|
||||
Created(c, domain)
|
||||
Created(c, map[string]string{"message": "domain created"})
|
||||
}
|
||||
|
||||
func validateDomainName(name string) error {
|
||||
|
|
@ -181,13 +180,15 @@ func (h *DomainHandler) Delete(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
ctx := c.Request.Context()
|
||||
domain, err := h.db.GetDomainByName(ctx, domainName)
|
||||
if err != nil {
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.DeleteDomain(domain.ID); err != nil {
|
||||
err = h.db.DeleteDomain(ctx, domain.ID)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "failed to delete domain")
|
||||
return
|
||||
}
|
||||
|
|
@ -196,20 +197,15 @@ func (h *DomainHandler) Delete(c *gin.Context) {
|
|||
}
|
||||
|
||||
type DomainPermissions struct {
|
||||
DomainID uint `json:"domainId"`
|
||||
DomainID uint32 `json:"domainId"`
|
||||
DomainName string `json:"domainName"`
|
||||
UserID uint `json:"userId"`
|
||||
UserID uint32 `json:"userId"`
|
||||
CanManage bool `json:"canManage"`
|
||||
}
|
||||
|
||||
func (h *DomainHandler) GetPermissions(c *gin.Context) {
|
||||
authCtx := GetAuthContext(c)
|
||||
if authCtx == nil {
|
||||
Error(c, http.StatusUnauthorized, "authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
if !authCtx.IsAdmin() {
|
||||
if authCtx == nil || !authCtx.IsAdmin() {
|
||||
Error(c, http.StatusForbidden, "admin access required")
|
||||
return
|
||||
}
|
||||
|
|
@ -220,13 +216,14 @@ func (h *DomainHandler) GetPermissions(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
ctx := c.Request.Context()
|
||||
domain, err := h.db.GetDomainByName(ctx, domainName)
|
||||
if err != nil {
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
}
|
||||
|
||||
users, err := h.db.GetUsersForDomain(domain.ID)
|
||||
users, err := h.db.GetUsersForDomain(ctx, domain.ID)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
|
|
@ -258,21 +255,23 @@ func (h *DomainHandler) AddPermission(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
ctx := c.Request.Context()
|
||||
domain, err := h.db.GetDomainByName(ctx, domainName)
|
||||
if err != nil {
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
UserID uint `json:"userId" binding:"required"`
|
||||
UserID uint32 `json:"userId" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
Error(c, http.StatusBadRequest, "invalid request")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.db.AddUserToDomain(req.UserID, domain.ID); err != nil {
|
||||
err = h.db.AddUserToDomain(ctx, req.UserID, domain.ID)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "failed to add user to domain")
|
||||
return
|
||||
}
|
||||
|
|
@ -293,7 +292,8 @@ func (h *DomainHandler) RemovePermission(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
domain, err := h.db.GetDomainByName(domainName)
|
||||
ctx := c.Request.Context()
|
||||
domain, err := h.db.GetDomainByName(ctx, domainName)
|
||||
if err != nil {
|
||||
Error(c, http.StatusNotFound, "domain not found")
|
||||
return
|
||||
|
|
@ -306,7 +306,8 @@ func (h *DomainHandler) RemovePermission(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if err := h.db.RemoveUserFromDomain(uint(userID), domain.ID); err != nil {
|
||||
err = h.db.RemoveUserFromDomain(ctx, uint32(userID), domain.ID)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "failed to remove user from domain")
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue