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,7 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -35,19 +36,19 @@ func (h *AliasHandler) List(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
canAccess, _ := h.db.CanAccessDomain(c.Request.Context(), uint32(authCtx.UserID), domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
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
|
||||
}
|
||||
|
||||
aliases, err := h.db.GetAliasesByDomain(domain.ID)
|
||||
aliases, err := h.db.GetAliasesByDomain(c.Request.Context(), domain.ID)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "database error")
|
||||
return
|
||||
|
|
@ -69,13 +70,13 @@ func (h *AliasHandler) Create(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
canAccess, _ := h.db.CanAccessDomain(c.Request.Context(), uint32(authCtx.UserID), domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
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
|
||||
|
|
@ -117,19 +118,22 @@ func (h *AliasHandler) Create(c *gin.Context) {
|
|||
}
|
||||
|
||||
// Check for duplicate alias
|
||||
existing, _ := h.db.GetAliasBySource(req.Source)
|
||||
if existing != nil {
|
||||
_, err = h.db.GetAliasBySource(c.Request.Context(), req.Source)
|
||||
if err == nil {
|
||||
Error(c, http.StatusConflict, "alias already exists")
|
||||
return
|
||||
}
|
||||
if err != nil && !strings.Contains(err.Error(), "sql: no rows") {
|
||||
// Only error if it's not "no rows" error
|
||||
}
|
||||
|
||||
alias, err := h.db.CreateAliasInDomain(req.Source, req.Destination, domain.ID)
|
||||
err = h.db.CreateAlias(c.Request.Context(), domain.ID, req.Source, req.Destination)
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "failed to create alias")
|
||||
return
|
||||
}
|
||||
|
||||
Created(c, alias)
|
||||
Created(c, map[string]string{"message": "alias created"})
|
||||
}
|
||||
|
||||
func (h *AliasHandler) Delete(c *gin.Context) {
|
||||
|
|
@ -147,7 +151,7 @@ func (h *AliasHandler) Delete(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
canAccess, _ := h.db.CanAccessDomain(authCtx.UserID, domainName, authCtx.IsAdmin())
|
||||
canAccess, _ := h.db.CanAccessDomain(c.Request.Context(), uint32(authCtx.UserID), domainName, authCtx.IsAdmin())
|
||||
if !canAccess {
|
||||
Error(c, http.StatusForbidden, "access denied")
|
||||
return
|
||||
|
|
@ -159,7 +163,8 @@ func (h *AliasHandler) Delete(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if err := h.db.DeleteAlias(uint(id)); err != nil {
|
||||
err = h.db.DeleteAlias(c.Request.Context(), uint32(id))
|
||||
if err != nil {
|
||||
Error(c, http.StatusInternalServerError, "failed to delete alias")
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue