Ripped out literally everything to simplify the backend as much as possible. Some of the code was so horrifically complicated it's insaneeee
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package limits
|
|
|
|
import (
|
|
"database/sql"
|
|
|
|
. "screenmark/screenmark/.gen/haystack/haystack/table"
|
|
|
|
. "github.com/go-jet/jet/v2/postgres"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const (
|
|
LISTS_LIMIT = 10
|
|
IMAGE_LIMIT = 10
|
|
)
|
|
|
|
type LimitsManager struct {
|
|
dbPool *sql.DB
|
|
}
|
|
|
|
type LimitsManagerMethods interface {
|
|
HasReachedStackLimit(userID uuid.UUID) (bool, error)
|
|
HasReachedImageLimit(userID uuid.UUID) (bool, error)
|
|
}
|
|
|
|
type listCount struct {
|
|
ListCount int `alias:"list_count"`
|
|
}
|
|
|
|
func (m *LimitsManager) HasReachedStackLimit(userID uuid.UUID) (bool, error) {
|
|
getStacks := Stacks.
|
|
SELECT(COUNT(Stacks.UserID).AS("listCount.ListCount")).
|
|
WHERE(Stacks.UserID.EQ(UUID(userID)))
|
|
|
|
var count listCount
|
|
err := getStacks.Query(m.dbPool, &count)
|
|
|
|
return count.ListCount >= LISTS_LIMIT, err
|
|
}
|
|
|
|
type imageCount struct {
|
|
ImageCount int `alias:"image_count"`
|
|
}
|
|
|
|
func (m *LimitsManager) HasReachedImageLimit(userID uuid.UUID) (bool, error) {
|
|
getStacks := Image.
|
|
SELECT(COUNT(Image.UserID).AS("imageCount.ImageCount")).
|
|
WHERE(Image.UserID.EQ(UUID(userID)))
|
|
|
|
var count imageCount
|
|
err := getStacks.Query(m.dbPool, &count)
|
|
|
|
return count.ImageCount >= IMAGE_LIMIT, err
|
|
}
|
|
|
|
func CreateLimitsManager(db *sql.DB) *LimitsManager {
|
|
return &LimitsManager{
|
|
db,
|
|
}
|
|
}
|