de30497f59
- Implemented tenant and user management features including: - Tenant creation and management with associated migrations. - User creation and management with associated migrations. - Tenant membership management with associated migrations. - Platform user roles management with associated migrations. - Quota management with associated migrations. - Article and template management with associated migrations. - Added HTTP handlers for templates and workspaces. - Created tests for protected and public routes. - Introduced a script to check tenant scope in SQL queries. - Documented task plan for backend completion and frontend foundation.
102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
|
|
)
|
|
|
|
type QuotaLedgerInput struct {
|
|
TenantID int64
|
|
QuotaType string
|
|
Delta int
|
|
BalanceAfter int
|
|
Reason *string
|
|
ReferenceType *string
|
|
ReferenceID *int64
|
|
}
|
|
|
|
type QuotaReservationInput struct {
|
|
TenantID int64
|
|
QuotaType string
|
|
ResourceType string
|
|
ResourceID int64
|
|
ReservedAmount int
|
|
ExpireAt time.Time
|
|
}
|
|
|
|
type QuotaRepository interface {
|
|
GetCurrentBalance(ctx context.Context, tenantID int64, quotaType string) (int, error)
|
|
InsertQuotaLedger(ctx context.Context, input QuotaLedgerInput) (int64, error)
|
|
CreateQuotaReservation(ctx context.Context, input QuotaReservationInput) (int64, error)
|
|
UpdateQuotaReservationResource(ctx context.Context, reservationID, tenantID, resourceID int64) error
|
|
ConfirmReservation(ctx context.Context, reservationID, tenantID int64) error
|
|
RefundReservation(ctx context.Context, reservationID, tenantID int64) error
|
|
}
|
|
|
|
type quotaRepository struct {
|
|
q generated.Querier
|
|
}
|
|
|
|
func NewQuotaRepository(db generated.DBTX) QuotaRepository {
|
|
return "aRepository{q: newQuerier(db)}
|
|
}
|
|
|
|
func (r *quotaRepository) GetCurrentBalance(ctx context.Context, tenantID int64, quotaType string) (int, error) {
|
|
balance, err := r.q.GetCurrentBalance(ctx, generated.GetCurrentBalanceParams{
|
|
TenantID: tenantID,
|
|
QuotaType: quotaType,
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return int(balance), nil
|
|
}
|
|
|
|
func (r *quotaRepository) InsertQuotaLedger(ctx context.Context, input QuotaLedgerInput) (int64, error) {
|
|
return r.q.InsertQuotaLedger(ctx, generated.InsertQuotaLedgerParams{
|
|
TenantID: input.TenantID,
|
|
QuotaType: input.QuotaType,
|
|
Delta: int32(input.Delta),
|
|
BalanceAfter: int32(input.BalanceAfter),
|
|
Reason: pgText(input.Reason),
|
|
ReferenceType: pgText(input.ReferenceType),
|
|
ReferenceID: pgInt8(input.ReferenceID),
|
|
})
|
|
}
|
|
|
|
func (r *quotaRepository) CreateQuotaReservation(ctx context.Context, input QuotaReservationInput) (int64, error) {
|
|
expireAt := input.ExpireAt
|
|
return r.q.CreateQuotaReservation(ctx, generated.CreateQuotaReservationParams{
|
|
TenantID: input.TenantID,
|
|
QuotaType: input.QuotaType,
|
|
ResourceType: input.ResourceType,
|
|
ResourceID: input.ResourceID,
|
|
ReservedAmount: int32(input.ReservedAmount),
|
|
ExpireAt: pgTimestamp(&expireAt),
|
|
})
|
|
}
|
|
|
|
func (r *quotaRepository) UpdateQuotaReservationResource(ctx context.Context, reservationID, tenantID, resourceID int64) error {
|
|
return r.q.UpdateQuotaReservationResource(ctx, generated.UpdateQuotaReservationResourceParams{
|
|
ResourceID: resourceID,
|
|
ID: reservationID,
|
|
TenantID: tenantID,
|
|
})
|
|
}
|
|
|
|
func (r *quotaRepository) ConfirmReservation(ctx context.Context, reservationID, tenantID int64) error {
|
|
return r.q.ConfirmReservation(ctx, generated.ConfirmReservationParams{
|
|
ID: reservationID,
|
|
TenantID: tenantID,
|
|
})
|
|
}
|
|
|
|
func (r *quotaRepository) RefundReservation(ctx context.Context, reservationID, tenantID int64) error {
|
|
return r.q.RefundReservation(ctx, generated.RefundReservationParams{
|
|
ID: reservationID,
|
|
TenantID: tenantID,
|
|
})
|
|
}
|