installed gin

This commit is contained in:
2026-01-13 22:07:20 -07:00
parent 851234d9fa
commit 7481cec0d5
29 changed files with 544 additions and 1094 deletions
+43 -41
View File
@@ -9,44 +9,41 @@ import (
"ruben/inventory2/internal/logging"
"ruben/inventory2/internal/server/middleware"
"ruben/inventory2/internal/server/response"
"ruben/inventory2/internal/server/router"
"strconv"
"github.com/gin-gonic/gin"
)
type accountSubrouter struct {
log *logging.Logger
*router.SubMux
log *logging.Logger
accts *accounts.Store
}
func NewAccountSubrouter(
func Routes(
r *gin.RouterGroup,
logger *logging.Logger,
accts *accounts.Store,
authMiddleware *middleware.Auth,
) *accountSubrouter {
mux := router.NewSubMux(logger)
) {
as := &accountSubrouter{
log: logger,
SubMux: mux,
accts: accts,
log: logger,
accts: accts,
}
withAuth := func(fn response.HandlerFunc) response.HandlerFunc {
return authMiddleware.AuthenticateAndAddIdentity(fn)
withAuth := func(fn response.HandlerFunc) gin.HandlerFunc {
return response.Handler(authMiddleware.AuthenticateAndAddIdentity(fn))
}
mux.Handle("POST /{acctID}/inventory/sync-groups/draft/listings", withAuth(as.createSyncGroupListingDraft))
mux.Handle("PUT /{acctID}/inventory/sync-groups/draft/listings/{orderIndex}/shop", withAuth(as.setShopInSyncGroupListingDraft))
mux.Handle("PUT /{acctID}/inventory/sync-groups/draft/listings/{orderIndex}/listing", withAuth(as.setListingInSyncGroupListingDraft))
mux.Handle("DELETE /{acctID}/inventory/sync-groups/draft/listings/{orderIndex}", withAuth(as.deleteSyncGroupListingDraft))
mux.Handle("POST /{acctID}/inventory/sync-groups", withAuth(as.saveNewSyncGroup))
return as
r.POST("/:acctID/inventory/sync-groups/draft/listings", withAuth(as.createSyncGroupListingDraft))
r.PUT("/:acctID/inventory/sync-groups/draft/listings/:orderIndex/shop", withAuth(as.setShopInSyncGroupListingDraft))
r.PUT("/:acctID/inventory/sync-groups/draft/listings/:orderIndex/listing", withAuth(as.setListingInSyncGroupListingDraft))
r.DELETE("/:acctID/inventory/sync-groups/draft/listings/:orderIndex", withAuth(as.deleteSyncGroupListingDraft))
r.POST("/:acctID/inventory/sync-groups", withAuth(as.saveNewSyncGroup))
}
// POST /accounts
func (s *accountSubrouter) createAccount(r *http.Request) (response.Response, error) {
// POST /api/accounts
func (s *accountSubrouter) createAccount(c *gin.Context) (response.Response, error) {
r := c.Request
ctx := r.Context()
email := r.FormValue("email")
if email == "" {
@@ -64,11 +61,12 @@ func (s *accountSubrouter) createAccount(r *http.Request) (response.Response, er
return nil, response.Errorf("failed to create account: %w", err)
}
return response.SeeOther(fmt.Sprintf("/accounts/%d", acct.AccountID)), nil
return response.SeeOther(fmt.Sprintf("/api/accounts/%d", acct.AccountID)), nil
}
// POST /accounts/{acctID}/inventory/sync-groups/draft/listings
func (s *accountSubrouter) createSyncGroupListingDraft(r *http.Request) (response.Response, error) {
// POST /api/accounts/{acctID}/inventory/sync-groups/draft/listings
func (s *accountSubrouter) createSyncGroupListingDraft(c *gin.Context) (response.Response, error) {
r := c.Request
ctx := r.Context()
acctID := middleware.GetIdentity(ctx).Account.AccountID
@@ -79,18 +77,19 @@ func (s *accountSubrouter) createSyncGroupListingDraft(r *http.Request) (respons
return response.Redirect(
http.StatusSeeOther,
fmt.Sprintf("/accounts/%d/inventory/sync-groups/draft/listings/%d", acctID, orderIndex),
fmt.Sprintf("/ui/accounts/%d/inventory/sync-groups/draft/listings/%d", acctID, orderIndex),
), nil
}
// PUT /accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}/shop
// PUT /api/accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}/shop
// @platform string
// @shopID string
func (s *accountSubrouter) setShopInSyncGroupListingDraft(r *http.Request) (response.Response, error) {
func (s *accountSubrouter) setShopInSyncGroupListingDraft(c *gin.Context) (response.Response, error) {
r := c.Request
ctx := r.Context()
acctID := middleware.GetIdentity(ctx).Account.AccountID
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(r)
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(c)
if err != nil {
return nil, err
}
@@ -114,16 +113,17 @@ func (s *accountSubrouter) setShopInSyncGroupListingDraft(r *http.Request) (resp
return response.Redirect(
http.StatusSeeOther,
fmt.Sprintf("/accounts/%d/inventory/sync-groups/draft/listings/%d", acctID, orderIndex),
fmt.Sprintf("/ui/accounts/%d/inventory/sync-groups/draft/listings/%d", acctID, orderIndex),
), nil
}
// PUT /accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}/listing
func (s *accountSubrouter) setListingInSyncGroupListingDraft(r *http.Request) (response.Response, error) {
// PUT /api/accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}/listing
func (s *accountSubrouter) setListingInSyncGroupListingDraft(c *gin.Context) (response.Response, error) {
r := c.Request
ctx := r.Context()
acctID := middleware.GetIdentity(ctx).Account.AccountID
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(r)
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(c)
if err != nil {
return nil, err
}
@@ -140,16 +140,17 @@ func (s *accountSubrouter) setListingInSyncGroupListingDraft(r *http.Request) (r
return response.Redirect(
http.StatusSeeOther,
fmt.Sprintf("/accounts/%d/inventory/sync-groups/draft/listings/%d", acctID, orderIndex),
fmt.Sprintf("/ui/accounts/%d/inventory/sync-groups/draft/listings/%d", acctID, orderIndex),
), nil
}
// DELETE /accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}
func (s *accountSubrouter) deleteSyncGroupListingDraft(r *http.Request) (response.Response, error) {
// DELETE /api/accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}
func (s *accountSubrouter) deleteSyncGroupListingDraft(c *gin.Context) (response.Response, error) {
r := c.Request
ctx := r.Context()
acctID := middleware.GetIdentity(ctx).Account.AccountID
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(r)
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(c)
if err != nil {
return nil, err
}
@@ -161,8 +162,9 @@ func (s *accountSubrouter) deleteSyncGroupListingDraft(r *http.Request) (respons
return response.Status(200), nil
}
// POST /accounts/{acctID}/inventory/sync-groups
func (s *accountSubrouter) saveNewSyncGroup(r *http.Request) (response.Response, error) {
// POST /api/accounts/{acctID}/inventory/sync-groups
func (s *accountSubrouter) saveNewSyncGroup(c *gin.Context) (response.Response, error) {
r := c.Request
ctx := r.Context()
acctID := middleware.GetIdentity(ctx).Account.AccountID
@@ -174,12 +176,12 @@ func (s *accountSubrouter) saveNewSyncGroup(r *http.Request) (response.Response,
return response.Redirect(
http.StatusSeeOther,
// TODO: template not implemented
fmt.Sprintf("/accounts/%d/inventory/sync-groups/%d", acctID, grp.SyncGroupID),
fmt.Sprintf("/ui/accounts/%d/inventory/sync-groups/%d", acctID, grp.SyncGroupID),
), nil
}
func getOrderIndexForSyncGroupListingDraftFromPath(r *http.Request) (int, error) {
orderIndexStr := r.PathValue("orderIndex")
func getOrderIndexForSyncGroupListingDraftFromPath(c *gin.Context) (int, error) {
orderIndexStr := c.Param("orderIndex")
if orderIndexStr == "" {
return 0, response.NotFound().
Msg("no orderIndex found")