readysite / hosting / controllers / billing.go
1.1 KB
billing.go
package controllers

import (
	"net/http"

	"github.com/readysite/readysite/hosting/internal/access"
	"github.com/readysite/readysite/hosting/models"
	"github.com/readysite/readysite/pkg/application"
)

// Billing returns the billing controller.
func Billing() (string, *BillingController) {
	return "billing", &BillingController{}
}

// BillingController handles the billing and plan page.
type BillingController struct {
	application.BaseController
}

// Setup registers routes.
func (c *BillingController) Setup(app *application.App) {
	c.BaseController.Setup(app)
	http.Handle("GET /billing", app.Serve("billing.html", RequireAuth))
}

// Handle returns a request-scoped controller instance.
func (c BillingController) Handle(r *http.Request) application.Controller {
	c.Request = r
	return &c
}

// UserSites returns the current user's non-deleted sites.
func (c *BillingController) UserSites() []*models.Site {
	user := access.GetUserFromJWT(c.Request)
	if user == nil {
		return nil
	}
	sites, err := models.Sites.Search("WHERE UserID = ? AND Status != 'deleted' ORDER BY CreatedAt DESC", user.ID)
	if err != nil {
		return nil
	}
	return sites
}
← Back