subrouters
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"ruben/inventory2/internal/consts"
|
||||
"ruben/inventory2/internal/domains/accounts"
|
||||
"ruben/inventory2/internal/server/middleware"
|
||||
"ruben/inventory2/internal/server/response"
|
||||
"ruben/inventory2/internal/server/router"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type accountSubrouter struct {
|
||||
log *slog.Logger
|
||||
*router.SubMux
|
||||
accts *accounts.Store
|
||||
}
|
||||
|
||||
func NewAccountSubrouter(
|
||||
logger *slog.Logger,
|
||||
accts *accounts.Store,
|
||||
authMiddleware *middleware.Auth,
|
||||
) *accountSubrouter {
|
||||
mux := router.NewSubMux(logger)
|
||||
|
||||
as := &accountSubrouter{
|
||||
log: logger,
|
||||
SubMux: mux,
|
||||
accts: accts,
|
||||
}
|
||||
|
||||
withAuth := func(fn response.HandlerFunc) response.HandlerFunc {
|
||||
return 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
|
||||
}
|
||||
|
||||
// POST /accounts
|
||||
func (s *accountSubrouter) createAccount(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
email := r.FormValue("email")
|
||||
if email == "" {
|
||||
return nil, response.BadRequest().Msg("no email provided")
|
||||
}
|
||||
|
||||
userID := middleware.GetIdentity(ctx).User.UserID
|
||||
|
||||
acct, err := s.accts.CreateAccount(ctx, userID, email)
|
||||
if err != nil {
|
||||
if errors.Is(err, consts.ErrConflict) {
|
||||
return nil, response.Conflict().
|
||||
Msg("user already has an account")
|
||||
}
|
||||
return nil, response.Errorf("failed to create account: %w", err)
|
||||
}
|
||||
|
||||
return response.SeeOther(fmt.Sprintf("/accounts/%d", acct.AccountID)), nil
|
||||
}
|
||||
|
||||
// POST /accounts/{acctID}/inventory/sync-groups/draft/listings
|
||||
func (s *accountSubrouter) createSyncGroupListingDraft(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
acctID := middleware.GetIdentity(ctx).Account.AccountID
|
||||
|
||||
orderIndex, err := s.accts.CreateSyncGroupListingDraft(ctx, acctID)
|
||||
if err != nil {
|
||||
return nil, response.Errorf("failed to create new listing draft: %w", err)
|
||||
}
|
||||
|
||||
return response.Redirect(
|
||||
http.StatusSeeOther,
|
||||
fmt.Sprintf("/accounts/%d/inventory/sync-groups/draft/listings/%d", acctID, orderIndex),
|
||||
), nil
|
||||
}
|
||||
|
||||
// PUT /accounts/{acctID}/inventory/sync-groups/draft/listings/{orderIndex}/shop
|
||||
// @platform string
|
||||
// @shopID string
|
||||
func (s *accountSubrouter) setShopInSyncGroupListingDraft(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
acctID := middleware.GetIdentity(ctx).Account.AccountID
|
||||
|
||||
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
platformStr := r.FormValue("platform")
|
||||
platform, err := accounts.NewPlatform(platformStr)
|
||||
if err != nil {
|
||||
return nil, response.BadRequest().
|
||||
Msgf("unrecognized platform: %s", platformStr)
|
||||
}
|
||||
|
||||
shopID := r.FormValue("shop-id")
|
||||
if shopID == "" {
|
||||
return nil, response.BadRequest().
|
||||
Msg("no shop-id provided")
|
||||
}
|
||||
|
||||
if err := s.accts.SetShopInSyncGroupListingDraft(ctx, acctID, orderIndex, platform, shopID); err != nil {
|
||||
return nil, response.Errorf("failed to set shop: %w", response.ErrorFromConstant(err))
|
||||
}
|
||||
|
||||
return response.Redirect(
|
||||
http.StatusSeeOther,
|
||||
fmt.Sprintf("/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) {
|
||||
ctx := r.Context()
|
||||
acctID := middleware.GetIdentity(ctx).Account.AccountID
|
||||
|
||||
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
listingID := r.FormValue("listing-id")
|
||||
if listingID == "" {
|
||||
return nil, response.BadRequest().
|
||||
Msg("no listing-id provided")
|
||||
}
|
||||
|
||||
if err := s.accts.SetListingInSyncGroupListingDraft(ctx, acctID, orderIndex, listingID); err != nil {
|
||||
return nil, response.Errorf("failed to set listing: %w", response.ErrorFromConstant(err))
|
||||
}
|
||||
|
||||
return response.Redirect(
|
||||
http.StatusSeeOther,
|
||||
fmt.Sprintf("/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) {
|
||||
ctx := r.Context()
|
||||
acctID := middleware.GetIdentity(ctx).Account.AccountID
|
||||
|
||||
orderIndex, err := getOrderIndexForSyncGroupListingDraftFromPath(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := s.accts.DeleteSyncGroupListingDraft(ctx, acctID, orderIndex); err != nil {
|
||||
return nil, response.Errorf("failed to delete listing: %w", response.ErrorFromConstant(err))
|
||||
}
|
||||
|
||||
return response.Status(200), nil
|
||||
}
|
||||
|
||||
// POST /accounts/{acctID}/inventory/sync-groups
|
||||
func (s *accountSubrouter) saveNewSyncGroup(r *http.Request) (response.Response, error) {
|
||||
ctx := r.Context()
|
||||
acctID := middleware.GetIdentity(ctx).Account.AccountID
|
||||
|
||||
grp, err := s.accts.SaveNewSyncGroup(ctx, acctID)
|
||||
if err != nil {
|
||||
return nil, response.Errorf("failed to save new sync group: %w", response.ErrorFromConstant(err))
|
||||
}
|
||||
|
||||
return response.Redirect(
|
||||
http.StatusSeeOther,
|
||||
// TODO: template not implemented
|
||||
fmt.Sprintf("/accounts/%d/inventory/sync-groups/%d", acctID, grp.SyncGroupID),
|
||||
), nil
|
||||
}
|
||||
|
||||
func getOrderIndexForSyncGroupListingDraftFromPath(r *http.Request) (int, error) {
|
||||
orderIndexStr := r.PathValue("orderIndex")
|
||||
if orderIndexStr == "" {
|
||||
return 0, response.NotFound().
|
||||
Msg("no orderIndex found")
|
||||
}
|
||||
|
||||
orderIndex, err := strconv.Atoi(orderIndexStr)
|
||||
if err != nil {
|
||||
return 0, response.NotFound().
|
||||
Msgf("invalid order index: %s", orderIndexStr)
|
||||
}
|
||||
|
||||
return orderIndex, nil
|
||||
}
|
||||
Reference in New Issue
Block a user