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 = 50
|
|
)
|
|
|
|
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 := Lists.
|
|
SELECT(COUNT(Lists.UserID).AS("listCount.ListCount")).
|
|
WHERE(Lists.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 := UserImages.
|
|
SELECT(COUNT(UserImages.UserID).AS("imageCount.ImageCount")).
|
|
WHERE(UserImages.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,
|
|
}
|
|
}
|